EXERCISES concerning computer numbers ITU KCS Introductory Programming 2019-09-23 * sestoft@itu.dk 1. Find Java types for the variables x, y, z, v, w, s and t, and values for them, such that: It holds that x+1 < x It holds that y != y It holds that z+1 == z It holds that v+w-w != v It holds that s==t && 1.0/s != 1.0/t 2. Write a Java program to check that your solutions are correct. 3. Find the binary representation of +3 and -3 as 8-bit byte, 32-bit int and 64-bit long of +128 and -127 as byte, int and long of 0.0625 and 0.625 and 62.5 and 625 as float of 0.01 as a 64-bit double 4. Check the results using displayInt etc from file Numbers.java. This file can be found at the address http://www.itu.dk/people/sestoft/bachelor/Numbers.java 5. Compute the following in a Java program: 1.0/Double.POSITIVE_INFINITY and Math.exp(Double.NEGATIVE_INFINITY) and 1.0/(1.0+1.0/0.0) and Math.pow(0.0, -1.0) 6. Write a Java program to compute the sum of the first N reciprocals both forwards, as in 1.0/1.0 + 1.0/2.0 + 1.0/3.0 + 1.0/4.0 + ... + 1.0/N and backwards, as in 1.0/N + 1.0/(N-1) + ... + 1.0/3.0 + 1.0/2.0 + 1.0/1.0 Use float (32-bit floating-point numbers) to represent the sum, and try with N=1000000, N=10000000 and N=100000000, that is, 1 million, 10 million and 100 million. In mathematics, the results of the two orders of summation would be the same, of course. What do you observe? Explain the results. Which result is more likely to be correct? 7. A naive programmer tries to compute the sum of the first N=count reciprocals of squares 1.0/(N*N) + 1.0/((N-1)*(N-1)) + ... + 1.0/9.0 + 1.0/4.0 + 1.0/1.0 using this Java program: int count = 100000; double sum = 0.0; for (int i=count; i>=1; i--) sum += 1.0/(i*i); System.out.println(sum); Disappointingly, this Java program prints Infinity rather than the expected result, which is roughly (pi^2)/6 = 1.6449. Explain why the result is so wrong. Hint: Integer computations are to blame, not floating-point. 8. Change the above Java program so that it computes the correct result.