/** OverflowRoundoff.java - The weird things that happen because
		the computer has finite precision. We see first
		overflow errors. We see that by adding 1000000000
		to x we get all sorts of weird values
			x = 1000000000
			x = 2000000000
			x = -1294967296
			x = -294967296
			x = 705032704
			x = 1705032704
			x = -1589934592
			x = -589934592
			x = 410065408
			x = 1410065408
		Then we see the effect of roundoff errors.
		We add to y ten thousand 0.1s and instead
		of getting 1000.0 we get
			y = 999.9029
		If instead of float we used double we would jave
		gotten a smaller error:
			z = 1000.0000000001588
		Notice that when using float we had to cast the
		real constants.
  */

import java.io.*;

public class OverflowRoundoff {
    public static void main(String[] args) {

	// Here we see integer overflow problem
	int x = 0;
	for (int k = 1; k <= 10; k++) {
	    x += 1000000000;
	    System.out.println("x = " + x);
	}
	
	// Here we see roundoff problem for reals
	float y = (float)0.0;
	for (int k = 1; k <= 10000; k++) 
	    y += (float)0.1;
	System.out.println("y = " + y);
	double z = 0.0;
	for (int k = 1; k <= 10000; k++) 
	    z += 0.1;
	System.out.println("z = " + z);
    }
}
