1 /*
 2  * To change this template, choose Tools | Templates
 3  * and open the template in the editor.
 4  */
 5 
 6 /**
 7  *
 8  * @author Adam J. Conover
 9  */
10 public class FloatingPointErrors {
11 
12     public static void main(String[] args) {
13 
14         // Should be .01
15         System.out.println(.1 * .1);
16 
17         // Should be .21
18         System.out.println(.3 * .7);
19 
20         // Should be .14
21         System.out.println(.2 * .7);
22 
23         // Both should should be 1.0
24         double x = .1 * 10;
25         double y = .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1;
26         System.out.println(x);
27         System.out.println(y);
28 
29         // Are they the same?
30         boolean same1 = (x == y);
31         System.out.println("Equal? : " + same1);
32 
33         boolean same2 = (Math.round(x) == Math.round(y));
34         System.out.println("Equal? : " + same2);
35     }
36 }