简体   繁体   中英

Floats being rounded in C++ and I don't understand why

I am very confused about this... Here is an extract from my code..

float m = 0.0, c = 0.0;
printf("toprightx = %d bottomrightx = %d toprighty = %d bottomrighty = %d\n",
    toprightx, bottomrightx, toprighty, bottomrighty);
// find m and c for symmetry line
if (toprightx == bottomrightx) {
  m = (-toprighty + bottomrighty);
}
else {
  m = (-toprighty + bottomrighty) / (toprightx - bottomrightx);
}

c = -toprighty - (m * toprightx);

printf("m = %f and c = %f\n", m, c);

And here is the output:

toprightx = 241 bottomrightx = 279 toprighty = 174 bottomrighty = 321
m = -3.000000 and c = 549.000000

Why is the output rounding m and c? I have declared them as floats so I don't understand why the code is returning integers. The correct value of m should be -3.8684.

(Note that toprightx, bottomrightx, toprighty, bottomrighty have been declared as integers further up in the code.)

Note that toprightx, bottomrightx, toprighty, bottomrighty have been declared as integers further up in the code.

There's your answer. Calculations that involve only integers are performed in integer math, including divisions. It doesn't matter that the result is then assigned to a float.

To fix this, either declare at least one of the x/y values as float or cast it to float in the calculation.

You are performing integer division on this line:

(-toprighty + bottomrighty) / (toprightx - bottomrightx);

Since topright, bottomrighty, toprightx, and bottomrightx are all integers, the result of that equation will also be an integer. After the equaition calculates an integer you are assigning it to a float. It is equivalent to:

float m = -3;

You could do something like this instead:

(-toprighty + bottomrighty + 0.0) / (toprightx - bottomrightx);

Here's ah int for you:

m = (-toprighty + bottomrighty) / (toprightx - bottomrightx);
       ^int        ^int              ^int        ^int

All of those operations will be performed using integer division (truncating floating points) and then cast to float . Try instead:

m = float(-toprighty + bottomrighty) / (toprightx - bottomrightx);

That's because you're using only int's on your calculations, so C++ uses integer calculation for them. Just cast one of your int variables to float and you'll be good.

Changing this statement m = (-toprighty + bottomrighty) / (toprightx - bottomrightx); to m = (-toprighty + bottomrighty) / (float)(toprightx - bottomrightx); will do that.

declare toprightx, bottomrightx, toprighty, bottomrighty as floats or cast them to floats before asking for mixed arithmetic.

Casting(implicitly, as you're doing) a float to an int will truncate the data that won't fit in the new type.

Note that your data isn't being rounded either, it's being truncated.

Try casting the divisor to a floating point number, to force the division to use floating point arithmetic:

m = (-toprighty + bottomrighty) / (float)(toprightx - bottomrightx);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM