2.4 Floating-point errors
Learning objectives
By the end of this section you should be able to
- Explain numerical inaccuracies related to floating-point representation.
- Use the
roundfunction to mitigate floating-point errors in output.
Floating-point errors
Computers store information using 0's and 1's. All information must be converted to a string of 0's and 1's. Ex: 5 is converted to 101. Since only two values, 0 or 1, are allowed the format is called binary.
Floating-point values are stored as binary by Python. The conversion of a floating point number to the underlying binary results in specific types of floating-point errors.
A round-off error occurs when floating-point values are stored erroneously as an approximation. The difference between an approximation of a value used in computation and the correct (true) value is called a round-off error.
Ex: Storing the float (0.1)10 results in binary values that actually produce (0.1000000000000000055511151231257827021181583404541015625)10 when converted back, which is not exactly equal to (0.1)10.
# Print floats with 30 decimal places
print(f'{0.1:.30f}') # prints 0.1
print(f'{0.2:.30f}') # prints 0.2
print(f'{0.4:.30f}') # prints 0.4
|
0.100000000000000005551115123126
0.200000000000000011102230246252
0.400000000000000022204460492503
|
An overflow error occurs when a value is too large to be stored. The maximum and minimum floating-point values that can be represented are and , respectively. Attempting to store a floating-point value outside the range leads to an overflow error.
Below, and can be represented, but is too large and causes an overflow error.
print('3.0 to the power of 256 =', 3.0**256)
print('3.0 to the power of 512 = ', 3.0**512)
print('3.0 to the power of 1024 = ', 3.0**1024)
|
3.0 to the power of 256 = 1.3900845237714473e+122
3.0 to the power of 512 = 1.9323349832288915e+244
3.0 to the power of 1024 =
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
print('3.0 to the power of 1024 = ', 3.0**1024)
OverflowError: (34, 'Numerical result out of range')
|
Floating point round function
Python's round function is used to round a floating-point number to a given number of decimal places. The function requires two arguments. The first argument is the number to be rounded. The second argument decides the number of decimal places to which the number is rounded. If the second argument is not provided, the number will be rounded to the closest integer. The round function can be used to mitigate floating-point errors.
Ex:
round(2.451, 2) = 2.45round(2.451) = 2