Why is m always = 0? The x and y members of someClass are integers.
float getSlope(someClass a, someClass b)
{
float m = (a.y - b.y) / (a.x - b.x);
cout << " m = " << m << "\n";
return m;
}
Why is m always = 0? The x and y members of someClass are integers.
float getSlope(someClass a, someClass b)
{
float m = (a.y - b.y) / (a.x - b.x);
cout << " m = " << m << "\n";
return m;
}
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.
You'll want to cast the expressions to floats first before dividing, e.g.
float m = static_cast<float>(a.y - b.y) / static_cast<float>(a.x - b.x);
inf
. But yes, if it's unexpected, it will cause problems. –
Capitulate +INF
, -INF
or NaN
which will probably cause the OP further problems when he tries to use m
. –
Pacifistic (static_cast<float>(a.y)-b.y)/(a.x-b.x)
–
Verism int
to float
, which typically needs to form a rounded float
for large int
values. –
Damnatory static_cast
or perfect forwarding on its arguments before calling back to... whatever it is you were doing. –
Hollander 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
:
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.
You'll want to cast the expressions to floats first before dividing, e.g.
float m = static_cast<float>(a.y - b.y) / static_cast<float>(a.x - b.x);
inf
. But yes, if it's unexpected, it will cause problems. –
Capitulate +INF
, -INF
or NaN
which will probably cause the OP further problems when he tries to use m
. –
Pacifistic (static_cast<float>(a.y)-b.y)/(a.x-b.x)
–
Verism int
to float
, which typically needs to form a rounded float
for large int
values. –
Damnatory static_cast
or perfect forwarding on its arguments before calling back to... whatever it is you were doing. –
Hollander you can cast both numerator and denominator by multiplying with (1.0) .
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. 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(a.y - b.y) / (a.x - b.x);
float m = (float(a.y) - b.y) / (a.x - b.x);
...etc...
(float(a.y) - b.y) / (a.x - b.x)
is strange as it does a float
subtraction for .y
, yet an int
subtraction for .x
. –
Damnatory 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.
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);
he does an integer divide, which means 3 / 4 = 0. cast one of the brackets to float
(float)(a.y - b.y) / (a.x - b.x);
You can use ios manipulators fixed(). It will allow you to print floating point values.
if (a.y - b.y) is less than (a.x - b.x), m
is always zero.
so cast it like this.
float m = ((float)(a.y - b.y)) / ((float)(a.x - b.x));
© 2022 - 2024 — McMap. All rights reserved.
(float)
is noC++
it isC
style. – Sphalerite