3.1 Errors
Source: Numerical-error ideas from Computational Physics (Landau, Páez, Bordeianu), Chapter 3. Rewritten in original wording; the sine series and the four-digit identity are standard classroom calculations, not a transcription of the book.
A computer printout looks exact. It is not. Every number you get is some mix of what the mathematics asked for and what the machine and the algorithm could actually do. Before you trust a result — especially a long run — name the error families and estimate which one is in charge.
Bugs and sloppy algebra are still the first place to look. Once the program is the program you meant to write, three more families remain.
Random error
Uncontrolled physical events: a voltage hiccup, a cosmic-ray bit flip, someone pulling a plug. You do not choose when they happen. Their chance of showing up grows with wall-clock time. A twenty-second check can look rock solid; a week-long integration may need independent reruns before you believe it is reproducible.
That is the opposite of truncation and round-off, which (on the same binary and the same inputs) happen the same way every time.
Random error
1 / 2Why can a week-long run be less trustworthy than a 20-second one even if the code is unchanged?
Pick an answer to see if you are right.
Approximation (algorithmic) error
You almost never run the exact mathematics. Infinite series become finite sums, becomes a step , a function that varies is frozen as a constant for one slice. The gap between the true math and that finite recipe is approximation error — also called algorithmic error, because it lives in the method, not in the hardware.
The textbook example is the sine series, which is exact as an infinite sum and approximate once you stop at terms:
is the ignored tail from onward. For a good algorithm that tail should shrink as grows and vanish as .
For this particular series the scale of is set by : you need . If and are comparable, the leftover is large even though the infinite series is perfect.
Theorem (truncation of a convergent scheme). If the discrete method is consistent with the mathematics, algorithmic error as the discretization parameter . For the sine Taylor sum, that limit is useful in practice only when is much larger than .
Approximation error
1 / 3Order the argument that a truncated sine series can still be accurate (top = start).
OrderDrag the handle to rearrange — top should be first.
- 1The infinite sum equals $\sin x$ exactly
- 2For this series the size of $N$ must be set by $x$: you need $N \gg |x|$
- 3A good algorithm makes $\mathcal{E}\to 0$ as $N\to\infty$
- 4The leftover $\mathcal{E}(x,N)$ is the tail from $N+1$ to $\infty$
Drag items into the correct order (or use ↑↓), then check.
Round-off error
Floating-point numbers keep finitely many digits. That is the same kind of limitation as a lab instrument with a finite number of significant figures: the value you store is close to the true value, not equal to it. Every extra arithmetic step is another chance to round, so the damage grows with the number of operations. Some methods amplify that noise and go unstable; when round-off is most of the answer, you are looking at garbage.
Toy machine with four digits after the decimal: becomes , becomes (last digit rounded). Then
Mathematically the combination is zero. On that machine it is a tiny bias. Repeat the same pattern millions of times and the tiny bias is no longer tiny.
Round-off error
1 / 2On a 4-decimal toy machine, $2(1/3)-2/3$ fails to be 0 because…
Pick an answer to see if you are right.
How a float is stored
Write a large number in scientific notation so the exponent is a small integer and the mantissa carries the significant figures:
The exponent is cheap to keep exactly. The mantissa is not: any digits past what the format allows are thrown away (or rounded). Double precision keeps on the order of 15–16 decimal digits of ; a 6-digit toy word would keep only and forget the rest. Relative error is — it falls as you keep more digits of , and has nothing to do with how large is.
Slide the digit count in the chart. Watch the relative error drop, then flatten once you have kept everything the true value had.
Stored floats and significant figures
1 / 2Order how a large floating-point value is stored (top = first).
OrderDrag the handle to rearrange — top should be first.
- 1Relative error is then |ã − a| / |a|, set by how many digits of m survived
- 2Write a = m × 10^e so the exponent is a small integer
- 3Digits of m beyond the format width are rounded or dropped
- 4Store e at full precision (it is small)
Drag items into the correct order (or use ↑↓), then check.
Those discarded least-significant digits are not a curiosity. Once a word has a fixed width, every later arithmetic step works with a slightly wrong mantissa. Subtract two such words and you can throw away the accurate part on purpose.
3.1.1 Courting disaster: subtractive cancellation
A stored float is the exact value times with of order machine precision. Feed that model through a close subtraction , then through a single quotient . Walk each identity — at each step pick the matching equation (lookalikes included).
Theorem. If you subtract two large numbers and the result is small, that small number is less significant than either of the large ones.
You already met this in the sine series for large : is a pile of big alternating terms whose true sum is only . The same trap sits in . A rewrite removes the cancellation: compute (all terms positive) and take the reciprocal. Round-off is still there; you just stopped manufacturing a tiny answer out of two giants.
Subtractive cancellation
1 / 3Order why a small difference of two large floats is untrustworthy (top = start).
OrderDrag the handle to rearrange — top should be first.
- 1When $b\simeq c$, the leading digits cancel and $a$ is small
- 2The leftover $a$ is assembled from the least-significant bits
- 3The factor $b/a$ multiplies whatever relative noise did not cancel
- 4Each stored value is $x_c\simeq x(1+\epsilon_x)$
Drag items into the correct order (or use ↑↓), then check.
3.1.2 Subtractive-cancellation labs
Quadratic formula
has two algebraically equal closed forms. When , , so one of the textbook roots cancels. Walk which form subtracts and how Vieta recovers the tiny root — pick among lookalikes.
A clean test family is , , as grows.
Input: three floats a b c (two distinct real roots). Print the smaller-magnitude root in scientific notation with 6 digits after the decimal (:.6e). Use a method that still works when .
The chart below is the log–log picture the vs lab is after: relative error versus , with as the reference. A straight stretch means the error is a power of . The vertical coordinate is (minus) a count of surviving significant figures.
Adding upward dumps small terms into a mantissa that is already full; downward parks the large terms last. That is why is usually the more precise of the two, even though the sums are identical on paper.
3.1.3 Round-off in one product, quotient, or function
The guide above already walked the product/quotient worst-case sum . Multiplication obeys the same rule: add the relative uncertainties.
For a smooth , the gap between and is first-order
On that is . Near a fourth-place error in stays a fourth-place error in — the derivative is not an amplifier here.
Propagating round-off
1 / 2Worst-case relative error in $b/c$ or $bc$ is…
Pick an answer to see if you are right.
3.1.4 Round-off error accumulation
One rounding event is a tiny step of length . After steps, if those steps point in random directions, the typical net displacement of a random walk is
If the errors refuse to cancel, a sober backup is the coherent bound . Worse: some upward recurrences (spherical Bessel functions are the usual exhibit) let the error grow like . Those need a separate analysis — the cartoon does not apply.
Accumulating round-off
1 / 2If each step’s rounding error is a random ±ε_m, the typical relative error after N steps scales as…
Pick an answer to see if you are right.
Which family is it?
Same program can carry all three. Name the dominant one before you “fix” the wrong knob.
Checking a numerical result
1 / 1Order a sane check of a long numerical job (top = first).
OrderDrag the handle to rearrange — top should be first.
- 1If the method uses a cutoff N or step h, raise N (or cut h) and see the answer settle
- 2If the job ran a long time, rerun it and compare (random error)
- 3Estimate whether the operation count could have let round-off take over
- 4Separate bugs from numerical error: is this even the intended algorithm?
Drag items into the correct order (or use ↑↓), then check.