• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

floating point and double accuracy

 
mat buckland
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cheers
 
Campbell Ritchie
Sheriff
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic