Game Development Community

Floating point issues.

by Stefan Lundmark · in Torque Game Engine · 02/15/2007 (3:24 pm) · 4 replies

This one has got me stuck.

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?

#1
02/15/2007 (4:06 pm)
"48" and "32" are integers;
you want "48.0f" and "32.0f".

you can leave off the "f", but they you'll have doubles instead of floats.
#2
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
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
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.