4.8.5Numerical Methods

Numerical stability vs instability — catastrophic cancellation

1,680 words8 min readdifficulty · medium6 backlinks

WHAT is going on?


WHY does subtraction destroy digits? (Derivation from scratch)

Suppose the true values are aa and bb with aba \approx b. The computer holds a^=a(1+δa),b^=b(1+δb),δa,δbu.\hat a = a(1+\delta_a), \qquad \hat b = b(1+\delta_b), \qquad |\delta_a|,|\delta_b|\le u.

Now form the difference a^b^\hat a - \hat b. The error is: a^b^=(ab)+(aδabδb).\hat a - \hat b = (a-b) + (a\,\delta_a - b\,\delta_b).

So the absolute error is aδabδba\delta_a - b\delta_b, bounded by u(a+b)u(|a|+|b|)small, just as expected.

The trouble is the relative error:

(a^b^)(ab)ab=aδabδbab.\frac{(\hat a - \hat b) - (a-b)}{a-b} = \frac{a\delta_a - b\delta_b}{a-b}.

Bounding it:

 rel errorua+bab \boxed{\ \left|\frac{\text{rel error}}{} \right| \lesssim u\,\frac{|a|+|b|}{|a-b|}\ }

The factor a+bab\dfrac{|a|+|b|}{|a-b|} is the condition number of subtraction. When aba\approx b it is enormous — that is the catastrophe.


Figure — Numerical stability vs instability — catastrophic cancellation

HOW to spot and fix it

The cure is almost always algebra: rewrite the expression so the dangerous subtraction never happens.



Recall Feynman: explain it to a 12-year-old

Imagine two tall towers of blocks, one 1000 blocks high, the other 1001. You only measured each to the nearest block — so each could really be off by half a block. Now you ask "how much taller is the second?" The answer is about 1 block — but your half-block uncertainties are now HUGE compared to that 1-block answer! You're confident about the towers but clueless about the tiny difference. The trick: don't measure both towers and subtract — find a way to measure the gap directly.


Active Recall

What is catastrophic cancellation?
A severe loss of relative accuracy when subtracting two nearly-equal rounded numbers; surviving significant digits are few.
Does cancellation increase the absolute error?
No — absolute error stays u(a+b)\lesssim u(|a|+|b|). It magnifies the relative error because aba-b is tiny.
What is the condition number of subtracting aba-b?
a+bab\dfrac{|a|+|b|}{|a-b|} — huge when aba\approx b.
Rule of thumb for lost digits?
If a,ba,b agree to kk leading digits, aba-b loses about kk significant digits.
Stable fix for x+1x\sqrt{x+1}-\sqrt{x}?
Multiply by conjugate: 1x+1+x\dfrac{1}{\sqrt{x+1}+\sqrt{x}} (turns difference into a sum).
Stable fix for the small quadratic root?
Compute the large-magnitude root by addition, then use Vieta x1x2=c/ax_1x_2=c/a to divide for the small one.
Stable form of 1cosx1-\cos x for small xx?
2sin2(x/2)2\sin^2(x/2), or the Taylor series x2/2x4/24+x^2/2 - x^4/24+\cdots.
Conditioning vs stability?
Conditioning = sensitivity of the problem; stability = error-amplification of the algorithm.
Does higher precision cure cancellation?
No, it only delays it; reformulating the algorithm is the real cure.
Unit roundoff uu for IEEE double?
1.1×1016\approx 1.1\times10^{-16} (~16 significant decimal digits).

Connections

Concept Map

stores as fl x = x 1+delta

hides in last digits

leading digits cancel

absolute error stays small

relative error blows up

quantified by

agree to k digits

is a mechanism of

contrast with

cured by algebra

e.g. conjugate rationalise

Finite precision floating-point

Rounding error bounded by u

Subtract nearly-equal numbers

Catastrophic cancellation

Absolute error ~ u times abs a + abs b

Huge relative error

Condition number abs a + abs b over abs a - b

Lose about k significant digits

Numerical instability

Stable: errors stay bounded

Rewrite to avoid subtraction

sqrt x+1 minus sqrt x becomes a sum

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, computer numbers ko sirf ~16 significant digits tak store karta hai (double precision). Jab tum do bahut close numbers ko subtract karte ho — jaise x+1x\sqrt{x+1}-\sqrt{x} jahan dono almost barabar hain — to aage ke matching digits cancel ho jaate hain, aur peeche chhupa hua rounding error suddenly front pe aa jaata hai. Isko catastrophic cancellation kehte hain. Mazedaar baat: absolute error chhota hi rehta hai, lekin relative error phat jaata hai, kyunki asli answer to bahut chhota hai aur error uske saamne bada lagne lagta hai.

Kitni digits gayi? Simple rule: agar do numbers pehle kk digits tak same hain, to subtract karne pe lagbhag kk digits ki accuracy chali jaati hai. Iska "kitna bura hoga" measure karta hai condition number a+bab\frac{|a|+|b|}{|a-b|} — jab aba\approx b ye number huge ho jaata hai.

Fix kya hai? Algebra se subtraction ko dodge karo. x+1x\sqrt{x+1}-\sqrt{x} ko conjugate se multiply karke 1x+1+x\frac{1}{\sqrt{x+1}+\sqrt{x}} bana do — ab difference ki jagah addition hai, koi cancellation nahi. Quadratic ke chhote root ke liye pehle bada root (addition se) nikaalo, fir Vieta x1x2=c/ax_1 x_2 = c/a use karke division se chhota root lo. 1cosx1-\cos x ke liye 2sin2(x/2)2\sin^2(x/2) use karo. Yaad rakho: higher precision sirf problem ko aage tak khinchta hai, solve nahi karta — asli ilaaj formula ko rewrite karna hai.

Ek important distinction: conditioning problem ki property hai (problem khud sensitive hai kya), aur stability algorithm ki property hai (tumhara method error ko badhata hai kya). Catastrophic cancellation ek unstable algorithm ka famous example hai — same maths, smart reformulation, full accuracy back.

Test yourself — Numerical Methods

Connections