Log into the Linux system to do this exercise.
This handout and some programs are on-line --
go to *http://cs.roanoke.edu/Spring2004/CPSC402A*
(use the Mozilla browser) and
click on the link to lab 1.

- The program tenth.cc illustrates the
hazards of testing for equality of two floating point numbers.
**Do the following:**- Save the program to your directory (you can do this by clicking
on the link to the program and then using
the File/Save As feature in the browser -- or copy and paste).

- Compile and run the program. The commands are:
g++ tenth.cc -o ten

to compile and put the executable in a file named*ten*(you may choose another name for the executable!). To run type./ten

(you need the ./ to refer to your current directory). Alternately, use the commandg++ tenth.cc

to compile then*./a.out*to run (*a.out*is the default name for executables in UNIX). - Run the program with the following input: (a) n = 8; (b) n = 6;
(c) n = 4096; (d) n = 10. (Remember that CTRL-C means break in UNIX!!)
What happened and why was the behavior
different for different input?

- Change the loop control to
*total > epsilon*(epsilon is already computed -- it is the approximation to machine epsilon which is a measure of machine precision -- discussed in Section 1.3.5 of the text). Rerun the program for the input above plus other values. Does it work correctly?

- Save the program to your directory (you can do this by clicking
on the link to the program and then using
the File/Save As feature in the browser -- or copy and paste).
- Suppose you need to evaluate the function f(x) = (x - sin(x))/x^3 for
values of x close to 0. The program sine.cc
evaluates the function for values of x starting with x = 1 then
dividing by 10 each iteration (so x becomes 0.1, then 0.01, 0.001, etc).
The user enters the number of iterations.
According to L'Hopital's rule, the limit of f(x) as x approaches 0 is 1/6 so
the values should be close to 1/6 as x gets close to 0.
- Save the program, compile it and run it for 16 iterations. What happened? Why do you think the program behaved as it did?
- Find a second way of calculating (or approximating) f(x) and add code to the program to perform the calculation and print out the result in a third column (leave the original calculation -- declare a new variable). Run your program to make sure it does a better job (if it doesn't try again!!!).

- Do computer problem #1.6(b) on page 45. Part (a) gives two ways to compute
a sequence of equally spaced points in an interval [a, b]. For part(b) you
are to write a program to implement these to see which is best. You may
use either Java or C++.

- The program poly.cc
is essentially Computer Problem 1.14
on page 47 (it just prints values rather than plotting them).
Save it to your directory.
- Read the problem and the program then compile and run the program. Study the output. Notice the difference in answers for the two different calculations. Also notice the computed value of (x - 1)^6 for x = 1.
- The later problem is related to what you were supposed to understand
from doing #1.6. Modify the program using what you learned from #1.6
so the answer for x = 1 is correct.

- The expanded form of the polynomial did not fare so well in the
above calculations. In general, a polynomial should not be evaluated
in expanded form but not all polynomials have nice factored forms.
The recommended method of evaluation (in the absence of a nice
factorization) is called Horner's
method (which your book tells you about on page 316). Horner's method
uses a nested evaluation. See the general formula on page 316. For
this particular polynomial, Horner's method is
1 + x(-6 + x(15 + x(-20 + x(15+ x(-6 + x))))

Add code to the program to evaluate the polynomial this way (assign to poly3) and print out the result (as a fourth column).

**HOMEWORK DUE Tuesday, January 27, 2004: **

- Why did the input 10 cause problems in the
*tenth.cc*program but 8 and 4096 worked as expected? - For Computer Problem 1.6,
- What did you use as an example to illustrate the difference between the two methods?
- How different were the results (compute the largest absolute error and the largest relative error you got)?
- Which method was better?
- Why do you think it was better?

- In Computer Problem #1.14, discuss the behavior for the three
different methods for computing (x - 1)^6 for x near 1. Explain
and compare the behavior of the three methods as follows.
- For what values of x do the three methods give very similar answers and for what values do the methods give very different answers?
- Compare the methods -- which is best? which is worst? How do you know?
- Were the different answers close enough that any one of the methods would have been acceptable to use?
- Use the formula for approximating the condition number of a function to get a formula for the condition number of f(x) = (x - 1)^6.
- Find the condition number for a value of at least one x for which the results of the three computations were very different.
- Find the condition number for a value of at least one x for which the results of the three computations were close.
- Does the condition number help explain the observed behavior of the three methods of computing f(x)? Explain.

**Printing and Getting Output: **To print use the *nenscript*
command printing in two columns (uses less paper!!!). The command is

nenscript -2rG filenamewhere "filename" is the name of the file you want to print.

To get output to examine either use redirection of output or create a
script file. For example, to put the output of tenth.cc in a file named
tenth.out you can run the program with the following command (assuming
the executable is named *ten*:

./ten > ten.outThis sends all output, including the prompts, to the file ten.out. You need to type in the input without the benefit of the prompts.

To put the output in a logfile (or script) named ten.out you would issue the following sequence of commands:

script ten.out ./ten exitWith either technique the output file can be examined using the