Sign in to follow this  

Recommended Posts

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this