Lesson

Small Errors, Big Consequences: The Patriot Clock

How a rounding error of one part in ten million brought down a missile defence: absolute and relative error, accumulation, truncation and round-off error, and cancellation.

Last updated:


A problem no formula warned us about

On 25 February 1991, during the Gulf War, a Patriot air-defence battery in Dhahran, Saudi Arabia, did not track an incoming Scud missile.
The missile hit a barracks and killed 28 soldiers. The United States General Accounting Office, today the Government Accountability Office (GAO), traced the failure to a software problem in the battery’s computer.
The battery had been running for more than 100 hours without a restart, and its clock had drifted by about a third of a second.
The report (GAO report IMTEC-92-26) lists how far such a clock error shifts the radar’s search window:
687 metres after 100 hours. That is the clock error multiplied by a speed of about 2,000 metres per second, the speed that the report’s own table implies.

A computer that follows its program exactly was wrong by a third of a second.
How? We have everything we need from How Computers Store Numbers and Rounding and Machine Epsilon to work it out.

What the system did. It counted time as a whole number of tenths of a second, and to get seconds it multiplied that count by 0.10.1⁠.
The value 0.10.1 was stored in a register (a small memory segment inside the processor that holds a number) of 24 bits, and the bits that did not fit were simply dropped (this is called chopping).
In this lesson and in the simulator, the number of bits is the number of binary digits kept after the binary point.
We take the 1991 setting to be 23 of them, because that is the choice that reproduces the clock error of 0.3433 s that the GAO report lists after 100 hours.

Work it out before you use anything on this page:

  1. Does 0.10.1 end in binary? If the extra bits are dropped, is the stored value above or below 0.10.1⁠?
  2. How many tenths of a second pass in 100 hours?
  3. If every tick adds a tiny error ee⁠, how large is the total error after NN ticks, and how does it depend on how long the system has been running?
Solution
  1. No: 0.1=0.00011‾20.1=0.0\overline{0011}_2 never ends. Dropping the extra bits removes something positive, so the stored value is a little below 0.10.1 and the clock runs slow.
  2. 100⋅3600⋅10=3,600,000100\cdot3600\cdot10=3{,}600{,}000 ticks.
  3. The same error is added at every tick, so the total error is N eN\,e⁠: it grows in proportion to the running time. Minutes after a restart the drift is harmless; after days it is not. This is why the failure appeared only after more than 100 hours.

Measuring an error: absolute and relative

The lecture How Computers Store Numbers introduced two numbers that describe how wrong an approximation x^\hat x is; here we put them to work.
The absolute error is the size of the difference, and the relative error compares it with the true value x≠0x\neq0⁠:

E=∣x−x^∣,Erel=∣x−x^∣∣x∣E=|x-\hat x|,\qquad E_{\text{rel}}=\frac{|x-\hat x|}{|x|}

Approximating 13\tfrac13 by 0.3330.333 gives E=13000≈3.3×10−4E=\tfrac{1}{3000}\approx3.3\times10^{-4} and Erel=10−3E_{\text{rel}}=10^{-3}⁠, within 5×10−35\times10^{-3}⁠, the bound for rounding to three significant digits.
The relative error usually matters more, because the same absolute error can be a disaster or irrelevant:

True valueApproximationAbsolute errorRelative error
2310.5
20,00020,00110.00005
101510^{15}1015−10610^{15}-10^{6}10610^{6}10−910^{-9}

Now the Patriot. Chopped to 23 fractional bits, the stored value is 0.09999990463…0.09999990463\ldots⁠.
So each tick is short by E=9.54×10−8E=9.54\times10^{-8} seconds and Erel=9.54×10−7E_{\text{rel}}=9.54\times10^{-7}⁠. When a number is rounded to six significant digits, the error is at most half a unit in the sixth digit, that is, 5×10−65\times10^{-6} relative to the size of the number.
Our error is smaller than that, so the stored value is correct to at least six significant digits.
That looks like enough: after a full day of ticks the clock is behind by only about 0.080.08 seconds (86400×9.54×10−7≈0.08286400\times9.54\times10^{-7}\approx0.082⁠), a relative error below one in a million.
Why was it not enough?

Because the error repeats. The relative error of the elapsed time stays at 9.54×10−79.54\times10^{-7} for as long as the system runs, but the elapsed time itself keeps growing, so the absolute error grows with it.
And what the radar needs is an absolute quantity: how many metres.
Those same 0.0820.082 seconds, with a target moving at about 20002000 meters per second, are 0.082×2000≈1650.082\times2000\approx165 meters, more than the window half-width of 100100 meters:
the required accuracy is tens of meters. The window where the radar looks for the target moves by the speed times the clock error:

shift=2000⏟speed (m/s)×3.6×106⏟ticks in 100 h×9.54×10−8⏟error per tick (s)≈687 m\text{shift}=\underbrace{2000}_{\text{speed (m/s)}}\times\underbrace{3.6\times10^{6}}_{\text{ticks in 100 h}}\times\underbrace{9.54\times10^{-8}}_{\text{error per tick (s)}}\approx687\ \text{m}

A tiny relative error, repeated millions of times and multiplied by a high speed, becomes almost 700 metres.

What is required to intercept

The radar predicts where the target will be one second later and looks in a window around that point.
If the clock is wrong, the window is in the wrong place, and it intercepts only as long as the target is still inside it.
Suppose, as later on this page, that the half-width of the window is G=100G=100 meters and that the target moves at v=2000v=2000 meters per second (the speed is derived from the table of the GAO report, and only the half-width of the window is an assumption of this lesson, which the sources we cite do not give).
GG is half the width of the window in which the radar searches for the target, not the size of the missile:
if the target leaves the window, the radar does not find it.
If each tick adds an error of ee seconds, after NN ticks the clock error is N eN\,e and the shift of the window is v N ev\,N\,e⁠.
The requirement for an intercept is

v⋅N⋅e≤Gv\cdot N\cdot e\le G

In a 100-hour run there are N=100⋅3600⋅10=3.6×106N=100\cdot3600\cdot10=3.6\times10^{6} ticks. We solve the inequality for ee⁠:

e≤Gv N=1002000×3.6×106≈1.39×10−8 seconds per ticke\le\frac{G}{v\,N}=\frac{100}{2000\times3.6\times10^{6}}\approx1.39\times10^{-8}\ \text{seconds per tick}

What remains is to ask how many bits are needed for the error per tick to be smaller than this.
Chopping to pp fractional bits drops less than one unit of the last place, that is, less than 2−p2^{-p}⁠, so it is enough that 2−p≤1.39×10−82^{-p}\le1.39\times10^{-8}⁠.
2−26≈1.49×10−82^{-26}\approx1.49\times10^{-8} is too large, and 2−27≈7.45×10−92^{-27}\approx7.45\times10^{-9} is small enough. So 27 bits guarantee an intercept with the 1991 method.
This condition is sufficient but not necessary: if it holds, an intercept is guaranteed, and if it does not, the radar may still intercept, as the simulator will show.

The simulator: check the calculation and play with the parameters

Choose the precision of the stored 0.10.1⁠, how the extra bits are treated, how time is counted and how long the battery has been running, then launch the attempt.
First check whether the calculation above fits: choose chopping, time since switch-on and 100 hours, and try 27 bits, then fewer.

The simulator needs JavaScript. The table and the figure below show the same results.

A straight line showing the radar window shift growing with the hours of operation: 55 metres after 8 hours and 687 metres after 100 hours, far above a window of plus or minus 100 metres.
The window shift grows in proportion to the running time (23 bits, chopped, time counted since switch-on).

The table lists seven settings you can reproduce in the simulator (“bits” counts the bits after the binary point, as in the simulator), for an assumed window of plus or minus 100 metres.
The size of the window is an assumption of this lesson: the sources we cite do not give it.

SettingClock errorWindow shiftOutcome
23 bits, chopped, 100 h (1991)0.3433 s687 mmisses
23 bits, chopped, 8 h0.0275 s55 mintercepts
23 bits, rounded, 100 h−0.0858 s−172 mmisses
24 bits, chopped, 100 h0.1287 s257 mmisses
25 bits, chopped, 100 h0.0215 s43 mintercepts
36 bits, chopped, 100 h0.0000314 s0.063 mintercepts
23 bits, chopped, time since the last detection0.00000095 s0.0019 mintercepts
Check against the GAO report

Appendix II of the GAO report IMTEC-92-26 (“Patriot Missile Defense: Software Problem Led to System Failure at Dhahran, Saudi Arabia”) lists, for several operating times, the clock error and the approximate shift of the range gate (the window where the radar looks for the target next).
The lesson’s model reproduces the report’s table:

HoursClock errorGAO shiftThis lesson’s model
10.0034 s7 m7 m
80.0275 s55 m55 m
200.0687 s137 m137 m
480.1648 s330 m330 m
720.2472 s494 m494 m
1000.3433 s687 m687 m

The report does not state the speed behind its shifts. Dividing each shift by its clock error gives about 2,000 metres per second every time (6870.3433≈2001\frac{687}{0.3433}\approx2001⁠), so the lesson uses 2,000 m/s, which is the speed implied by the report’s own table.
The more often quoted Scud speed of about 1,676 m/s would give smaller shifts (575 m after 100 hours); we do not know which speed or geometry the report assumed, for example the closing speed between the Scud and the interceptor.
The report also notes that after about 20 hours of continuous operation the target was outside the range gate, and that the battery in Dhahran had run about 100 hours.
The window of ±100 m used in the lesson is consistent with that note, but it is our assumption.

In the simulator we see that 25 bits already work: the actual error of chopping 0.10.1 to 25 bits is 6×10−96\times10^{-9}⁠, better than the bound, because 0.1⋅225=3355443.20.1\cdot2^{25}=3355443.2 and only the 0.20.2 is dropped.
At 24 bits the dropped part is 0.60.6 units and the error is 3.6×10−83.6\times10^{-8}⁠, too large.
The guaranteed bound is safe but not tight, and the exact threshold depends on the digits of the number that is stored.
Both answers are above the 23 bits after the point of the 1991 setting, and the guaranteed bound changes with the data (the window, the running time and the speed).
A 64-bit double has 52 fractional bits, far more than needed.

Self-check. We change the data: a window half-width of 5050 meters, a battery that runs 24 hours, chopping and time counted since switch-on (the speed stays 20002000 meters per second).

  1. Calculate, as in the section “What is required to intercept”, how many bits the formula guarantees.
  2. Check in the simulator (window ±50, 24 hours) the smallest number of bits that intercepts in practice, and compare with your calculation.
Hint

First calculate NN and e≤Gv Ne\le\dfrac{G}{v\,N}⁠, then find the smallest pp with 2−p≤e2^{-p}\le e⁠.

Solution

N=24⋅3600⋅10=864,000N=24\cdot3600\cdot10=864{,}000⁠, so e≤502000×864,000≈2.89×10−8e\le\dfrac{50}{2000\times864{,}000}\approx2.89\times10^{-8} seconds per tick. 2−25≈2.98×10−82^{-25}\approx2.98\times10^{-8} is slightly too large, and 2−26≈1.49×10−82^{-26}\approx1.49\times10^{-8} fits:
26 bits guarantee an intercept. In the simulator 25 bits already intercept (a shift of about 10 meters) and 24 bits miss (about 62 meters), because the condition the formula gives is sufficient but not necessary.
Note that the guaranteed bound dropped from 27 to 26 when the data changed, while the practical answer, 25 bits, stayed what it was in the 100-hour run with a ±100 meter window.
If your result differs, check that the slider is on 50 meters and 24 hours.

Two lessons beyond the bits:

  • More bits is not the only lever. Counting only the time since the last detection (a few ticks instead of millions) keeps the drift at about a millimetre even with 23 bits. A better algorithm beat more precision. According to the GAO report, a modified version of the software reached Dhahran the day after the attack.
  • A closely related effect appears in any loop that adds a stored 0.10.1⁠, as the next sections show. The two are not identical: the Patriot multiplied a growing whole-number count by a slightly wrong constant, so its error grows in proportion to the count, while a loop that adds 0.10.1 over and over rounds again at every addition.

Two kinds of numerical error

The Patriot error came from the finite number of bits: a round-off error.
The second kind appears when we replace an infinite process by a finite one, a truncation error.
Do not confuse this name with the chopping we saw in the opening of the lesson:
chopping is how bits are dropped when a number is stored (and the round-off error it causes is the Patriot’s), while a truncation error comes from stopping an infinite process.
For example, the number ee is a sum with infinitely many terms, shown first as a general term and then with its first terms:

e=∑k=0∞1k!=1+1+12+16+124+⋯e=\sum_{k=0}^{\infty}\frac{1}{k!}=1+1+\frac12+\frac16+\frac1{24}+\cdots

We can only add finitely many of them. The following code adds the terms up to 1n!\tfrac1{n!} in Python and prints, after each term, the difference from ee⁠.
Here is how to read it:

  • for n in range(1, 11) repeats with n=1,2,…,10n=1,2,\dots,10⁠;
  • term /= n divides term by nn⁠, so term is 1n!\tfrac1{n!}⁠;
  • abs(...) is the absolute value.
import math
total, term = 1.0, 1.0        # the first term, 1/0! = 1
for n in range(1, 11):
    term /= n                  # term is now 1/n!
    total += term
    print(n, abs(math.e - total))
1 0.7182818284590451
2 0.2182818284590451
3 0.05161516179237857
4 0.009948495125712054
5 0.0016151617923787498
6 0.0002262729034896438
7 2.7860205076724043e-05
8 3.0586177750535626e-06
9 3.0288585284310443e-07
10 2.7312660577649694e-08

The error left by stopping, the truncation error, shrinks very fast:

Terms up to 1n!\frac1{n!}Error
n=2n=22.2×10−12.2\times10^{-1}
n=4n=49.9×10−39.9\times10^{-3}
n=6n=62.3×10−42.3\times10^{-4}
n=8n=83.1×10−63.1\times10^{-6}
n=10n=102.7×10−82.7\times10^{-8}

Truncation error is controlled by the method (how many terms, how small a step); round-off error is controlled by the arithmetic.
A good method keeps both small. The next sections show round-off error at work:
accumulation in a loop and catastrophic cancellation.

Accumulation in a loop: the Patriot in miniature

Add 0.10.1 ten times in Python:

t = 0.0
for _ in range(10):
    t += 0.1
print(t, t == 1.0)     # 0.9999999999999999 False
0.9999999999999999 False

A loop meant to stop at 1.0 shows the same effect:

t, n = 0.0, 0
while t < 1.0:
    t += 0.1
    n += 1
print(n)               # 11
11

It runs eleven times instead of ten. Now add 0.10.1 a total of 3,600,0003{,}600{,}000 times, the number of ticks in 100 hours, with an ordinary double.
The sum is 359999.99998799…359999.99998799\ldots instead of 360000360000⁠: an error of 1.2×10−51.2\times10^{-5} seconds, about 28,000 times smaller than the Patriot’s 0.340.34 seconds.
This is a closely related effect, not an identical one: each addition rounds again, and the precision decided how fast it drifted.
Python’s math.fsum adds a list of floats with one rounding at the end instead of one per addition:
math.fsum([0.1] * 10) gives 1.0.

Cancellation: when subtracting destroys the answer

Accumulation was the Patriot’s trouble. A second, faster way to lose digits is to subtract two nearly equal numbers:
the leading digits cancel, and what is left is mostly earlier rounding errors.
This is catastrophic cancellation.

A real situation from data work: the variance of a set of numbers that are large but close together, such as timestamps or counters.
The variance of nn numbers x1,…,xnx_1,\dots,x_n measures how spread out they are around their mean μ\mu⁠:
it is the mean of the squared deviations from the mean.

σ2=1n∑i=1n(xi−μ)2=1n((x1−μ)2+(x2−μ)2+⋯+(xn−μ)2),μ=1n∑i=1nxi\sigma^2=\frac1n\sum_{i=1}^{n}(x_i-\mu)^2=\frac1n\Bigl((x_1-\mu)^2+(x_2-\mu)^2+\cdots+(x_n-\mu)^2\Bigr), \qquad \mu=\frac1n\sum_{i=1}^{n}x_i

We can also compute the same variance in an equivalent form, “mean of the squares minus the square of the mean”:

σ2=1n∑i=1nxi2⏟mean of the squares−μ2⏟square of the mean\sigma^2=\underbrace{\frac1n\sum_{i=1}^{n}x_i^2}_{\text{mean of the squares}}-\underbrace{\mu^2}_{\text{square of the mean}}

The two forms are equal on paper. We compute the second form in code:
len(data) is the number of items in the list data, sum(...) adds them up, and sum(x * x for x in data) adds x * x for every x in the list:

data = [1e9 + 4, 1e9 + 7, 1e9 + 13, 1e9 + 16]
n = len(data)
mean = sum(data) / n
naive = sum(x * x for x in data) / n - mean ** 2
print(naive)           # 0.0
0.0

The true variance is 22.522.5⁠. Here the mean is μ=1,000,000,010\mu=1{,}000{,}000{,}010⁠, and the squares are near 101810^{18}⁠.
The two numbers the formula subtracts, the mean of the squares and the square of the mean, are almost exactly equal, about 1.00000002×10181.00000002\times10^{18}⁠.
Near 101810^{18} the gap between stored numbers is 259−52=1282^{59-52}=128 (by the gap formula), so each of them is stored with an error of up to 64.
A true difference of 22.522.5 is smaller than that, and the subtraction returns only what is left of the rounding errors:
the leading digits were erased, and no digit of the data remains.

The second form comes from expanding the brackets of the first.
Since ∑i=1nxi=nμ\sum_{i=1}^{n}x_i=n\mu⁠:

∑i=1n(xi−μ)2=∑i=1nxi2−2μ∑i=1nxi+nμ2=∑i=1nxi2−nμ2\sum_{i=1}^{n}(x_i-\mu)^2=\sum_{i=1}^{n}x_i^2-2\mu\sum_{i=1}^{n}x_i+n\mu^2=\sum_{i=1}^{n}x_i^2-n\mu^2

and after dividing by nn we get the second form. When the numbers are large and close to each other, each of its two terms is very large and almost equal to the other:
this is exactly a subtraction of nearly equal numbers.

The algebra also shows how to avoid it. We write each number as xi=c+dix_i=c+d_i⁠, where cc is the large common size and did_i are small differences.
Then the mean is μ=c+dˉ\mu=c+\bar d⁠, where dˉ\bar d is the mean of the differences, and so

xi−μ=(c+di)−(c+dˉ)=di−dˉx_i-\mu=(c+d_i)-(c+\bar d)=d_i-\bar d

The large size cc cancels exactly, before we square. In the example c=109c=10^9⁠, di=4,7,13,16d_i=4,7,13,16⁠, dˉ=10\bar d=10⁠, and the deviations are −6,−3,3,6-6,-3,3,6⁠:
we square small numbers, 36,9,9,3636,9,9,36⁠, and their mean is 22.522.5⁠. The subtraction that erases digits, between a number and the mean, is done on the data itself, which is stored exactly, not on squares that were already rounded.
So in the code we subtract the mean first:

two_pass = sum((x - mean) ** 2 for x in data) / n
print(two_pass)        # 22.5
22.5

The same trap sits in the quadratic formula. For x2−108x+1=0x^2-10^8x+1=0 the small root is about 10−810^{-8}⁠.
The quadratic formula, run on a computer, gives 7.45×10−97.45\times10^{-9}⁠, wrong in the first digit.
The reason: for the small root the formula subtracts two numbers that are both close to 10810^8⁠.

Plan in words before you open the block: how can the formula be rewritten so that the small root is computed without subtracting two close numbers?

Hint

The product (p−q)(p+q)=p2−q2(p-q)(p+q)=p^2-q^2 turns a difference of two numbers into a difference of their squares.
What happens to the numerator −b−b2−4ac-b-\sqrt{b^2-4ac} if we multiply the numerator and the denominator by −b+b2−4ac-b+\sqrt{b^2-4ac}⁠?

The rearranged formula for the small root

Multiplying the numerator and the denominator by the conjugate of the numerator, −b+b2−4ac-b+\sqrt{b^2-4ac}⁠, gives, for b<0b<0⁠, an equivalent formula for the small root:

x2=−b−b2−4ac2a=2c−b+b2−4acx_2=\frac{-b-\sqrt{b^2-4ac}}{2a}=\frac{2c}{-b+\sqrt{b^2-4ac}}

Now both terms in the denominator are positive and nothing cancels.
For a=1a=1⁠, b=−108b=-10^8⁠, c=1c=1 it gives 1.0×10−81.0\times10^{-8}⁠. The large root uses the original formula, where the signs agree.

A second example uses a trigonometric identity. For small xx the function

f(x)=1−cos⁡xx2f(x)=\frac{1-\cos x}{x^{2}}

is close to 12\tfrac12⁠, but the numerator subtracts two numbers that are almost equal:
cos⁡x\cos x is close to 1.

Plan in words before you open the block: which trigonometric identity replaces 1−cos⁡x1-\cos x by an expression with no subtraction?

Hint

The double-angle identity is cos⁡2t=1−2sin⁡2t\cos 2t=1-2\sin^{2}t⁠. Substitute t=x2t=\tfrac x2⁠.

The formula with no subtraction

The half-angle identity 1−cos⁡x=2sin⁡2 ⁣x21-\cos x=2\sin^{2}\!\tfrac x2 gives an equal formula with no subtraction:

1−cos⁡xx2=2sin⁡2(x/2)x2\frac{1-\cos x}{x^{2}}=\frac{2\sin^{2}(x/2)}{x^{2}}

In the code, math.cos and math.sin are the cosine and the sine, with the angle in radians.

import math
x = 1e-8
print((1 - math.cos(x)) / x**2)           # 0.0
print(2 * math.sin(x / 2)**2 / x**2)      # 0.5
0.0
0.5

For x=10−8x=10^{-8} the number cos⁡x=1−5×10−17\cos x=1-5\times10^{-17} is less than half a gap below 1, so it is stored as exactly 1 and the subtraction returns 0.
The rewritten formula gives the right answer. Both formulas are the same function on paper; they differ only in what the computer does with them.

What to do about it

  • Judge an approximation by its relative error when a number of correct digits is what matters, and by the absolute error when the consequence is measured in the units of the problem (metres, seconds, money).
  • Count how many operations repeat. A tiny error times millions of repetitions is not tiny.
  • Prefer quantities that stay small: the time since the last event, differences from the mean, not totals since the beginning.
  • Do not subtract nearly equal numbers if algebra can avoid it.
  • Rounding to nearest makes each single error smaller, but it does not cancel accumulation when the same constant is rounded the same way every time: the error is the same in every repetition and in the same direction, so it adds up instead of canceling. In the Patriot, rounding instead of chopping reduced the shift to 172 meters, not to zero.
  • Test with the failure in mind: ask what happens after a very long run.

Why prefer small quantities? The danger is not that small numbers cancel each other, but that a large number covers the small details.
A stored number holds a fixed number of significant digits, and these are its large digits.
The gap between stored numbers grows with the size of the number, and the small details that interest us lie beyond the last digit that is stored, so they are rounded or swallowed.
In a small number the same stored digits are close to the details.
Three examples we have already seen:

  • Near 101610^{16} the gap is 2, so 1e16 + 1 == 1e16: the added 1 is swallowed.
  • In a loop that adds 0.10.1 again and again, as the sum grows the gap between stored numbers grows, and the tenth is closer and closer to the edge of the stored digits. A small sum keeps it more accurately.
  • In the variance, the squares near 101810^{18} are stored with a gap of 128, while the deviations −6,−3,3,6-6,-3,3,6 and the variance 22.522.5 are far smaller than that. The deviations themselves are small and stored exactly.

What we learned to do in this unit

When a system computes with floating-point numbers, this unit taught three things:

  1. Decide the precision the system needs. Translate the requirement into the units of the problem (for example, a drift of a few hundredths of a second after a hundred hours), choose absolute or relative error, estimate how much error accumulates, and choose a format (binary16, binary32 or binary64). The comparison threshold is chosen the same way: not smaller than the gap between neighboring stored numbers, or equal numbers will look different, and not too coarse, or distant numbers will count as close.
  2. Tell truncation error from round-off error. Identify the source of a deviation in a result, an expansion stopped early or a number stored approximately, and estimate its size.
  3. Plan an order of operations that reduces round-off error. Count repetitions and convert once, prefer differences from the mean over sums from the start, and do not subtract nearly equal numbers when the formula can be rewritten.

Continue learning

The next step is the practice session for this lecture: absolute or relative, drift and subtraction of nearly equal numbers in code, the usual mistakes and transfer questions.

Unit 2 studies equations that cannot be solved by a formula, such as finding the yield of a bond or the implied volatility of an option.
The methods there stop when an error estimate falls below a tolerance, an absolute or a relative error as defined on this page; the gaps between stored numbers from Rounding and Machine Epsilon limit how small a tolerance can be.

For a deeper look: