C++ int 浮点数转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5456801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:12:57  来源:igfitidea点击:

C++ int float casting

c++castingfloating-point

提问by el_pup_le

Why is m always = 0? The x and y members of someClass are integers.

为什么 m 总是 = 0?someClass 的 x 和 y 成员是整数。

float getSlope(someClass a, someClass b)
{           
    float m = (a.y - b.y) / (a.x - b.x);
    cout << " m = " << m << "\n";
    return m;
}

回答by Kiril Kirov

You need to use cast. I see the other answers, and they will really work, but as the tag is C++I'd suggest you to use static_cast:

您需要使用演员表。我看到了其他答案,它们确实有效,但由于标签是C++我建议您使用static_cast

float m = static_cast< float >( a.y - b.y ) / static_cast< float >( a.x - b.x );

回答by BoltClock

Integer division occurs, then the result, which is an integer, is assigned as a float. If the result is less than 1 then it ends up as 0.

发生整数除法,然后将结果作为整数分配为浮点数。如果结果小于 1,则结果为 0。

You'll want to cast the expressions to floats first before dividing, e.g.

在划分之前,您需要先将表达式转换为浮点数,例如

float m = (float)(a.y - b.y) / (float)(a.x - b.x);

回答by Tony Delroy

You should be aware that in evaluating an expression containing integers, the temporary results from each stage of evaluation are also rounded to be integers. In your assignment to float m, the value is only converted to the real-number capable floattype after the integer arithmetic. This means that, for example, 3 / 4 would already be a "0" value before becoming 0.0. You need to force the conversion to float to happen earlier. You can do this by using the syntax float(value)on any of a.y, b.y, a.x, b.x, a.y - b.y, or a.x - b.x: it doesn't matter when it's done as long as one of the terms is a float before the division happens, e.g.

您应该知道,在计算包含整数的表达式时,每个计算阶段的临时结果也会四舍五入为整数。在您对 的赋值中float m,该值仅float在整数算术运算后转换为实数类型。这意味着,例如,3 / 4 在变为 0.0 之前已经是“0”值。您需要强制转换为浮动更早发生。您可以通过float(value)a.y, b.y, a.x, b.x, a.y - b.y, or 中的任何一个上使用语法来做到这a.x - b.x一点:只要在除法发生之前其中一个术语是浮点数,什么时候完成并不重要,例如

float m = float(a.y - b.y) / (a.x - b.x); 
float m = (float(a.y) - b.y) / (a.x - b.x); 
...etc...

回答by Vineet G

You are performing calculations on integers and assigning its result to float. So compiler is implicitly converting your integer result into float

您正在对整数执行计算并将其结果分配给浮点数。所以编译器隐式地将您的整数结果转换为浮点数

回答by Dumitrescu Bogdan

Because (a.y - b.y) is probably less then (a.x - b.x) and in your code the casting is done after the divide operation so the result is an integer so 0.

因为 (ay - by) 可能小于 (ax - bx) 并且在您的代码中,转换是在除法运算之后完成的,因此结果是一个整数,因此为 0。

You should cast to float before the / operation

您应该在 / 操作之前强制转换为浮动

回答by Ronny Brendel

he does an integer divide, which means 3 / 4 = 0. cast one of the brackets to float

他做了一个整数除法,这意味着 3 / 4 = 0。将其中一个括号转换为浮动

 (float)(a.y - b.y) / (a.x - b.x);

回答by Prince John Wesley

if (a.y - b.y) is less than (a.x - b.x), mis always zero.

如果 (ay - by) 小于 (ax - bx),m则始终为零。

so cast it like this.

所以就这样投吧。

float m = ((float)(a.y - b.y)) / ((float)(a.x - b.x));