Difference between revisions of "CSC231 Final Exam 2015"
Line 53: | Line 53: | ||
You may assume that the stack is at most 6 MBytes in length. | You may assume that the stack is at most 6 MBytes in length. | ||
− | = | + | Submit a pdf with your answers in the Final PB 1 Section on Moodle. Make sure you write your name at the top of the file! |
− | + | <br /> | |
+ | =Problem #2= | ||
+ | <br /> | ||
+ | Python can handle very larger integer numbers, much larger than the integers Java can handle. | ||
− | + | With real numbers, however, Python sometimes behaves in unexpected ways. To see an example of this, get a copy of the following Python program: [[CSC231 sum_of_floats.py | sum_of_floats.py]], and study it. It creates a list of 10 random floating-point numbers, each one the result of picking a random real number between 0 and 1.0, which is then multiplied by 10 raised to some other random integer in the range -40 to +40. | |
− | + | The program then computes the sum of the ten numbers as stored in the list. That's sum1. Next it sorts the list in increasing order and computes the sum again, this time saving it into sum 2. Finally, it reverses the list so that it is sorted in decreasing order, and computes the sum of the numbers which it stores in sum3. It then compares sum1, sum2, and sum3, and displays the list and the sums if these are not equal. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | Before answering the questions below, you may want to read this [https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/BinMath/addFloat.html interesting page] explaining how floating-point numbers are added inside an FPU. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
'''Question 1''': Explain why the 3 sums are usually the same, but ''sometimes'' not equal. Base your explication on the 32-bit IEEE Floating-Point format which we saw in class. | '''Question 1''': Explain why the 3 sums are usually the same, but ''sometimes'' not equal. Base your explication on the 32-bit IEEE Floating-Point format which we saw in class. | ||
Line 104: | Line 69: | ||
'''Question 2''': What recommendations would you make when one has to compute the sum of several floating-point numbers? | '''Question 2''': What recommendations would you make when one has to compute the sum of several floating-point numbers? | ||
− | '''Question 3''': What test could you | + | '''Question 3''': What test could you perform (in any language of your choice) when you are given a list of numbers to figure out whether the sum ''might'' not be correct when the addition is performed? |
− | '''Question 4''': Do we have to be worried about incurring the same problem when computing the product of a series of real numbers? | + | '''Question 4''': Do we have to be worried about incurring the same problem when computing the product of a series of real numbers? Why? |
Store your answers in a text file (not a Word doc file, please!) called '''sumfloat.txt''' and submit it as follows: | Store your answers in a text file (not a Word doc file, please!) called '''sumfloat.txt''' and submit it as follows: |