Solving a quadratic equation at first glance seems to be a trivial calculation using the quadratic formula,
However, a closer look reveals a number of numerical subtleties that, if overlooked, could lead to grossly inaccurate approximations to the roots.
To see one of the problems associated with the numerical aspects of the quadratic formula, work through the following exercise.
Exercise 3.4 Solve the quadratic equation,
by evaluating the quadratic formula using three-digit decimal
arithmetic and unbiased rounding.
The exact
roots rounded to 6 digits are 0.0399675 and 12.3600.
The above exercise illustrates an important numerical problem called
cancellation or loss of significance which manifests itself
when subtracting values of nearly equal magnitude.
Cancellation occurs when the digits necessary to accurately define the
difference have been discarded by rounding in previous calculations due
to the finite precision of machine arithmetic. Problems arise when this
difference is an intermediate result which must be used to complete the
calculation--most of the significant digits that remain after rounding
are eliminated by subtraction. To complicate the situation, the digits
that become significant after subtraction may be accurate to only a few
places due to the previous rounding errors in the two values being
subtracted. For example, suppose that a calculation contains the
intermediate values,
and
, both correct only to 6 significant figures, with the last two
digits incorrect due to rounding errors in previous calculations.
Assuming a computer with 8 digit decimal arithmetic, the computed
difference in the two numbers is
. On the assumption that the last two digits
are incorrect due to previous rounding errors, this difference contains no
correct figures, all of which have been brought to significance after
subtraction.
Such severe cancellation can usually be eliminated by algebraic
reformulation. In the case of the quadratic equation, the cancellation
observed in the previous exercise results from the subtraction performed
between -b and
.
This cancellation occurs when 4ac is small relative to
, so that
. This problem may be
resolved by calculating the larger root (in absolute value) using the
quadratic formula and obtaining the smaller root by another means.
The larger root (in absolute value) can be obtained from the quadratic
formula by choosing the sign of
so that no subtraction
occurs. The smaller root (in absolute value) can be obtained by
observing that the product of the roots of a quadratic equation must
equal the constant term. So, for a general quadratic equation,
, the
product of the roots,
. Thus, the second
root may be obtained by division, circumventing the cancellation
problem in the previous exercise.
As mentioned previously, the calculation of
is another
possible source of severe cancellation. This is illustrated in the
following exercise.
As indicated in the previous exercise, cancellation may occur in
computing
when
. This time, however,
algebraic reformulation cannot be used to solve the problem.
Intermediate extended precision is another technique used to combat the
effects of severe cancellation. The quantity
is a
prime candidate for the use of extended precision because the result of
applying the square root function will bring the digits used in the
double precision calculation to significance. Converting this quantity
to single precision preserves the significant digits that would have
been inaccurate if the entire computation had been done in one precision
only.
Exercise 3.7 Modify your quadratic equation solver to use
double precision in calculating
. Fortran 90 has two
type conversion functions, real and double, to
convert numbers to single precision and double precision, respectively.