简体   繁体   English

C++ int 浮点数转换

[英]C++ int float casting

Why is m always = 0?为什么 m 总是 = 0? The x and y members of someClass are integers. 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;
}

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

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, eg在划分之前,您需要先将表达式转换为浮点数,例如

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

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 float type after the integer arithmetic.在您对float m的赋值中,该值仅在整数运算之后转换为具有实数能力的float类型。 This means that, for example, 3 / 4 would already be a "0" value before becoming 0.0.这意味着,例如,3 / 4 在变为 0.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 ay , by , ax , bx , ay - by , or ax - bx : it doesn't matter when it's done as long as one of the terms is a float before the division happens, eg您可以通过在aybyaxbxay - byax - bx任何一个上使用语法float(value)来做到这一点:只要其中一个术语是浮点数,什么时候完成并不重要在分裂发生之前,例如

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

Because (ay - by) is probably less then (ax - bx) 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您应该在 / 操作之前强制转换为浮动

You are performing calculations on integers and assigning its result to float.您正在对整数执行计算并将其结果分配给浮点数。 So compiler is implicitly converting your integer result into float所以编译器隐式地将您的整数结果转换为浮点数

When doing integer division, the result will always be a integer unless one or more of the operands are a float.进行整数除法时,结果将始终为整数,除非一个或多个操作数是浮点数。 Just type cast one/both of the operands to a float and the compiler will do the conversion.只需将一个/两个操作数类型转换为浮点数,编译器就会进行转换。 Type casting is used when you want the arithmetic to perform as it should so the result will be the correct data type.当您希望算术按预期执行时使用类型转换,以便结果将是正确的数据类型。

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

您可以通过乘以 (1.0) 来转换分子和分母。

You can use ios manipulators fixed().您可以使用 ios 操纵器 fixed()。 It will allow you to print floating point values.它将允许您打印浮点值。

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

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

if (ay - by) is less than (ax - bx), m is always zero.如果 (ay - by) 小于 (ax - bx),则m始终为零。

so cast it like this.所以就这样投吧。

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

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

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