Interactive Educational Modules in
Scientific Computing

Quadratic Formula

This module demonstrates some potential pitfalls in solving quadratic equations using finite-precision, floating-point arithmetic. Naive implementation of the standard quadratic formula in floating-point arithmetic can result in poor accuracy or outright failure due to overflow, underflow, or cancellation.

The user first selects the precision to be used in the calculation, from one to ten decimal digits. The coefficiencts a, b, and c of the quadratic equation can then be entered explicitly, or chosen at random, or chosen from a pre-set example. To avoid potential overflow or unnecessary underflow, the coefficients can optionally be rescaled so that the largest has magnitude 1. Clicking "calculate" then causes the two roots of the equation to be computed in the selected working precision using both the standard quadratic formula and an alternative formula having a different sign pattern. For comparison, the correctly rounded roots are also shown. Typically, each formula will produce one accurate root and one with significant error, but because of the differing sign patterns, the opposite root is contaminated.

In this module, the randomly generated coefficients are selected in a way that guarantees the roots of the quadratic equation will be real. They are also chosen in a way that slightly favors problems for which the quadratic formula will experience cancellation between −b and the square root.

Reference: Michael T. Heath, Scientific Computing, An Introductory Survey, 2nd edition, McGraw-Hill, New York, 2002. See Section 1.3.9, especially Example 1.15.

Developers: Nicholas Exner and Michael Heath