Round-off Error
Goal
Understand how round-off errors accumulate quantitatively over many operations, learn the trade-off with truncation error, and master reduction techniques.
Prerequisites
- Machine epsilon ($\varepsilon_{\text{mach}}$ and the $\text{fl}(a \circ b) = (a \circ b)(1+\delta)$ formula)
- Significant digits and rounding errors (catastrophic cancellation, absorption)
Table of Contents
1. Context
Round-off error arises when a real number, or the result of an arithmetic operation, is rounded to finite-precision floating-point. Under the default round-to-nearest mode, the relative error of a single rounding is bounded by the unit roundoff $u = \varepsilon_{\text{mach}}/2$ (where $\varepsilon_{\text{mach}}$ is machine epsilon), but over many operations the accumulated error can become non-negligible.
For the definition, generation mechanism, and rounding modes, see the earlier chapters. This chapter focuses on quantitative accumulation analysis and reduction techniques.
Below, in expressions for the order of error, we write $\varepsilon_{\text{mach}}$ simply as $\varepsilon$ (inside $O(\cdot)$ the factor-of-2 difference from $u$ is absorbed into the constant).
2. Accumulation Models
Random Walk Model
Assuming round-off errors are random and independent over $n$ operations, the cumulative error grows as
$$E \sim \sqrt{n} \cdot \varepsilon_{\text{mach}}$$(random walk). In practice, error signs tend to fluctuate somewhat randomly, so many computations exhibit behavior close to this model.
Here $E$ is the relative error of computing the sum $S = \sum_i a_i$ of $n$ numbers, namely the error $|\hat{S} - S|$ divided by $\sum_i |a_i|$. The relative error with respect to $|S|$ can be much larger when positive and negative terms cancel (see avoiding catastrophic cancellation in §3).
Worst Case
When errors always accumulate in the same direction:
$$E \sim n \cdot \varepsilon_{\text{mach}}$$This worst case gives the error bound of naive sequential summation. On real data, however, the error signs fluctuate, so the behavior is usually close to the random walk above; growth proportional to $n$ appears only for data constructed so that all roundings go the same way.
3. Summation Algorithm Comparison
When computing $\displaystyle S = \displaystyle\sum_{i=1}^{n} a_i$ for $n$ floating-point numbers, algorithm choice dramatically affects the accumulated error.
| Algorithm | Error Bound | Extra Memory | Notes |
|---|---|---|---|
| Naive summation | $O(n\varepsilon)$ | $O(1)$ | Simplest. The bound is the worst case $O(n\varepsilon)$ (typically about $\sqrt{n}\,\varepsilon$). |
| Sorted summation | $O(n\varepsilon)$ | $O(1)$ | Add from smallest. The constant improves for some data, but this is not guaranteed. Requires $O(n \log n)$ sort. |
| Pairwise summation | $O(\log n \cdot \varepsilon)$ | $O(\log n)$ | Recursively add halves. Used by NumPy's np.sum. |
| Kahan summation | $O(\varepsilon)$ | $O(1)$ | Compensation variable tracks errors. Independent of $n$. |
Kahan Compensated Summation
Accumulates the fractional part lost to rounding in a compensation variable, correcting at each step. See Loss of Significance for pseudocode and details.
Pairwise Summation
Recursively splits the array in half and sums each half. Only $\log_2 n$ addition levels are needed, bounding accumulated error at $O(\log n \cdot \varepsilon)$.
function pairwiseSum(a[], lo, hi):
if hi - lo < 16: // base case: naive sum
s = 0.0
for i = lo to hi-1: s += a[i]
return s
mid = (lo + hi) / 2
return pairwiseSum(a, lo, mid)
+ pairwiseSum(a, mid, hi)
Other Techniques
- Avoiding catastrophic cancellation: Transform formulas to avoid subtraction of nearly equal values.
- Higher precision: Use quadruple or arbitrary precision when double precision is insufficient.
- Choosing stable algorithms: Prefer backward-stable algorithms.
4. Trade-off with Truncation Error
The two main sources of error in an algorithm are round-off error and truncation error (errors in the input data and modeling errors are considered separately), and the overall error is roughly estimated as their sum. In many situations, improving accuracy causes round-off error to grow, so an optimal parameter exists.
Numerical Differentiation Example
Consider the forward difference $f'(x) \approx \dfrac{f(x+h) - f(x)}{h}$.
- Truncation error (Taylor remainder): $\dfrac{h}{2}|f''(\xi)| = O(h)$ — decreases as $h$ shrinks.
- Round-off error: $f(x+h)$ and $f(x)$ each carry rounding error $O(\varepsilon |f(x)|)$, proportional to the size of the function value. The difference $f(x+h)-f(x)$ retains error of this size, and dividing by $h$ amplifies it to $O(\varepsilon |f(x)|/h)$.
Minimizing total error $E(h) \sim C_1 h + C_2 \varepsilon/h$ gives optimal step $h^* = \sqrt{C_2\varepsilon/C_1}$; smaller $h$ actually degrades accuracy. Here $C_1 = |f''(\xi)|/2$ and $C_2 \approx 2|f(x)|$ (since the rounding errors of $f(x+h)$ and $f(x)$ are each about $\varepsilon|f(x)|$; the constant also depends on how the function is computed, so it is only a guide), so $h^*$ depends on the function. If $|f(x)|$ and $|f''(x)|$ are comparable, $h^* \approx 2\sqrt{\varepsilon} \approx 3 \times 10^{-8}$ (double precision); roughly, $h^* \sim \sqrt{\varepsilon} \approx 10^{-8}$ in order of magnitude.
The figure below plots $E_{\text{trunc}}=C_1 h$ (blue), $E_{\text{round}}=C_2\varepsilon/h$ (red), and total error (green) on a log-log scale. The two lines cross at the optimal $h^*$, where total error reaches its minimum.
Two Mechanisms of Round-off Error Growth
In numerical differentiation, decreasing $h$ does not increase the number of operations. Round-off grows because dividing the $O(\varepsilon |f(x)|)$ rounding error in $f(x+h)-f(x)$ by a small $h$ amplifies the error (a different mechanism from §2). In numerical integration, however, smaller step sizes increase the number of subintervals $n=(b-a)/h$ (for the interval $[a, b]$), causing round-off accumulation exactly as in the §2 model.
Series Truncation
Truncating the Taylor series to the finite sum $e^x \approx \displaystyle\sum_{k=0}^{N} x^k/k!$ also exhibits a trade-off.
- Truncation error: $O(|x|^{N+1}/(N+1)!)$ — decreases rapidly as $N$ increases.
- Round-off error: $N$ additions → $O(N\varepsilon)$ accumulation (§2 model).
Unless $|x|$ is large, the terms of the series for $e^x$ decrease rapidly ($|x|^k/k!$ starts to decrease once $k$ exceeds $|x|$), so round-off accumulation is typically not a concern at practical $N$ (a few dozen terms). The more serious issue is catastrophic cancellation when computing $e^{-x}$ ($x>0$) via $\displaystyle\sum (-x)^k/k!$, which requires reformulation such as $e^{-x}=1/e^x$ (though for very large $x$, $e^x$ itself overflows, requiring a different approach).
5. Frequently Asked Questions
Q1. How does round-off error accumulate?
Over $n$ operations, if each rounding error is random and independent, cumulative error grows as $O(\sqrt{n} \cdot \varepsilon)$. In the worst case (same-direction accumulation), it grows as $O(n \cdot \varepsilon)$. For summation, Kahan compensated summation keeps the error at $O(\varepsilon)$ regardless of $n$.
Q2. Round-off error vs. truncation error?
Round-off error comes from finite-precision arithmetic; truncation error from replacing infinite mathematical processes with finite approximations. The error of an algorithm is roughly estimated as the sum of the two (errors in the input data are considered separately); as step size decreases, truncation error decreases but round-off error increases.
Q3. Error bounds for summation algorithms?
Naive sequential: $O(n\varepsilon)$, sorted: $O(n\varepsilon)$ (the constant improves for some data), pairwise: $O(\log n \cdot \varepsilon)$, Kahan compensated: $O(\varepsilon)$. Here $\varepsilon$ is machine epsilon and $n$ is the number of terms.
6. References
- Wikipedia, "Round-off error"
- Wikipedia, "Kahan summation algorithm"
- Wikipedia, "Pairwise summation"
- N. J. Higham, Accuracy and Stability of Numerical Algorithms, 2nd ed., SIAM, 2002, Chapter 4 (Summation).
- D. Goldberg, "What Every Computer Scientist Should Know About Floating-Point Arithmetic," ACM Computing Surveys, vol. 23, no. 1, pp. 5--48, 1991.