简体   繁体   English

浮点数在 C++ 中被舍入,我不明白为什么

[英]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:这是 output:

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

Why is the output rounding m and c?为什么 output 舍入 m 和 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. m 的正确值应该是 -3.8684。

(Note that toprightx, bottomrightx, toprighty, bottomrighty have been declared as integers further up in the code.) (请注意,toprightx、bottomrightx、toprighty、bottomrighty 已在代码中进一步声明为整数。)

Note that toprightx, bottomrightx, toprighty, bottomrighty have been declared as integers further up in the code.请注意,toprightx、bottomrightx、toprighty、bottomrighty 已在代码中进一步声明为整数。

There's your answer.这就是你的答案。 Calculations that involve only integers are performed in integer math, including divisions.仅涉及整数的计算在 integer 数学中执行,包括除法。 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.要解决此问题,请在计算中将至少一个 x/y 值声明为浮点数或将其转换为浮点数。

You are performing integer division on this line:您正在这条线上执行 integer 除法:

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

Since topright, bottomrighty, toprightx, and bottomrightx are all integers, the result of that equation will also be an integer.由于 topright、bottomrighty、toprightx 和 bottomrightx 都是整数,因此该等式的结果也将是 integer。 After the equaition calculates an integer you are assigning it to a float.等式计算出 integer 后,您将其分配给浮点数。 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:这是给你的 ah int

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 .所有这些操作都将使用 integer 除法(截断浮点)执行,然后转换为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.那是因为您在计算中仅使用 int,因此 C++ 对它们使用 integer 计算。 Just cast one of your int variables to float and you'll be good.只需将您的 int 变量之一转换为 float 即可。

Changing this statement m = (-toprighty + bottomrighty) / (toprightx - bottomrightx);更改此语句m = (-toprighty + bottomrighty) / (toprightx - bottomrightx); to m = (-toprighty + bottomrighty) / (float)(toprightx - bottomrightx);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.在要求混合算术之前将 toprightx、bottomrightx、toprighty、bottomrighty 声明为浮点数或将它们转换为浮点数。

Casting(implicitly, as you're doing) a float to an int will truncate the data that won't fit in the new type.将浮点数转换为 int (隐含地,就像你正在做的那样)将截断不适合新类型的数据。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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