Sub topic 12
Cancellation Error When Computing Roots
Prerequisites and the source of the problem
This chapter assumes that you can solve linear equations, expand polynomial expressions, simplify square roots, and read a coordinate graph. You should also know the quadratic formula:
\[ x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}. \]
A quadratic equation has standard form \(ax^2+bx+c=0\), where \(a\ne0\). The discriminant is \(D=b^2-4ac\). This chapter focuses on computing real roots accurately when \(D\ge0\).
The formula contains two subtractions:
\[ -b-\sqrt D \quad\text{and}\quad -b+\sqrt D. \]
When two numbers with nearly equal values are subtracted, most of their leading digits cancel. This is called cancellation error. A computer stores numbers with limited precision, so the digits that remain may contain a large relative error.
An example of cancellation
Consider
\[ x^2+10^8x+1=0. \]
Here \(a=1\), \(b=10^8\), and \(c=1\). The roots are approximately
\[ x=-10^8 \quad\text{and}\quad x=-10^{-8}. \]
The discriminant is
\[ D=(10^8)^2-4=10^{16}-4. \]
The small root uses
\[ \frac{-10^8+\sqrt{10^{16}-4}}{2}. \]
The two terms in the numerator are almost equal:
\[ -10^8+\sqrt{10^{16}-4}\approx -10^8+10^8. \]
A floating-point calculation can round the square root to \(10^8\). The numerator then becomes zero, so the computed small root may be \(0\), even though the true root is about \(-10^{-8}\).
The large root does not have this problem because it uses the addition of the magnitudes:
\[ \frac{-10^8-\sqrt{10^{16}-4}}{2}. \]
The issue is not that the quadratic formula is mathematically wrong. The issue is that finite-precision arithmetic cannot preserve enough digits during the subtraction.
A more stable calculation
The roots of
\[ ax^2+bx+c=0 \]
satisfy Vieta's product relationship:
\[ x1x2=\frac ca. \]
First compute the root whose numerator does not suffer cancellation. Define
\[ q=-\frac{b+\operatorname{sign}(b)\sqrt D}{2}. \]
Then calculate
\[ x1=\frac qa, \qquad x2=\frac{c}{q}. \]
The second root comes from the product relationship rather than from subtracting nearly equal numbers.
If \(b>0\), the stable numerator uses \(-b-\sqrt D\). If \(b<0\), it uses \(-b+\sqrt D\). The sign choice makes the two terms add in magnitude.
import math
def stable_real_roots(a, b, c):
if a == 0:
raise ValueError("a must be nonzero")
discriminant = b * b - 4 * a * c
if discriminant < 0:
raise ValueError("the roots are not both real")
root_discriminant = math.sqrt(discriminant)
q = -(b + math.copysign(root_discriminant, b)) / 2
# This case includes ax² = 0 and a repeated root at zero.
if q == 0:
return [0.0]
return [q / a, c / q]
print(stable_real_roots(1, 10**8, 1))
# Approximately [-100000000.0, -1e-08]The function assumes that the coefficients are ordinary finite numbers. For a repeated root, both formula results should agree mathematically. For very large or very small coefficients, scaling the equation can provide additional protection against overflow or underflow.
Comparing the two methods
| Method | Strength | Risk |
|---|---|---|
| Direct quadratic formula | Simple and mathematically familiar | Can lose precision through cancellation |
| Stable formula using \(q\) and \(c/q\) | Preserves the small root more accurately | Requires a sign choice and a zero check |
The stable method does not change the exact answer. It changes the sequence of arithmetic operations so that the computer keeps useful digits.
Checking computed roots
Use Vieta's relationships as quick checks:
\[ x1+x2=-\frac ba, \qquad x1x2=\frac ca. \]
For the example \(x^2+10^8x+1=0\), the roots should have sum \(-10^8\) and product \(1\). If a direct computation returns \(0\) for the small root, the product is also \(0\), which exposes the loss of accuracy.
Exercise
Why does the stable method compute one root as c/q instead of using the other sign in the quadratic formula?
Show answer
The other sign may subtract nearly equal numbers and lose significant digits. Since the product of the roots is c/a, c/q obtains the second root without that cancellation.