next up previous
Next: The Code Up: Roots of a Quadratic: Previous: Precision

Round-off errors and loss of significance

When two values of nearly equal magnitude are subtracted, significant digits are lost. For example subtracting 0.12345678 and 0.12345676 results in $0.00000002 = 0.2 \times10^{-7}$ with only one significant digit. If the two values subtracted had to be rounded to eight significant digits in order to fit into a 32 bit floating point number, the difference is clearly very inaccurate. Referring to the standard solution above, we see that we have trouble if $b$ is positive and $4ac$ is much less than $b^2$. In that case the two terms in the numerator are nearly equal in magnitude, so one of the roots (the one involving a subtraction) is unreliable. The other root should be fine. Which one it is depends on the sign of $b$. If $b$ is positive, then the solution with the positive square root involves a subtraction. If $b$ is negative, then the solution with the negative square root is risky.

There is a simple solution to this problem. We can divide the quadratic equation by $x^2$ so it reads

\begin{displaymath}
c/x^2 + b/x + a = 0.
\end{displaymath}

Notice that this is a quadratic in $1/x$. We can use the traditional formula to solve for $1/x$ and then invert it to get

\begin{displaymath}
x = \frac{2c}{-b \mp \sqrt{b^2 - 4ac}}.
\end{displaymath}

where the upper sign here correlates with the upper sign in the previous, standard formula. (Prove it yourself! It's just a simple exercise in algebra.) This formula also has one reliable root and one less reliable one. A little thought shows that the possibly bad root in one formula is the good root in the other.

So the best strategy is to use both formulas and to choose the more reliable root from each.


next up previous
Next: The Code Up: Roots of a Quadratic: Previous: Precision
Carleton DeTar 2017-09-05