programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Floating Point Precision

Skye Antinozzi
Ranch Hand
Posts: 68
3
Consider this small program and output.

The output is 1.0499999999999998. Why not 1.05?

And thanks!

Mark King
Ranch Hand
Posts: 55
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
• 1
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
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
Hey look at that, makes sense. Thanks, Ranchers.