Loss of Significance

Loss of significance is the loss of accurate digits that happens when a computer subtracts two nearly equal numbers. Their matching leading digits cancel, and what is left is mostly round-off error. The fix is to rewrite the formula, with a conjugate, an identity or a series, so the dangerous subtraction never happens.

Before this lesson: Absolute and Relative Error and Floating-Point Arithmetic

Why subtracting nearly equal numbers loses digits

Every stored number carries a small relative error, at most the unit roundoff u. That error sits in the last digit or two. When two numbers agree in their first several digits, subtracting them wipes out the digits that were correct and keeps the ones that were not.

Think of two 5-digit values, 0.76545 and 0.76541. Each is accurate to about 1 part in 200,000. Their difference, 0.00004, has just one significant digit, and that digit came from the rounding. The subtraction itself was exact; it simply exposed the error that was already there.

This is also called catastrophic cancellation. It is the most common way a short, correct formula gives a badly wrong answer on a computer, even in double precision.

Graph of (1 − cos x)/x² computed in double precision for x from 10^−9 to 10^−3 on a logarithmic axis. The true value is very close to 0.5. Computed directly, the orange points scatter widely, from about 0.35 to 1, for x between 10^−8 and 10^−7, and drop to exactly 0 for x below 10^−8. Computed with the rewritten formula 2 sin²(x/2)/x², the blue line stays at 0.5 everywhere.
Computed directly, (1 − cos x)/x² falls apart for small x; the rewritten form 2 sin²(x/2)/x² stays at the true value 0.5.

How many digits are lost

Suppose the stored values \tilde{x} and \tilde{y} have relative errors of at most \varepsilon. Then the relative error of their difference is bounded by

\frac{|(\tilde{x} - \tilde{y}) - (x - y)|}{|x - y|} \le \frac{|x| + |y|}{|x - y|}\,\varepsilon

The factor (|x| + |y|)/|x - y| is the amplification. It is huge when x and y are close. A quick rule of thumb for positive x \gt y:

\text{digits lost} \approx -\log_{10}\!\left(1 - \frac{y}{x}\right)

So if two numbers agree in their first 4 digits, expect to lose about 4 digits. Adding, multiplying and dividing do not have this problem; only the subtraction of nearly equal quantities does.

How to avoid loss of significance

You can’t get lost digits back by carrying more digits at the end. Instead, rewrite the formula before computing so that the nearly equal quantities are never subtracted:

  1. Conjugates: \sqrt{a} - \sqrt{b} = \dfrac{a - b}{\sqrt{a} + \sqrt{b}}.
  2. Trig identities: 1 - \cos x = \dfrac{\sin^2 x}{1 + \cos x} = 2\sin^2(x/2).
  3. Taylor series: for small x, e^x - 1 = x + \frac{x^2}{2} + \frac{x^3}{6} + \cdots.
  4. Algebra: \frac{1}{x} - \frac{1}{x+1} = \frac{1}{x(x+1)} and \ln(x+1) - \ln x = \ln(1 + 1/x).
  5. Quadratics: find the larger root with the quadratic formula, then the smaller one from x_1 x_2 = c/a.
  6. Reordering: subtract the mean before squaring when finding a variance.

Loss of significance examples

Example 1: Counting the lost digits

Let x = 0.765452 and y = 0.765413. Store both with 5-digit rounding, subtract, and measure the damage.

\operatorname{fl}(x) = 0.76545, \quad \operatorname{fl}(y) = 0.76541

Round each input to 5 significant digits.

\frac{|0.76545 - 0.765452|}{0.765452} \approx 2.6 \times 10^{-6}

Relative error in \operatorname{fl}(x): tiny.

\frac{|0.76541 - 0.765413|}{0.765413} \approx 3.9 \times 10^{-6}

Relative error in \operatorname{fl}(y): also tiny.

0.76545 - 0.76541 = 0.00004

Subtract the stored values. This step is exact.

0.765452 - 0.765413 = 0.000039

Find the true difference.

\frac{|0.00004 - 0.000039|}{0.000039} \approx 0.026

Relative error of the result: about 2.6%.

1 - \frac{0.765413}{0.765452} \approx 5.1 \times 10^{-5}

Measure how close the inputs are.

-\log_{10}(5.1 \times 10^{-5}) \approx 4.3

Apply the rule of thumb: about 4 of the 5 digits are lost.

Answer: the result 0.00004 has one significant digit, and its relative error (2.6%) is about 10,000 times the input errors. The bound gives \frac{1.530865}{0.000039}(3.9 \times 10^{-6}) \approx 0.15, so the actual 2.6% fits inside it.

Example 2: Rewriting with a conjugate

Use 4-digit rounding to find \sqrt{1001} - \sqrt{1000} two ways: directly, and after rewriting.

\sqrt{1001} = 31.6385\ldots, \quad \operatorname{fl}(\sqrt{1001}) = 31.64

Round the first root to 4 digits.

\sqrt{1000} = 31.6227\ldots, \quad \operatorname{fl}(\sqrt{1000}) = 31.62

Round the second root.

31.64 - 31.62 = 0.02

Subtract directly: one significant digit is left.

\sqrt{1001} - \sqrt{1000} = \frac{(\sqrt{1001} - \sqrt{1000})(\sqrt{1001} + \sqrt{1000})}{\sqrt{1001} + \sqrt{1000}}

Multiply top and bottom by the conjugate (see rationalizing the denominator).

\frac{1001 - 1000}{\sqrt{1001} + \sqrt{1000}} = \frac{1}{\sqrt{1001} + \sqrt{1000}}

Multiply out the numerator: it is a difference of squares.

31.64 + 31.62 = 63.26

Add the stored roots. Adding causes no cancellation.

\frac{1}{63.26} = 0.015807\ldots, \quad \operatorname{fl} = 0.01581

Divide and round to 4 digits.

Answer: the direct formula gives 0.02 and the rewritten one gives 0.01581. The true value is 0.0158074…, so the relative errors are about 27% and 0.016%.

Example 3: The small root of a quadratic

Solve x^2 - 75.30x + 1 = 0 with 4-digit rounding. Find the small root with the quadratic formula, then again from the product of the roots.

75.30^2 = 5670.09, \quad \operatorname{fl} = 5670

Square b and round to 4 digits.

5670 - 4(1)(1) = 5666

Subtract 4ac to get the discriminant.

\sqrt{5666} = 75.2728\ldots, \quad \operatorname{fl} = 75.27

Take the square root and round.

75.30 + 75.27 = 150.57, \quad \operatorname{fl} = 150.6

Large root from the quadratic formula: the terms add, so nothing cancels.

x_1 = \frac{150.6}{2} = 75.30

Divide by 2a = 2.

75.30 - 75.27 = 0.03

Small root from the formula: the terms nearly cancel.

x_2 = \frac{0.03}{2} = 0.01500

Divide by 2.

x_1 x_2 = \frac{c}{a} = 1

The product of the roots of ax^2 + bx + c is c/a.

x_2 = \frac{1}{75.30} = 0.013280\ldots, \quad \operatorname{fl} = 0.01328

Divide to find the small root from the large one.

Answer: the formula gives x_2 \approx 0.01500, about 13% off. The product of the roots gives 0.01328, with relative error about 0.02%. The true roots are 75.2867… and 0.013282….

Example 4: Using a trig identity

Use 5-digit rounding to find 1 - \cos x at x = 0.005 directly, then with the form \frac{\sin^2 x}{1 + \cos x}.

\cos 0.005 = 0.9999875\ldots, \quad \operatorname{fl} = 0.99999

Round the cosine to 5 digits.

1 - 0.99999 = 0.00001

Subtract directly: one digit is left.

1 - \cos x = \frac{(1 - \cos x)(1 + \cos x)}{1 + \cos x}

Multiply by 1 + \cos x over itself.

\frac{1 - \cos^2 x}{1 + \cos x} = \frac{\sin^2 x}{1 + \cos x}

Multiply out the top, then use \sin^2 x + \cos^2 x = 1 (see fundamental trig identities).

\sin 0.005 = 0.0049999\ldots, \quad \operatorname{fl} = 0.0050000

Round the sine to 5 digits.

0.0050000^2 = 0.000025000

Square it.

1 + 0.99999 = 1.99999, \quad \operatorname{fl} = 2.0000

Add and round. Again, adding is safe.

\frac{0.000025000}{2.0000} = 0.000012500

Divide.

Answer: the direct formula gives 0.00001, about 20% too low. The rewritten form gives 0.000012500, while the true value is 0.0000124999…, a relative error of about 2 \times 10^{-6}.

Example 5: A variance that comes out zero (word problem)

A quality lab measures three bolts at 10.001, 10.002 and 10.003 mm. Its software finds the population variance with the shortcut formula \sigma^2 = \overline{x^2} - \bar{x}^2 (mean of the squares minus the square of the mean), using 6-digit rounding. Find what it reports, then fix it.

10.001^2 = 100.020001, \quad \operatorname{fl} = 100.020

Square the first length and round to 6 digits.

10.002^2 = 100.040004, \quad \operatorname{fl} = 100.040

Square the second length.

10.003^2 = 100.060009, \quad \operatorname{fl} = 100.060

Square the third length.

100.020 + 100.040 + 100.060 = 300.120

Add the squares.

\overline{x^2} = \frac{300.120}{3} = 100.040

Divide by 3 to get the mean of the squares.

10.001 + 10.002 + 10.003 = 30.006

Add the lengths.

\bar{x} = \frac{30.006}{3} = 10.002

Divide by 3 to get the mean.

10.002^2 = 100.040004, \quad \operatorname{fl} = 100.040

Square the mean and round.

100.040 - 100.040 = 0

The shortcut subtracts two nearly equal numbers, and every digit cancels.

x_i - \bar{x} = -0.001, \; 0, \; 0.001

Fix: find each deviation from the mean first (the two-pass method in standard deviation).

(-0.001)^2 + 0^2 + 0.001^2 = 0.000002

Square the deviations and add them.

\sigma^2 = \frac{0.000002}{3} = 6.66667 \times 10^{-7}

Divide by 3 and round to 6 digits.

Answer: the shortcut reports a variance of 0, as if the three bolts were identical. The two-pass method gives 6.66667 \times 10^{-7} mm², correct to all 6 digits, so the standard deviation is about 0.000816 mm.

Common mistakes

MistakeExample of the errorFix
Blaming the subtraction step“0.76545 − 0.76541 was computed wrong”The subtraction is exact. It exposes errors that were already in the inputs.
Carrying extra digits only at the endRounding the answer to 10 digits after a 4-digit subtractionLost digits can’t come back. Rewrite the formula before computing.
Rewriting into a form that still subtractsReplacing 1 - \cos x with 1 - (1 - 2\sin^2(x/2)) and computing thatSimplify all the way to 2\sin^2(x/2), with no subtraction left.
Using the quadratic formula for both rootsx_2 = (75.30 - 75.27)/2 = 0.01500Find the larger root first, then x_2 = c/(a x_1).
Treating every subtraction as dangerousRewriting 1000 - 3 to avoid cancellationOnly nearly equal numbers cause trouble. Check how close y/x is to 1.
Using the one-pass variance formula on data with a large meanA variance of 0 for 10.001, 10.002 and 10.003Subtract the mean first, then square (the two-pass method).

Related ideas

Absolute and relative error

Loss of significance is a statement about relative error: the absolute error of a difference stays small, but the true difference is smaller still, so the relative error grows.

Round-off in floating-point arithmetic

In floating-point arithmetic, \operatorname{fl}(x - y) = (x - y)(1 + \delta) with |\delta| \le u. The subtraction adds almost no new error; the trouble is the error already stored in x and y.

Series as a fix

When no identity helps, a Maclaurin series often does. Terms like e^x - 1 or x - \sin x become sums with no cancellation when x is small.

Difference quotients

The quotient (f(x + h) - f(x))/h subtracts two nearly equal values when h is tiny, so making h smaller eventually makes the estimate worse. Numerical Differentiation shows how to choose h.

Practice problems

  1. Using 6-digit values, find 3.14159 - 3.14152. About how many significant digits are lost?
  2. Use 4-digit rounding to find \sqrt{5001} - \sqrt{5000} directly and with the conjugate.
  3. Rewrite \sqrt{x^2 + 1} - x to avoid cancellation for large x. Then evaluate both forms at x = 200 with 5-digit rounding.
  4. Use 4-digit rounding to find \frac{1}{1234} - \frac{1}{1235} directly and as \frac{1}{x(x+1)}.
  5. Rewrite \frac{1 - \cos x}{\sin x} to avoid cancellation near 0. Evaluate both forms at x = 0.004 with 5-digit rounding.
  6. Solve x^2 + 90.15x + 1 = 0 with 4-digit rounding, finding the small root without cancellation.
  7. Use 5-digit rounding to find e^x - 1 at x = 0.00002 directly and with two terms of its Maclaurin series.
  8. Find \ln(x + 1) - \ln x at x = 20000 with 5-digit rounding directly, then rewrite it and use \ln(1 + t) \approx t - \frac{t^2}{2}.
  9. Stored values x = 2.71828 and y = 2.71801 each have relative error at most 5 \times 10^{-6}. Bound the relative error of x - y.
  10. A laser beam leaves the ground level and horizontal. Two kilometers away, its height above Earth’s surface is \sqrt{R^2 + 4} - R km, with R = 6371 km. Find it with 6-digit rounding directly and with the conjugate.
Show answers

1.

3.14159 - 3.14152 = 0.00007

Subtract.

1 - \frac{3.14152}{3.14159} \approx 2.2 \times 10^{-5}

Measure how close the inputs are.

-\log_{10}(2.2 \times 10^{-5}) \approx 4.7

Apply the rule of thumb.

Answer: 0.00007, with one significant digit; about 5 of the 6 digits are lost.

2.

\operatorname{fl}(\sqrt{5001}) = 70.72, \quad \operatorname{fl}(\sqrt{5000}) = 70.71

Round each root to 4 digits (70.7177… and 70.7106…).

70.72 - 70.71 = 0.01

Subtract directly.

\sqrt{5001} - \sqrt{5000} = \frac{1}{\sqrt{5001} + \sqrt{5000}}

Rewrite with the conjugate.

70.72 + 70.71 = 141.43, \quad \operatorname{fl} = 141.4

Add the roots and round.

\frac{1}{141.4} = 0.0070721\ldots, \quad \operatorname{fl} = 0.007072

Divide and round.

Answer: directly 0.01 (about 41% too high); rewritten 0.007072 (true value 0.0070707…).

3.

\sqrt{x^2 + 1} - x = \frac{(\sqrt{x^2 + 1} - x)(\sqrt{x^2 + 1} + x)}{\sqrt{x^2 + 1} + x}

Multiply by the conjugate over itself.

\frac{(x^2 + 1) - x^2}{\sqrt{x^2 + 1} + x} = \frac{1}{\sqrt{x^2 + 1} + x}

Simplify the numerator.

\sqrt{40001} = 200.0024\ldots, \quad \operatorname{fl} = 200.00

At x = 200, round the root to 5 digits.

200.00 - 200 = 0

Direct form: every digit cancels.

200.00 + 200 = 400.00

Rewritten form: add.

\frac{1}{400.00} = 0.0025000

Divide.

Answer: \frac{1}{\sqrt{x^2 + 1} + x}; directly 0, rewritten 0.0025000 (true value 0.0024999…).

4.

\frac{1}{1234} = 0.00081037\ldots, \quad \operatorname{fl} = 0.0008104

Round the first fraction to 4 digits.

\frac{1}{1235} = 0.00080971\ldots, \quad \operatorname{fl} = 0.0008097

Round the second fraction.

0.0008104 - 0.0008097 = 0.0000007

Subtract directly.

\frac{1}{x} - \frac{1}{x+1} = \frac{(x + 1) - x}{x(x+1)} = \frac{1}{x(x+1)}

Combine over a common denominator (see adding and subtracting rational expressions).

1234 \cdot 1235 = 1523990, \quad \operatorname{fl} = 1524000

Multiply and round to 4 digits.

\frac{1}{1524000} = 6.5616\ldots \times 10^{-7}, \quad \operatorname{fl} = 6.562 \times 10^{-7}

Divide and round.

Answer: directly 7 \times 10^{-7} (about 7% high); rewritten 6.562 \times 10^{-7} (true value 6.5617\ldots \times 10^{-7}).

5.

\frac{1 - \cos x}{\sin x} \cdot \frac{1 + \cos x}{1 + \cos x} = \frac{1 - \cos^2 x}{\sin x\,(1 + \cos x)}

Multiply by 1 + \cos x over itself.

\frac{1 - \cos^2 x}{\sin x\,(1 + \cos x)} = \frac{\sin^2 x}{\sin x\,(1 + \cos x)}

Use 1 - \cos^2 x = \sin^2 x.

\frac{\sin^2 x}{\sin x\,(1 + \cos x)} = \frac{\sin x}{1 + \cos x}

Cancel one factor of \sin x.

\cos 0.004 = 0.999992\ldots, \quad \operatorname{fl} = 0.99999

Round the cosine to 5 digits.

\sin 0.004 = 0.0039999\ldots, \quad \operatorname{fl} = 0.0040000

Round the sine to 5 digits.

1 - 0.99999 = 0.00001

Direct form: subtract.

\frac{0.00001}{0.0040000} = 0.0025

Divide.

1 + 0.99999 = 1.99999, \quad \operatorname{fl} = 2.0000

Rewritten form: add and round.

\frac{0.0040000}{2.0000} = 0.0020000

Divide.

Answer: \frac{\sin x}{1 + \cos x}; directly 0.0025 (25% too high), rewritten 0.0020000 (true value 0.0020000…).

6.

90.15^2 = 8127.0225, \quad \operatorname{fl} = 8127

Square b and round to 4 digits.

8127 - 4 = 8123

Subtract 4ac = 4.

\sqrt{8123} = 90.1276\ldots, \quad \operatorname{fl} = 90.13

Take the square root and round.

-90.15 - 90.13 = -180.28, \quad \operatorname{fl} = -180.3

Large root: use the minus sign, so the terms add in size.

x_1 = \frac{-180.3}{2} = -90.15

Divide by 2.

x_2 = \frac{1}{-90.15} = -0.011092\ldots, \quad \operatorname{fl} = -0.01109

Use x_1 x_2 = c/a = 1.

Answer: x_1 \approx -90.15 and x_2 \approx -0.01109. (The formula directly gives (-90.15 + 90.13)/2 = -0.01000, about 10% off; the true small root is −0.0110939….)

7.

e^{0.00002} = 1.0000200\ldots, \quad \operatorname{fl} = 1.0000

Round to 5 digits.

1.0000 - 1 = 0

Subtract directly: everything cancels.

e^x - 1 = x + \frac{x^2}{2} + \frac{x^3}{6} + \cdots

Subtract 1 from the Maclaurin series for e^x.

\frac{0.00002^2}{2} = 0.0000000002

Find the second term.

0.00002 + 0.0000000002 = 0.0000200002, \quad \operatorname{fl} = 0.000020000

Add and round.

Answer: directly 0; with the series 2.0000 \times 10^{-5} (true value 2.00002 \times 10^{-5}).

8.

\ln 20001 = 9.90353\ldots, \quad \operatorname{fl} = 9.9035

Round to 5 digits.

\ln 20000 = 9.90348\ldots, \quad \operatorname{fl} = 9.9035

Round to 5 digits.

9.9035 - 9.9035 = 0

Subtract directly.

\ln(x + 1) - \ln x = \ln\frac{x + 1}{x} = \ln\!\left(1 + \frac{1}{x}\right)

Use the quotient rule for logs (see properties of logarithms).

t = \frac{1}{20000} = 0.00005

Find t = 1/x.

\frac{0.00005^2}{2} = 0.00000000125

Find the second series term.

0.00005 - 0.00000000125 = 0.00004999875, \quad \operatorname{fl} = 0.000049999

Subtract (the terms differ in size, so nothing cancels) and round.

Answer: directly 0; rewritten 4.9999 \times 10^{-5} (true value 4.99987\ldots \times 10^{-5}).

9.

2.71828 - 2.71801 = 0.00027

Find |x - y|.

2.71828 + 2.71801 = 5.43629

Find |x| + |y|.

\frac{5.43629}{0.00027} \approx 20134

Find the amplification factor.

20134 \times (5 \times 10^{-6}) \approx 0.10

Multiply by \varepsilon.

Answer: the relative error of x - y can be as large as about 10%.

10.

6371^2 = 40589641, \quad \operatorname{fl} = 40589600

Square R and round to 6 digits.

40589600 + 4 = 40589604, \quad \operatorname{fl} = 40589600

Add 4 and round: the 4 is lost.

\sqrt{40589600} = 6370.9967\ldots, \quad \operatorname{fl} = 6371.00

Take the square root and round.

6371.00 - 6371 = 0

Subtract directly.

\sqrt{R^2 + 4} - R = \frac{4}{\sqrt{R^2 + 4} + R}

Rewrite with the conjugate: the numerator is (R^2 + 4) - R^2 = 4.

6371.00 + 6371 = 12742.0

Add.

\frac{4}{12742.0} = 0.000313922\ldots, \quad \operatorname{fl} = 0.000313922

Divide and round to 6 digits.

Answer: directly 0 km; rewritten 0.000313922 km, or about 0.314 m (31 cm).

Loss of significance FAQ

What is loss of significance?

It is the loss of correct significant digits when two nearly equal numbers are subtracted. The leading digits cancel, so the result has fewer reliable digits than the inputs did.

What is catastrophic cancellation?

It is another name for loss of significance, used when the inputs already carry round-off error. If the inputs are exact, the cancellation is harmless and is sometimes called benign cancellation.

How do you avoid loss of significance?

Rewrite the formula so nearly equal quantities are never subtracted: multiply by a conjugate, use a trig or log identity, switch to a Taylor series for small inputs, or reorder the computation.

Why does the quadratic formula lose accuracy?

When b^2 is much larger than 4ac, \sqrt{b^2 - 4ac} is almost |b|, so one of the two roots subtracts nearly equal numbers. Find the other root first, then use x_1 x_2 = c/a.

Summary

  • Subtracting nearly equal numbers cancels their correct leading digits and leaves mostly round-off error.
  • The relative error of x - y can grow by the factor (|x| + |y|)/|x - y|; about -\log_{10}(1 - y/x) digits are lost.
  • Extra digits at the end don’t help. Rewrite the formula before computing.
  • Common fixes: conjugates, trig and log identities, Taylor series, the product of the roots for quadratics, and two-pass variance.
  • Adding, multiplying and dividing positive numbers are safe; only near-cancellation is dangerous.

Previous lesson: Floating-Point Arithmetic

Next lesson: Bisection Method

Back to the Numerical Methods Index