---
title: Rounding error and cancellation
heading: How a few ulps of error become a wrong answer
eyebrow: A13 · Lesson 2 · Rounding error and cancellation
xp: 40
---

::: hook
One rounding costs at most a part in $10^{16}$. That sounds harmless, and it usually is, until two nearly equal numbers are subtracted. Their leading digits cancel, and what's left is mostly the rounding error they carried, now promoted to the front. The quadratic formula, the difference of two close voltages, $1 - \cos x$ for small x, a running sum of millions of samples: each can lose most of its digits this way. The cure is rarely more precision; it's rewriting the formula so it never subtracts nearly equal quantities.
:::

::: play
- Both curves are $(1 - \cos x)/x^2$, whose true value tends to $\frac12$. The solid one is computed as written, in double precision, live in your browser; the dashed one uses the identity $1 - \cos x = 2\sin^2(x/2)$.
- Move toward small x: around $10^{-5}$ the solid curve starts to wobble, by $10^{-7}$ it's wrong in the fourth digit, and below $10^{-8}$ it's exactly 0, because $\cos x$ rounds to exactly 1.
- The dashed curve stays at 0.5 all the way down: it never subtracts.
:::
:::

```sim plot
view: graph
x: x = 1e-10..1 log
y: f = 0..1
plot: (1 - cos(x))/x^2 | samples: 2000 | label: (1 − cos x)/x², as written
plot: 2*sin(x/2)^2/x^2 | dashed | label: 2 sin²(x/2)/x²
readout: cos x rounds to 1 below about = 2^(-26.5)
```

::: predict
## In double precision, what does $(10^8 - \sqrt{10^{16} - 4})/2$ give for the small root of $x^2 - 10^8x + 1 = 0$, whose true value is $10^{-8}$?

- [ ] $1.0000000 \times 10^{-8}$
- [x] About $7.45 \times 10^{-9}$, 25% off
- [ ] 0
- [ ] An overflow

::: reveal
$7.450580596923828 \times 10^{-9}$. The square root is $10^8 - 2 \times 10^{-8}$, which rounds to the nearest double, $10^8 - 1.49 \times 10^{-8}$ (doubles near $10^8$ are $1.49 \times 10^{-8}$ apart). Subtracting from $10^8$ leaves just that rounding. The rewritten form $\frac{2c}{b + \sqrt{b^2 - 4c}}$ adds instead of subtracting and gives $1.0000000000000000 \times 10^{-8}$.
:::
:::

::: nameit
## Errors through arithmetic

If $\hat a = a(1 + \delta_a)$ and $\hat b = b(1 + \delta_b)$:

- **multiplying or dividing** adds relative errors: harmless;
- **adding numbers of one sign** gives a relative error no worse than the larger;
- **subtracting nearly equal numbers** keeps the absolute error, $\lvert a\delta_a - b\delta_b\rvert$, but divides by a small result: the relative error grows by about $\frac{\lvert a\rvert + \lvert b\rvert}{\lvert a - b\rvert}$. That's **cancellation**, and it costs about $\log_{10}\frac{\lvert a\rvert}{\lvert a - b\rvert}$ decimal digits.

## Rewrite to avoid it

| Instead of | Use |
|---|---|
| $1 - \cos x$ | $2\sin^2(x/2)$ |
| $\sqrt{x + 1} - \sqrt x$ | $\frac{1}{\sqrt{x + 1} + \sqrt x}$ |
| $\frac{-b + \sqrt{b^2 - 4ac}}{2a}$ (with $b > 0$) | $\frac{2c}{-b - \sqrt{b^2 - 4ac}}$ |
| $e^x - 1$, $\ln(1 + x)$ for small x | `math.expm1(x)`, `math.log1p(x)` |

## Sums

Adding n numbers left to right has an error bound of about $(n - 1)u\sum\lvert x_i\rvert$, because each partial sum is rounded. Adding 0.1 ten million times in double gives 999 999.9998389754. **Kahan's compensated summation**, or Python's `math.fsum`, carries the lost low-order part along and gives 1 000 000 exactly. (Since version 3.12, Python's built-in `sum` compensates too; a plain loop doesn't.)
:::

::: example
## Two node voltages are 2.500 004 V and 2.500 001 V, each known to 1 µV. What's their difference, and to what relative accuracy?

::: solution
1. **The difference.**
   $$\begin{aligned} \Delta V &= 2.500\,004 - 2.500\,001\ \text{V} \\ &= 3\ \text{µV} \end{aligned}$$
2. **Its error.** Each input can be off by 1 µV, so the difference can be off by up to 2 µV:
   $$\frac{2\ \text{µV}}{3\ \text{µV}} = 67\%$$
   a relative error of 67%, from inputs good to 0.4 parts per million.
3. **The amplification factor.**
   $$\frac{2.5 + 2.5}{3 \times 10^{-6}} \approx 1.7 \times 10^6$$
4. **The fix.** The fix is in the measurement: measure the difference directly, with a differential amplifier.

**Answer:** the difference is 3 µV, uncertain by up to 2 µV, a relative error of 67% from inputs good to 0.4 parts per million.
:::
:::

::: example
## Evaluate $\sqrt{x^2 + 1} - x$ at $x = 10^8$ in double.

::: solution
1. **The direct formula.** Doubles near $10^{16}$ are 2 apart, so $x^2 + 1 = 10^{16} + 1$ rounds to $10^{16}$:
   $$\sqrt{10^{16}} - 10^8 = 0$$
   The formula gives exactly 0.
2. **The rewritten form.**
   $$\begin{aligned} \frac{1}{\sqrt{x^2 + 1} + x} &= \frac{1}{2 \times 10^8} \\ &= 5 \times 10^{-9} \end{aligned}$$
   This is right to full precision.

**Answer:** the formula as written gives exactly 0; the rewritten form gives $5 \times 10^{-9}$, right to full precision.
:::
:::

::: example
## Compute $1 - \cos x$ at $x = 10^{-5}$ in double, and find a form that keeps all its digits.

::: solution
1. **The cosine.** $\cos 10^{-5} = 0.99999999995$, which rounds to a double whose last bits are uncertain at the $10^{-16}$ level.
2. **Subtract from 1.**
   $$\begin{aligned} 1 - \cos x &= 5.0000004 \times 10^{-11} \\ \text{true} &= 5.0000000 \times 10^{-11} \end{aligned}$$
   The seventh digit is already wrong, an error of about $10^{-8}$ relative.
3. **Use an identity with no subtraction.**
   $$\begin{aligned} 1 - \cos x &= 2\sin^2\frac x2 \\ &= 4.99999999996 \times 10^{-11} \end{aligned}$$
   This is correct to the last digit.
4. **The rule.** When a formula subtracts nearly equal quantities, look for an identity that does the cancellation algebraically.

**Answer:** direct subtraction gives $5.0000004 \times 10^{-11}$, wrong in the seventh digit; $2\sin^2\frac x2$ gives $4.99999999996 \times 10^{-11}$, correct to the last digit.
:::
:::

::: check
given: d = 0.001 | 0.000001 | 0.1
answer: (2 + d)/d
tolerance: 1%

## a = 1 + {d} and b = 1 each carry a relative rounding error u. By about what factor is the relative error of $a - b$ larger than u?

::: right
Right. $\frac{\lvert a\rvert + \lvert b\rvert}{\lvert a - b\rvert}$ = {answer}.
:::

::: misses
- [(1 + d)/d] Both inputs carry error; add their sizes: $(a + b)/(a - b)$.
- [any] Not yet. The absolute errors add; divide by the small result.
:::
:::

::: check
given: d = 0.001 | 0.000001 | 0.000000001
answer: log(1/d)
tolerance: 1%

## Two numbers agree to a relative difference of {d}. About how many decimal digits does subtracting them cost?

::: right
Right. $\log_{10}(1/d)$ = {answer} digits.
:::

::: misses
- [ln(1/d)] Decimal digits use $\log_{10}$.
- [any] Not yet. The leading digits that cancel: $\log_{10}\frac{\lvert a\rvert}{\lvert a - b\rvert}$.
:::
:::

::: check
given: b = 1000000 | 100000000 | 3000000; c = 1 | 2 | 5
answer: 2*c/(b + sqrt(b^2 - 4*c))
tolerance: 0.5%

## What's the small root of $x^2 - \given{b}x + \given{c} = 0$? (Compute it without cancellation.)

::: right
Right. $\frac{2c}{b + \sqrt{b^2 - 4c}}$ = {answer}, very nearly $c/b$.
:::

::: misses
- [any] Not yet. Use $x_{small} = \frac{2c}{b + \sqrt{b^2 - 4c}}$, which only adds.
:::
:::

::: check
given: p = 24 | 53 | 11
answer: 2^(-p/2)
tolerance: 2%

## In a format with {p} significant bits, below about what x does $\cos x$ round to exactly 1? (Use $\cos x \approx 1 - x^2/2$ and the spacing $2^{-p}$ just below 1.)

::: right
Right. When $x^2/2 < 2^{-p-1}$: x below about $2^{-p/2}$ = {answer}.
:::

::: misses
- [2^(-p)] The cosine's change is $x^2/2$, so x itself can be as large as the square root of the spacing.
- [any] Not yet. Solve $x^2/2 = 2^{-p-1}$.
:::
:::

::: check
given: n = 1000 | 1000000 | 1000000000; S = 1 | 1000
answer: (n - 1)*2^(-53)*S
tolerance: 1%

## A left-to-right double sum of {n} terms has $\sum\lvert x_i\rvert$ = {S}. What's the worst-case error bound $(n - 1)u\sum\lvert x_i\rvert$?

::: right
Right. {answer}. In practice errors partly cancel and the typical error is nearer $\sqrt n\,u\sum\lvert x_i\rvert$.
:::

::: misses
- [(n - 1)*2^(-52)*S] Use the unit roundoff $u = 2^{-53}$, half of epsilon.
- [any] Not yet. $(n - 1)\times 2^{-53}\times\sum\lvert x_i\rvert$.
:::
:::

::: check
kind: choice

## Which form of $\sqrt{x^2 + 1} - x$ is accurate for large x?

- [ ] $\sqrt{x^2 + 1} - x$, in higher precision
- [x] $\frac{1}{\sqrt{x^2 + 1} + x}$
- [ ] $x\left(\sqrt{1 + 1/x^2} - 1\right)$
- [ ] $\sqrt{x^2 + 1 - x^2}$

::: misses
- [1] Higher precision only delays the problem to larger x.
- [3] $\sqrt{1 + 1/x^2} - 1$ still subtracts nearly equal numbers.
- [4] That's $\sqrt{1} = 1$: it isn't the same function.
:::
:::

::: check
kind: parts
given: x = 0.001 | 0.01 | 0.0001

## Compute $1 - \cos x$ for x = {x} in double precision.

::: part
answer: 2*sin(x/2)^2
tolerance: 0.5%

### What is its true value?

::: misses
- [x^2] The leading term is $\frac{x^2}{2}$.
- [any] $2\sin^2\frac x2$.
:::
:::

::: part
answer: log(2/x^2)
tolerance: 1%

### About how many decimal digits does the direct subtraction lose?

::: misses
- [log(1/x)] The result is about $\frac{x^2}{2}$ relative to 1.
- [any] $\log_{10}\frac{1}{1 - \cos x} \approx \log_{10}\frac{2}{x^2}$.
:::
:::
:::

::: check
kind: derivation
vars: x = 1..100

## Rewrite expressions that cancel for large x.

::: step
answer: 1/(sqrt(x + 1) + sqrt(x))

### Write $\sqrt{x + 1} - \sqrt x$ without a subtraction.
:::

::: step
answer: ln(1 + 1/x)

### Write $\ln(x + 1) - \ln x$ as a single logarithm.
:::

::: right
Right. Multiplying by the conjugate, or combining the logarithms, moves the cancellation into exact algebra.
:::
:::

::: explain
## Why does subtracting two nearly equal numbers destroy accuracy, when each number is accurate to 16 digits?

::: model
Each number carries a small absolute error, about one part in $10^{16}$ of its size. Subtracting removes the leading digits the two numbers share, but it can't remove their errors; those stay the same absolute size. The result is much smaller than the inputs, so the same absolute error is now a much larger fraction of it. If the inputs agreed to 12 digits, only about 4 meaningful digits remain. Nothing was rounded badly in the subtraction itself; it just exposed the errors that were already there, which is why the cure is to rearrange the formula so the subtraction never happens.
:::
:::

::: practice

::: check
given: d = 0.1 | 0.01 | 0.001 | 0.00001 | 0.0000001
answer: (2 + d)/d
tolerance: 1%

## Error amplification of (1 + {d}) − 1?

::: misses
- [any] $(a + b)/(a - b)$.
:::
:::

::: check
given: b = 5000000 | 20000000; c = 3 | 7
answer: 2*c/(b + sqrt(b^2 - 4*c))
tolerance: 0.5%

## Small root of $x^2 - \given{b}x + \given{c}$?

::: misses
- [any] $\frac{2c}{b + \sqrt{b^2 - 4c}}$.
:::
:::

::: check
given: d = 0.001 | 0.00001 | 0.0000001 | 0.000000001 | 0.0000000001
answer: log(1/d)
tolerance: 1%

## Digits lost subtracting numbers {d} apart (relatively)?

::: misses
- [any] $\log_{10}(1/d)$.
:::
:::
:::
