C++ int float casting
Solution 1
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);
Solution 2
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 );
Solution 3
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...
Comments
-
el_pup_le almost 2 years
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; }
-
django11 about 13 years..And watch out for division by zero!
-
Kiril Kirov about 13 years@GrahamS - It's not that dangerous to divide by 0, when we're talking about floating point numbers. You could safely divide any float number by 0.0 or -0.0, will give you
inf
. But yes, if it's unexpected, it will cause problems. -
django11 about 13 years@Kiril Kirov: Yeah it won't cause an exception/crash like integer division-by-zero does, but it leaves you with either
+INF
,-INF
orNaN
which will probably cause the OP further problems when he tries to usem
. -
juanchopanza about 13 years@BoltClock isn't it enough to cast any one of the elements of the expression to float? e.g.
(static_cast<float>(a.y)-b.y)/(a.x-b.x)
-
notlesh over 6 yearsI don't necessarily disagree, but counterpoints: (1) this is far less readable, and (2) less portable
-
Kiril Kirov over 6 years@notlesh - "less portable" in what way?
-
notlesh over 6 yearsNamely, it couldn't be ported directly to C... again, just a counterpoint :)
-
Kiril Kirov over 6 years@notlesh - C is completely different language. I don't qualify this as "portability issue", but I get your point
-
Anubis over 6 years+1 this should be the correct answer as the OP asked for C++. stroustrup.com/bs_faq2.html#static-cast
-
S.S. Anne over 4 yearsYou should use a C++-style cast instead of a C-style cast in C++.
-
chux - Reinstate Monica over 3 years@juanchopanza cast is useful to quiet a warning about the loss of precision with
int
tofloat
, which typically needs to form a roundedfloat
for largeint
values. -
chux - Reinstate Monica over 3 years
(float(a.y) - b.y) / (a.x - b.x)
is strange as it does afloat
subtraction for.y
, yet anint
subtraction for.x
. -
AAEM over 3 yearstry to reEdit your answer by highlighting the code
-
cmarangu about 3 years@BoltClock thanks for writing such a concise answer!
-
BoltClock about 3 years@S.S. Anne: I finally fixed my answer since it's stayed accepted all these years. Little over 10 years and a month... better late than never, right? (I think I wasn't very active at the time you commented so I may have missed it.)
-
Michael Rovinsky almost 3 yearsPlease add some code to illustrate your answer
-
0xB00B almost 3 yearsI have a 30000 characters long Equation in my code, I can't be manually casting each term, is there a way to cast all ints to float in a that Equation ? (except using some python script to fix it, I already did that, but I am hoping for a proper way ?)