• Advertisement

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by icecubeflower
Then 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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement