This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android 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: 38033
    
  22
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: 38033
    
  22
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: floating point and double accuracy
 
Similar Threads
regarding bigdecimal.divide problem
format
Numerical methods using Java, Precision
# opposite of
Precision of floating-point arithmetic