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 .
The value 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:
- Does end in binary? If the extra bits are dropped, is the stored value above or below ?
- How many tenths of a second pass in 100 hours?
- If every tick adds a tiny error , how large is the total error after ticks, and how does it depend on how long the system has been running?
Solution
- No: never ends. Dropping the extra bits removes something positive, so the stored value is a little below and the clock runs slow.
- ticks.
- The same error is added at every tick, so the total error is : 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 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 :
Approximating by gives and , within , 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 value | Approximation | Absolute error | Relative error |
|---|---|---|---|
| 2 | 3 | 1 | 0.5 |
| 20,000 | 20,001 | 1 | 0.00005 |
Now the Patriot. Chopped to 23 fractional bits, the stored value is .
So each tick is short by seconds and . When a number is rounded to six significant digits, the error is at most half a unit in the sixth digit, that is, 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 seconds (), 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 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 seconds, with a target moving at about meters per second, are meters, more than the window half-width of 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:
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 meters and that the target moves at 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).
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 seconds, after ticks the clock error is and the shift of the window is .
The requirement for an intercept is
In a 100-hour run there are ticks. We solve the inequality for :
What remains is to ask how many bits are needed for the error per tick to be smaller than this.
Chopping to fractional bits drops less than one unit of the last place, that is, less than , so it is enough that .
is too large, and 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 , 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.
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.
| Setting | Clock error | Window shift | Outcome |
|---|---|---|---|
| 23 bits, chopped, 100 h (1991) | 0.3433 s | 687 m | misses |
| 23 bits, chopped, 8 h | 0.0275 s | 55 m | intercepts |
| 23 bits, rounded, 100 h | −0.0858 s | −172 m | misses |
| 24 bits, chopped, 100 h | 0.1287 s | 257 m | misses |
| 25 bits, chopped, 100 h | 0.0215 s | 43 m | intercepts |
| 36 bits, chopped, 100 h | 0.0000314 s | 0.063 m | intercepts |
| 23 bits, chopped, time since the last detection | 0.00000095 s | 0.0019 m | intercepts |
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:
| Hours | Clock error | GAO shift | This lesson’s model |
|---|---|---|---|
| 1 | 0.0034 s | 7 m | 7 m |
| 8 | 0.0275 s | 55 m | 55 m |
| 20 | 0.0687 s | 137 m | 137 m |
| 48 | 0.1648 s | 330 m | 330 m |
| 72 | 0.2472 s | 494 m | 494 m |
| 100 | 0.3433 s | 687 m | 687 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 (), 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 to 25 bits is , better than the bound, because and only the is dropped.
At 24 bits the dropped part is units and the error is , 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 meters, a battery that runs 24 hours, chopping and time counted since switch-on (the speed stays meters per second).
- Calculate, as in the section “What is required to intercept”, how many bits the formula guarantees.
- 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 and , then find the smallest with .
Solution
, so seconds per tick. is slightly too large, and 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 , 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 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 is a sum with infinitely many terms, shown first as a general term and then with its first terms:
We can only add finitely many of them. The following code adds the terms up to in Python and prints, after each term, the difference from .
Here is how to read it:
for n in range(1, 11)repeats with ;term /= ndividestermby , sotermis ;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 | Error |
|---|---|
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 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 a total of times, the number of ticks in 100 hours, with an ordinary double.
The sum is instead of : an error of seconds, about 28,000 times smaller than the Patriot’s 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 numbers measures how spread out they are around their mean :
it is the mean of the squared deviations from the mean.
We can also compute the same variance in an equivalent form, “mean of the squares minus the 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 . Here the mean is , and the squares are near .
The two numbers the formula subtracts, the mean of the squares and the square of the mean, are almost exactly equal, about .
Near the gap between stored numbers is (by the gap formula), so each of them is stored with an error of up to 64.
A true difference of 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 :
and after dividing by 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 , where is the large common size and are small differences.
Then the mean is , where is the mean of the differences, and so
The large size cancels exactly, before we square. In the example , , , and the deviations are :
we square small numbers, , and their mean is . 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 the small root is about .
The quadratic formula, run on a computer, gives , wrong in the first digit.
The reason: for the small root the formula subtracts two numbers that are both close to .
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 turns a difference of two numbers into a difference of their squares.
What happens to the numerator if we multiply the numerator and the denominator by ?
The rearranged formula for the small root
Multiplying the numerator and the denominator by the conjugate of the numerator, , gives, for , an equivalent formula for the small root:
Now both terms in the denominator are positive and nothing cancels.
For , , it gives . The large root uses the original formula, where the signs agree.
A second example uses a trigonometric identity. For small the function
is close to , but the numerator subtracts two numbers that are almost equal:
is close to 1.
Plan in words before you open the block: which trigonometric identity replaces by an expression with no subtraction?
Hint
The double-angle identity is . Substitute .
The formula with no subtraction
The half-angle identity gives an equal formula with no subtraction:
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 the number 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 the gap is 2, so
1e16 + 1 == 1e16: the added 1 is swallowed. - In a loop that adds 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 are stored with a gap of 128, while the deviations and the variance 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:
- 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.
- 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.
- 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:
- U.S. General Accounting Office, Patriot Missile Defense: Software Problem Led to System Failure at Dhahran, Saudi Arabia, report IMTEC-92-26, 1992.
- Douglas Arnold, Two disasters caused by computer arithmetic errors, with the Patriot case and the Ariane 5 failure of 1996.
- The Ariane 5 Flight 501 inquiry board report, a second case where a numerical conversion mattered.