• Post Reply Bookmark Topic Watch Topic
  • New Topic

Floating Point Precision  RSS feed

 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider this small program and output.


The output is 1.0499999999999998. Why not 1.05?

And thanks!
 
Mark King
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because doubles only have 16 precise digits. Here the double lost precision in the 17th digit and it affected all the 0s before it.
This is because the 64 bits alloted to a double don't suffice for more than 16 precise digits.
If you need more precision you can use BigDecimal, if you don't need more precision, it is wise to round the number to keep the 17th digit from affecting the first 16.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at these FAQ (No 20). You actually have 53 digits's precision in a double, but they are binary digits, so most decimal numbers cannot be represented exactly because they recur indefinitely. Obviously the difference is closer to 1.0499999999999998 than to 1.0499999999999999, so that is what the output is.
A BigDecimal object will give you exactly the 2 digits' precision you want, and they will be decimal digits. Examples in this thread.
System.out.println(new BigDecimal("2.80").subtract(new BigDecimal("1.75")));
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You get 53 binary digits' precision in a double whose absolute value is greater than or equal to Double.MIN_NORMAL (value here). As the values get closer to 0 than that, the number of bits decreases and the imprecision increases.
53 × log2 ≅ 15.95 which is your maximum of 16 decimal digits.
 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey look at that, makes sense. Thanks, Ranchers.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!