=========================================== Assignment #02 - range/precision errors in simple numerical calculations =========================================== - Ian! D. Allen - idallen@idallen.ca - www.idallen.com Available online: Wednesday September 14, 2011 Deliverables: In-lab demo during Week 2. Show me your results in the lab. There is nothing to hand in. Read the whole lab specification before you begin. Read *all* the words. 0. Download and compile/run the FunnyMath0.java program from the Class Notes. The program produces very wrong answers for very simple arithmetic. If you don't know what "1.0E7" means, read "E notation" in this page: http://en.wikipedia.org/wiki/Scientific_notation Make a table (online or on paper) to record the FunnyMath0 program output for the changes you make below: 1. Run and record the above program outputs with no changes. Can you explain what causes each wrong output? What is the reason? Do not change the program. Observe the results. Why? 2. Modify the floating-point sections of the program to add 2 instead of adding 1. Do not make any other changes to the program. Run and Record the new program output. How has the output changed, when adding two? Can you explain the new floating-point outputs? 3. Modify the floating-point sections of the program to add 3 instead of adding 1. Do not make any other changes to the program. Run and Record the new program output. How has the output changed, when adding three? Can you explain the new floating-point outputs? 4. Repeat the above modifications and try adding 4, 5, 6, 7, 8, and 9. Record in your table the outputs for each modification. Do not make any other changes to the program. Can you explain the new floating-point outputs? 5. Call over your instructor. Show the table of outputs for adding 1 through 9. Try to explain what is happening to produce these wrong answers. (Don't worry - it will be explained in class.) 6. On a piece of paper, rewrite 1,234,000,000 using "E" notation with one digit to the left of the decimal point: 1._______________E______ Rewrite 0.000102030400 the same way: 1._______________E______ Show your instructor. 7. True or False: Computers give exact answers to all mathematical problems. 8. Optional: Instead of modifying the program nine times, modify the program once to include two FOR loops that iterate the floating-point sections to add each of 1 through 9 so that the program loops to produce each of the nine outputs one after the other, for both single- and double-precision floating point. (This is faster than modifying the program multiple times and running it multiple times and writing the output down on paper!) The output of the looping program serves as your table; you don't need to write any output on paper. Your output will have this format when you use two FOR loops: ------------------------------------------- Fun with single-precision floating-point 1.0E7 plus 1 is [...] 2.0E7 plus 1 is [...] 1.0E7 plus 2 is [...] 2.0E7 plus 2 is [...] 1.0E7 plus 3 is [...] 2.0E7 plus 3 is [...] 1.0E7 plus 4 is [...] 2.0E7 plus 4 is [...] 1.0E7 plus 5 is [...] 2.0E7 plus 5 is [...] 1.0E7 plus 6 is [...] 2.0E7 plus 6 is [...] 1.0E7 plus 7 is [...] 2.0E7 plus 7 is [...] 1.0E7 plus 8 is [...] 2.0E7 plus 8 is [...] 1.0E7 plus 9 is [...] 2.0E7 plus 9 is [...] ------------------------------------------- Fun with double-precision floating-point 1.0E15 plus 1 is [...] 1.0E16 plus 1 is [...] 1.0E15 plus 2 is [...] 1.0E16 plus 2 is [...] 1.0E15 plus 3 is [...] 1.0E16 plus 3 is [...] 1.0E15 plus 4 is [...] 1.0E16 plus 4 is [...] 1.0E15 plus 5 is [...] 1.0E16 plus 5 is [...] 1.0E15 plus 6 is [...] 1.0E16 plus 6 is [...] 1.0E15 plus 7 is [...] 1.0E16 plus 7 is [...] 1.0E15 plus 8 is [...] 1.0E16 plus 8 is [...] 1.0E15 plus 9 is [...] 1.0E16 plus 9 is [...] This lab is an in-lab demo. There is nothing to hand in. Show me. -- | Ian! D. Allen - idallen@idallen.ca - Ottawa, Ontario, Canada | Home Page: http://idallen.com/ Contact Improv: http://contactimprov.ca/ | College professor (Free/Libre GNU+Linux) at: http://teaching.idallen.com/ | Defend digital freedom: http://eff.org/ and have fun: http://fools.ca/