No history yet

Floating-Point Stability

When the Quadratic Formula Fails

The standard quadratic formula, x=b±b24ac2ax = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}, is a cornerstone of algebra. In pure mathematics, it is infallible. In computational practice, however, it harbours a subtle but critical flaw, especially when implemented using finite-precision arithmetic like floating-point numbers.

The problem emerges from subtraction, specifically when two nearly equal numbers are subtracted. This phenomenon, known as , can lead to a drastic loss of significant digits, polluting the result with noise from rounding errors.

Consider the quadratic equation where the term 4ac4ac is much smaller in magnitude than b2b^2. In this scenario, the discriminant Δ=b24ac\Delta = b^2 - 4ac is very close to b2b^2, making Δ\sqrt{\Delta} very close to b|b|.

If b24ac, then b24acb\text{If } b^2 \gg |4ac|, \text{ then } \sqrt{b^2 - 4ac} \approx |b|

This approximation creates a problem for one of the roots. If b>0b > 0, the calculation of the root x1=b+b24ac2ax_1 = \frac{-b + \sqrt{b^2 - 4ac}}{2a} involves subtracting two nearly identical numbers in the numerator, triggering catastrophic cancellation. The same issue occurs for the other root, x2x_2, if b<0b < 0.

The root computed via the subtraction of two nearly equal quantities will suffer a severe loss of relative accuracy.

Quantifying the Error

Let's analyse the condition number for the roots of ax2+bx+c=0ax^2+bx+c=0. The condition number of a problem measures how much the output value can change for a small change in the input data. A problem with a high condition number is called ill-conditioned. For the roots xx, the condition number with respect to changes in cc is given by:

κc=cxxc=cx(2ax+b)=cb24acx\kappa_c = \left| \frac{c}{x} \frac{\partial x}{\partial c} \right| = \left| \frac{c}{x (2ax+b)} \right| = \frac{|c|}{\sqrt{b^2-4ac}|x|}

When 4ac4ac is small, the discriminant b24acb\sqrt{b^2-4ac} \approx |b|. One root, let's call it the large root, will be approximately b/a-b/a. The other root, the small root, will be approximately c/b-c/b. For the small root, the condition number becomes large, indicating that the problem of finding this root is inherently sensitive to small errors in the coefficients. But the failure of the standard formula is an issue of numerical instability, not just ill-conditioning.

Lesson image

A More Stable Alternative

To avoid this loss of precision, we can use a different formulation for the problematic root. The key is to rationalise the numerator, transforming the problematic subtraction into an addition.

We know that for a quadratic equation, the product of the roots x1x2x_1 x_2 is equal to c/ac/a. This relationship, known as Vieta's formulas, provides the basis for a stable algorithm. The procedure is as follows:

  1. First, compute the discriminant Δ=b24ac\Delta = b^2 - 4ac.
  2. Compute the first root using the standard formula, but choose the sign inside the numerator to avoid subtraction. This means if bb is positive, we use bΔ-b - \sqrt{\Delta}. If bb is negative, we use b+Δ-b + \sqrt{\Delta}. This can be written compactly using the sign function, sgn(b)\text{sgn}(b).
x1=bsgn(b)b24ac2ax_1 = \frac{-b - \text{sgn}(b) \sqrt{b^2 - 4ac}}{2a}

This calculation for x1x_1 is always stable as it involves adding quantities of the same sign (or subtracting quantities of opposite signs).

  1. Finally, compute the second root using the product-of-roots relationship to avoid cancellation altogether.
x2=cax1x_2 = \frac{c}{a x_1}

This two-step method ensures that both roots are computed with high relative accuracy, sidestepping the pitfalls of catastrophic cancellation that plague the naive implementation of the quadratic formula.

For many applications, the problem of floating-point errors can be attenuated by cleverly re-arranging the order in which certain operations are performed.

Let's test your understanding of these numerical pitfalls.

Quiz Questions 1/4

What is the primary numerical issue when implementing the standard quadratic formula, x=b±b24ac2ax = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}, using finite-precision arithmetic, particularly when the term 4ac4ac is much smaller than b2b^2?

Quiz Questions 2/4

For the quadratic equation x2+108x+1=0x^2 + 10^8x + 1 = 0, which root calculation would be numerically unstable using the standard formula due to catastrophic cancellation?

Understanding these stability issues is crucial for writing robust numerical code, ensuring that theoretical algorithms perform reliably in the real world of finite-precision machines.