Introduction to Python Programming

# 2.4Floating-point errors

Introduction to Python Programming2.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 round() function 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 
Table 2.1 Round-off error. (The example above shows a formatted string or f-string, which are introduced in the Objects chapter.)

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 $1.8×103081.8×10308$ and $−1.8×10308−1.8×10308$, respectively. Attempting to store a floating-point value outside the range $(−1.8×10308,1.8×10308)(−1.8×10308,1.8×10308)$ leads to an overflow error.

Below, $3.02563.0256$ and $3.05123.0512$ can be represented, but $3.010243.01024$ 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 "", line 3, in     print('3.0 to the power of 1024 = ', 3.0**1024) OverflowError: (34, 'Numerical result out of range') 
Table 2.2 Overflow error.

## Concepts in Practice

### Floating-point errors

For each situation, which error occurs?

1.
The statement result = 2.0 * (10.0 ** 500) assigns the variable result with too large of a value.
1. round-off
2. overflow
2.
0.123456789012345678901234567890 * 0.1 produces 0.012345678901234568430878013601.
1. round-off
2. overflow

## 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.45
• round(2.451) = 2

## Concepts in Practice

### Examples of round() function

3.
What is the output of round(6.6)?
1. 6
2. 6.6
3. 7
4.
What is the output of round(3.5, 2)?
1. 3
2. 3.5
3. 3.50
5.
What is the output of round(12)?
1. 12.0
2. 12
3. 12.00
6.
What is the output of round(0.1, 1)?
1. 0.1
2. 0.10
3. 0.1000000000000000055511151231257827021181583404541015625

## Try It

### Inaccurate tips

The following code calculates the tip amount, given a bill amount and the tip ratio. Experiment with the following bill amounts and tip ratios and see if any inaccuracies may result in calculating the tip amount.

• bill amount: 22.70 and 33.33
• tip ratio: 0.15, 0.18, and 0.20

## Try It

### Area of a triangle

Complete the following steps to calculate a triangle's area, and print the result of each step. The area of a triangle is $bh2bh2$, where b is the base and h is the height.

1. Calculate the area of a triangle with base = 7 and height = 3.5.
2. Round the triangle's area to one decimal place.
3. Round the triangle's area to the nearest integer value.

This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.