rounding error
A rounding error is the difference between the exact result a calculation would produce with unlimited precision and the value left after rounding each number to fit a finite-precision format, such as a float.
The error has two sources:
- Representation error appears because most real numbers have no exact finite form in a given base. For example, the decimal 0.1 becomes an infinitely repeating binary fraction, so the stored value is only the nearest representable approximation.
- Arithmetic error appears when an operation’s true result must itself be rounded back into the format, even when both inputs were exact. For example, adding
1.0to2.0 ** 53leaves the value unchanged, because the exact result lands between two representable floats and rounds back to2.0 ** 53.
The relative error of a single rounding step is bounded by the format’s machine epsilon, the gap between 1 and the next representable value.
Rounding errors matter most when they compound. For example, over a long sequence of operations they can accumulate until they dominate the result. Subtracting two nearly equal numbers can also magnify them sharply, an effect called catastrophic cancellation.
Rounding error is distinct from truncation error, which comes from approximating a continuous process with finitely many steps, not from finite precision.
Related Resources
Tutorial
How to Round Numbers in Python
Learn how Python's round() works, why it rounds 2.5 to 2, and how you can manage rounding errors with decimal, NumPy, and pandas.
For additional information on related topics, take a look at the following resources:
- Rounding Numbers in Python (Course)
- Basic Data Types in Python: A Quick Exploration (Tutorial)
- Exploring Basic Data Types in Python (Course)
- The Python math Module: Everything You Need to Know (Tutorial)
- Numbers in Python (Tutorial)
- Representing Rational Numbers With Python Fractions (Tutorial)
- Rounding Numbers in Python (Quiz)
- Basic Data Types in Python: A Quick Exploration (Quiz)
- Exploring the Python math Module (Course)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.