next up previous contents
Next: Complete Example Programs Up: Advanced Numerical Experiments Previous: Interval Arithmetic

Subtleties in Solving a Quadratic Equation

Solving a quadratic equation at first glance seems to be a trivial calculation using the quadratic formula,

exercise425

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. gifThe exact roots rounded to 6 digits are 0.0399675 and 12.3600.gif

The above exercise illustrates an important numerical problem called cancellation or loss of significance which manifests itself when subtracting values of nearly equal magnitude.gif 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, tex2html_wrap_inline893 and tex2html_wrap_inline895, 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 tex2html_wrap_inline899 . 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 tex2html_wrap_inline905 .gif This cancellation occurs when 4ac is small relative to tex2html_wrap_inline907 , so that tex2html_wrap_inline911. 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 tex2html_wrap_inline899 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, tex2html_wrap_inline913, the product of the roots, exercise437 . Thus, the second root may be obtained by division, circumventing the cancellation problem in the previous exercise.

exercise441

As mentioned previously, the calculation of tex2html_wrap_inline899 is another possible source of severe cancellation. This is illustrated in the following exercise.

tex2html_wrap_inline925

As indicated in the previous exercise, cancellation may occur in computing tex2html_wrap_inline899 when exercise448 . 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 tex2html_wrap_inline899 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.gif

Exercise 3.7 Modify your quadratic equation solver to use double precision in calculating tex2html_wrap_inline899. Fortran 90 has two type conversion functions, real and double, to convert numbers to single precision and double precision, respectively.


next up previous contents
Next: Complete Example Programs Up: Advanced Numerical Experiments Previous: Interval Arithmetic