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.
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>?
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;
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.
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
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.