Mark King

Ranch Hand

Posts: 55

posted 2 years ago

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.

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

posted 2 years ago

- 1

Look at these FAQ (No 20). You actually have 53 digits's precision in a

A BigDecimal object will give you exactly the 2 digits' precision you want, and they will be decimal digits. Examples in this thread.

`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

posted 2 years ago

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 ×

53 ×

*log*2 ≅ 15.95 which is your maximum of 16 decimal digits.