aspose file tools*
The moose likes Beginning Java and the fly likes floating point and double accuracy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "floating point and double accuracy" Watch "floating point and double accuracy" New topic
Author

floating point and double accuracy

mat buckland
Greenhorn

Joined: Sep 01, 2009
Posts: 24
Hi,

I'm curious to know why my flaoting point calculations in Java give incorrect results. For example, doing this:


gives the result:

0.1
0.2
0.30000000000000004
0.4
0.5
0.6
0.7
0.7999999999999999
0.8999999999999999
0.9999999999999999

Would someone be kind enough to explain what's going on here and what i can do to correct it?

Many thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40041
    
  28
Those results are correct and accurate. There is however always an imprecision with all floating-point arithmetic. Adding 0.1 is notorious for imprecision.

We have an FAQ (look for no 20) which gives a nice explanation.
mat buckland
Greenhorn

Joined: Sep 01, 2009
Posts: 24
cheers
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40041
    
  28
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: floating point and double accuracy