# floats

This topic is 3826 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey how come if I do this:
#define RATDAMAGE 0.1f

int main()
{
float fnum=RATDAMAGE;

return 0;
}


Then fnum==0.100000001? Why doesn't it equal 0.1?

##### Share on other sites
The short answer is that floating point numbers are inexact.

The longer answer is that the binary format of the floating point number is incapable of representing 0.1 exactly.

The longest answer would point you to IEEE 754

##### Share on other sites
0.1 is not representable in floating point.

To rationalize this for yourself think of how real numbers are represented in base 10. Then expand that reasoning to base 2. Once you do that try and write out 0.1 exactly. You'll find you can't do it.

For a complete overview:

What Every Computer Scientist Should Know About Floating-Point Arithmetic

[Edited by - fpsgamer on July 25, 2008 10:07:19 PM]

##### Share on other sites
Read the article above, however here's a quick explanation:
Everyone knows that writing out 1/3 requires an infinite number of 3's right?! Computers don't have an infinite amount of memory, so you're out of luck there! Of course someone who did everything in base 3 would say that it simply equals 0.1 in base 3, and laugh at your inferrior decimal system!

This can go the other way as well. 1/10, or 0.1 in decimal is easy to write, but now change to base 3. Uh oh now we get an infinite number of digits again. Who's laughing now!

The lesson here is that floats are just an approximation. Floats have a fixed base (usually 2) and that will always mean that some numbers that look simple in base 10, would take an infinite number of digits to represent.

Of course there's more to it than that though.

##### Share on other sites
Quote:
 Original post by icecubeflowerThen fnum==0.100000001?

That's just a rounded representation of the actual value. Try this:
#include <iostream>#include <iomanip>int main(){    std::cout << std::fixed << std::setprecision(30) << 0.1f << std::endl;    std::cout << std::fixed << std::setprecision(60) << 0.1 << std::endl;}

0.1f is really 0.100000001490116119384765625 and
0.1 is really 0.1000000000000000055511151231257827021181583404541015625

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 14
• 46
• 22
• 27
• ### Forum Statistics

• Total Topics
634049
• Total Posts
3015239
×