/** Demo program that illustrates the loss of significant numbers
 *  (in other words accuracy) that can occur due to multiple
 *  mathematical operations using doubles.
 *
 *  @author TK Rogers
 *  @version 10-27-10 */
 
public class RoundingErrorDemo {
    public static void main ( ) {
        double x = 0.123456789, z = 123456789.0;
        double x356 = 1.0, x4 = 1.0, x360 = 1.0;
        double result1, result2, result3;
        int y;
      
        for ( y = 0; y < 4; y++ ) {
            x4 *= x;
        }
        for ( y = 0; y < 356; y++ ) {
            x356 *= x;
        }
        for ( y = 0; y < 360; y++ ) {
            x360 *= x;
        }
       
        result1 = z * x4 *x356;
        result2 = x4 * x356 * z;
        result3 = z * x360 ;
        System.out.println( " (0.123456789)^4    = " + x4);
        System.out.println( " (0.123456789)^356 = " + x356);
        System.out.println( " (0.123456789)^360 = " + x360);
        System.out.println( "\nMathematically the following results should all \n" +
                                      "be equal but they're not due to rounding errors.");
        System.out.println( " result 1 = " + result1);
        System.out.println( " result 2 = " + result2);
        System.out.println( " result 3 = " + result3);
    }
}