Skip Navigation

Floating-Point Assignment

For this assignment, you will write a Python program that demonstrates the effects of cumulative errors in floating-point operation.

Page Contents

Background Material

The Python programming language (which is covered in CSCI 135 and 145) supports floating-point numbers that follow the IEEE 754 standard. Most Python interpreters implement these numbers using double precision.

One consequence of converting fractions of 10 into binary is that the result repeats forever. A fraction of 10 is still a rational number in binary, since rational numbers are those which either repeat or terminate. Consider the value 1 / 10 or 0.1 in decimal. In binary, this value is:

0.000110011001100110011001100110011001100110011…

… and it goes on forever, repeating the pattern “0011.”

Now recall that IEEE 754 double precision specifies storing this number using only 64 bits, where 1 bit is reserved for the sign and 11 bits are reserved for the biased exponent. The remaining 52 bits are used to store the trailing significand, which is the part of the number that actually stores the significant figures in the original value.

Can you see a problem with trying to store an infinitely long repeating sequence of binary digits in a fixed amount of space?

Requirements

Write a Python program that sets a variable to the value 0.1. Create a loop that runs at least 10,000 times, adding 0.01 to the value and printing it each time.

Prepare a video presentation that contains the following:

  1. Show your Python code, and explain how it works.
  2. Run your Python program, and show at least the last 8 to 10 lines of output.
  3. Is there any mathematical way that repeatedly adding 0.01 to 0.1 can give you the values that you’re seeing? If you’re unsure, do a few additions by hand.
  4. Explain why your Python program is giving the results that you see.
  5. Identify at least one real-world application where we routinely use fractions of 10 together with computers. What could possibly go wrong with IEEE 754 numbers in this application?

Grading

Please review the Floating-Point Rubric before submitting this assignment to Moodle.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.