Link

Puzzle (Time for a change)

Consider the following example.

package demo;

public class App {
  public static void main( String[] args ) {
    System.out.println( 2.00 - 1.10 );
  }
}

What do you think it will print?

0.8999999999999999

This example was taken from PUZZLE 2: TIME FOR A CHANGE in Java™ Puzzlers: Traps, Pitfalls, and Corner Cases.

The problem is that the number 1.1 can’t be represented exactly as a double, so it is represented by the closest double value. The program subtracts this value from 2. Unfortunately, the result of this calculation is not the closest double value to 0.9. The shortest representation of the resulting double value is the hideous number that you see printed.

Binary floating-point is particularly ill-suited to monetary calculations, as it is impossible to represent 0.1—or any other negative power of 10—exactly as a finite-length binary fraction (Effective Java - Item 60: Avoid float and double if exact answers are required).