Float and Double: 1/3 + 2/3 = 0.99999999….

31 07 2013

Rob Eamon wrote about why 1/3 + 2/3 = 0.99999999….

Some excerpts:

Roedy Green’s Java Glossary (http://mindprod.com/jglossfloatingpoint.html) discusses floating point in Java at some length. He offers insight on why developers should not be surprised at the inaccuracy of floating point:

[0.1 is a repeater fraction in binary.] It is like the repeater fraction 1/3 = 0.33333 in base 10. When you add 0.333333… to 0.666666… why are you not astonished to get 0.999999… rather than 1.0, even though you just added 1/3 + 2/3 to get 1? Yet, with Java floating point you are astonished when you add 0.1 + 0.1 and get something other than 0.2.

It is clear that when using float and double types in Java, some care must be taken to ensure proper behavior for the task at hand. For financial applications, where accuracy is paramount, float and double may need to be avoided altogether.

Here’s the full article :

http://www.wmusers.com/ezine/2002oct1_reamon_1.shtml

Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: