Floating point issues.
by Stefan Lundmark · in Torque Game Engine · 02/15/2007 (3:24 pm) · 4 replies
This one has got me stuck.
The above results in 1.50000 being printed, which is obvious.
And this results in 1.00000 being printed, which is not obvious at all. It should be the same as before (48 / 32 is 1.5). The same goes for (96 / 0.73) which should be 70.08, but I get 70.00000 returned. So basically, what I get back is integers and no decimals.
Whats'up with that? What am I doing wrong?
mMadness = 1.5;
Con::printf ("Madness: %f", mMadness);The above results in 1.50000 being printed, which is obvious.
mMadness = 48 / 32;
Con::printf ("Madness: %f", mMadness);And this results in 1.00000 being printed, which is not obvious at all. It should be the same as before (48 / 32 is 1.5). The same goes for (96 / 0.73) which should be 70.08, but I get 70.00000 returned. So basically, what I get back is integers and no decimals.
Whats'up with that? What am I doing wrong?
About the author
#2
The above will work with integer variables too instead of integer constants.
02/15/2007 (7:07 pm)
You can also for the result this way:(F32)(48/32)
The above will work with integer variables too instead of integer constants.
#3
Check out the following: en.wikibooks.org/wiki/C++_Programming/Type_Casting
02/15/2007 (7:31 pm)
(F32)(48/32) will still give you 1.Check out the following: en.wikibooks.org/wiki/C++_Programming/Type_Casting
#4
Adding an .0f did the trick!
Thanks guys, I can't believe I've missed that.
02/16/2007 (12:08 am)
I didn't know this was related to type casting. Adding an .0f did the trick!
Thanks guys, I can't believe I've missed that.
Associate Orion Elenzil
Real Life Plus
you want "48.0f" and "32.0f".
you can leave off the "f", but they you'll have doubles instead of floats.