Difference between revisions of "CSC231 Homework 8 2014"
(Created page with "--~~~~ ---- <onlydft> =Problem #1= <br /> Python seems to be more "clever" about dealing with variables of different types than other languages. For example, if you write a...") |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 10:38, 4 December 2014 (EST) | --[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 10:38, 4 December 2014 (EST) | ||
---- | ---- | ||
− | < | + | <br /> |
+ | <bluebox> | ||
+ | This assignment is due on 12/12/14 @ 11:55 p.m. You will need to submit a file with answers to the questions below. Regular text file (with a .txt extension), or a pdf file (with a .pdf extension) only, please! | ||
+ | </bluebox> | ||
+ | <br /> | ||
=Problem #1= | =Problem #1= | ||
Line 10: | Line 14: | ||
Sometimes, however, Python seems to behave in unexpected ways. To see an example of this, get 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. | Sometimes, however, Python seems to behave in unexpected ways. To see an example of this, get 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 sum2. 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 | + | 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 sum2. 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 and displays the list, and the three sums. Observe the least significant digits of the 3 sums for differences. |
'''Question 1''': Explain why the 3 sums are usually the same, but ''sometimes'' not equal. Hints: the 32-bit IEEE Floating-Point format which we saw in class has a lot to do with it. | '''Question 1''': Explain why the 3 sums are usually the same, but ''sometimes'' not equal. Hints: the 32-bit IEEE Floating-Point format which we saw in class has a lot to do with it. | ||
Line 33: | Line 37: | ||
− | |||
<br /> | <br /> | ||
<br /> | <br /> |
Latest revision as of 10:52, 4 December 2014
--D. Thiebaut (talk) 10:38, 4 December 2014 (EST)
This assignment is due on 12/12/14 @ 11:55 p.m. You will need to submit a file with answers to the questions below. Regular text file (with a .txt extension), or a pdf file (with a .pdf extension) only, please!
Problem #1
Python seems to be more "clever" about dealing with variables of different types than other languages. For example, if you write a Python program to compute the terms of the Fibonacci series, it will keep on expanding the storage allocated to each variable to allow the growing number of digits required to represent each new term.
Sometimes, however, Python seems to behave in unexpected ways. To see an example of this, get the following Python program: 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 sum2. 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 and displays the list, and the three sums. Observe the least significant digits of the 3 sums for differences.
Question 1: Explain why the 3 sums are usually the same, but sometimes not equal. Hints: the 32-bit IEEE Floating-Point format which we saw in class has a lot to do with it.
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 use when you are given a list of numbers to figure out whether the sum might not be mathematically 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?
Submission
Submit your answers to each question, clearly marked Question 1, Question 2, etc. in a text, or a pdf file (no word file please!) on Moodle, in the Homework 8 Problem 1 Section.