CPSC/MATH 402 Assignment #2
Due _________________________________________
Computer problem #1.10 on page 46 of the text asks you to write
a "robust" program for solving a quadratic equation accurately. This
means that your program should check for special cases (a = 0, for
example), work for complex roots (a negative discriminant), and
in other cases, it should
choose the most accurate method of computing the roots
(to avoid overflow, unnecessary underflow, and cancellation error).
For this assignment, write the program as described in the text
but so you can see that the way you implement the quadratic
formula really matters also compute and print the roots you
would get using both the standard formula and the alternative
formula. So, your program should do the following:
- Check for the special cases and print out roots in those situations.
A reminder: if the discriminant b2 - 4ac is negative the roots are
complex with real part -b/2a. The square root of the absolute value
of the discriminant divided by 2a is the imaginary part of one root;
the negative of that is the imaginary part of the other root.
- Compute both roots using the standard formula; print the results
appropriately labeled.
- Compute both roots using the alternative formula; print the
results appropriately labeled.
- Compute both roots the "best" way -- have the program decide which is best
for each root based on values of a, b, and c. Print the results.
Test your program thoroughly. The book gives some test values for you
to use (by the way, you can input values such as 5 times 10154
using exponential notation -- 5e154).
Hand In
- A printout of your source code.
- Email to ingram@roanoke.edu
a copy of your program.
- Answers to the following questions:
- Show (by hand!) that 10n and 10-n are the exact
roots when a = 1, b = -(10n + 10-n), c = 1.
- Test your program for the above input with different values of
n. For example, b = -10000.0001 (104 + 10-4).
What is the smallest positive value of n that gives some error in
computing using the standard formula (it should also
give error in the alternative formula)? Explain what is happening to cause
the problem (be specific -- don't just say round-off error or cancellation -
state where it is occurring in the calculation and why it is happening
for that particular n).
- For the example above, what is the smallest positive n for which
you get 0 for one of the roots when using the standard formula? Explain
why this is happening.
- The input values of 1 -4 3.999999 suggested in the text are
supposed to illustrate another phenomenon. They would in single
precision but not in double precision. Double precision should get
the correct answer no matter which implementation you use. (NOTE:
the input is a = 1, b = -4, c = 4 - 10-n where
n = 6. The exact solutions are 2 + 10-n/2 and
2 - 10-n/2 so for n=6, that is 2.001 and 1.999.) Try other
values of n and find
the smallest positive value of n (the number of 9's) that does
not give the correct answer. Where is the problem in the calculation
for this value of n and why is even the "best" method wrong?