General math calculations

1
2
3
int calc_x = floor(origin.x/32);
int calc_y = floor(origin.y/32);
int test_square = floor(calc_y*ROOM_WIDTH)+calc_x;


if origin.x and origin.y are both set to 100 and ROOM_WIDTH is 20 (set elsewhere in code.)
what should test_square be set to?

I hope its being set to 63. but im not sure.


closed account (zb0S216C)
You code should look like this:

1
2
3
float calc_x = floor( 100 / 32 ); // 3.125
float calc_y = floor( 100 / 32 ); // 3.125
float test_square = floor( 3.125 * 20 ) /* = 62.5 */ + 3.125 = 65.625;


GPP wrote:
what should test_square be set to?

What do you mean?

Wazzak
origin.x and origin.y are always changing.
and I was hopping that the floor function made 100/32 = 3 instead of 3.blahblahblah
closed account (zb0S216C)
As you can see in my conversion, the division operation of calc_x and calc_y returns with a real number. However, if calc_x and calc_y are of type int, the .125 will be truncated (since int cannot hold real numbers), leaving you with the value of 3.

Wazzak
Last edited on
I dont need any decimals, I want test_square to be set to 63
closed account (zb0S216C)
GPP wrote:
I dont need any decimals, I want test_square to be set to 63

Not possible. You need calc_y to hold the value of 3.15 (which it can't) in order to receive the value of 63 from the computation.

Wazzak
oh maybe Im using the floor command wrong. I thought calc_y = floor(3.15) would set calc_y to 3? if not how do I round down the decimal. with floor() command found in <math.h>?
Last edited on
closed account (zb0S216C)
GPP wrote:
I thought calc_y = floor(3.15) would set calc_y to 3?

Yes, is does. Since calc_y is of type int (can only contain whole numbers), truncation takes place when a real number is assigned to it (cutting off the .125).

GPP wrote:
if not how do I round down the decimal. with floor() command found in <math.h>?

You can perform a C-style cast on calc_y (force the conversion) when you pass it to floor( ). For example:

1
2
// Force the conversion...
int test_square = floor( ( double )calc_y * ROOM_SIZE ) + calc_x;

Wazzak
Last edited on
ohhhh. that looks like a lot there.
sooo.

int calc_x = floor( ( int ) origin.x/32);

calc_x is now set to 3?
closed account (zb0S216C)
floor( ) has 3 sets of argument lists[1]; all of them require a real number. When you explicitly cast an object to fit the required argument, you're taking a copy of the object, converting it, then letting the function execute. For example:

1
2
3
4
int test_square = floor( ( double )calc_y * ROOM_SIZE );

// Here, when 'calc_y' is passed to 'floor( )', a copy of 'calc_y' is made, then converted 
// to a 'double' object. However, the actual 'calc_y' object isn't converted.  


GPP wrote:
calc_x is now set to 3?

You're casting an int to an int, therefore, no casting takes place.

References:
[1]http://www.cplusplus.com/reference/clibrary/cmath/floor/


Wazzak
Last edited on
okay I just realized that test_square, should never have to deal with decimals.


1
2
3
4
    int calc_x = floor(origin.x/32);
    int calc_y = floor(origin.y/32);

    int test_square = calc_y*ROOM_WIDTH+calc_x;


Im trying to set test_square to a interger.

now when origin.x = 100
and origin.y = 100
and ROOM_WIDTH = 20

I want the program to set test_square to 63,
Im hoping my code is doing this calculation by then end given the inputs.
test_square = 3*20+3
test_square = 63


closed account (zb0S216C)
test_square will never have the value of 63 unless calc_y is holding the value of 3.15 (which it can't). Unless one of your 3 objects is holding the value of 3.15 (which they wan't), you will have to add the value of 3 at the end of the expression (assuming your computation evaluates to 60), or, explicitly assign the value of 3 to one of the 3 objects, which will alter the outcome of the computation.

Wazzak
calc_x = floor(origin.x/32)
calc_x = floor((100)/32)
calc_x = floor(3.125)
calc_x = 3

calc_y = floor(origin.y/32)
calc_y = floor((100)/32)
calc_y = floor(3.125)
calc_y = 3

ROOM_WIDTH = 20



test_square = calc_y*ROOMWITH+calc_x

test_square = (3)*(20)+(3)
test_square = (60)+(3)
test_square = 63..

why dosnt my code work like that?
Topic archived. No new replies allowed.