|CIT 591 More About the Second Java
CIT 591, David Matuszek, Fall 2002
First: My sincere apologies; I had the algorithm right the first time. It should go like this:
numerator = 1; temp = 0; denominator = numerator; numerator = (numerator * last-unused-denominator) + temp; temp = denominator;
Basically, what happened was this: After the program was working, I must have made some "trivial" change to it that I forgot about (something I've warned you against). When I went to post the inner loop statements in the program, I discovered that my program wasn't working correctly; it gave the inverse of the correct answer. I thought I must have been reading the results incorrectly earlier (I had not been printing them out in a neat "fraction" format), so I "fixed" the program.
The error was not in the inner loop (for which the correct code is given above); it was in computing the denominators. I had one too many zeros in the array of denominators, so that instead of looking like
0 1 1 2 1 1 208332
(for 0.5833333), the array looked like:
0 0 1 1 2 1 1 208332
If you look at the math, the effect of having this extra zero in front is to
invert the final answer, so that instead of
7/12, you get
12/7. I "corrected" my mistake in the wrong place.
Students have been asking what the output should look like. That's basically up to you, so long as you include all the information that I asked for. Here's what mine looks like (for pi):
Program by David Matuszek Input decimal number to be converted: 3.141592653589793 All computed denominators: 3 7 15 1 292 1 1 1 2 1 3 1 Using these 1 denominators: 3 fraction = 3/1, value = 3.0, goodness = 7, error = 4% Using these 2 denominators: 3 7 fraction = 22/7, value = 3.142857142857143, goodness = 15, error = 0% Using these 3 denominators: 3 7 15 fraction = 333/106, value = 3.141509433962264, goodness = 1, error = 0% Using these 4 denominators: 3 7 15 1 fraction = 355/113, value = 3.1415929203539825, goodness = 292, error = 0%
There seems to be a lot of confusion about the fact that the program is supposed to compute a sequence of approximations. The question I keep hearing is, "What is N?"
In the section of the assignment labeled The Math, I talked about how
to start with the six denominators
turn this into the fraction
7/12. Along the way I computed the
7/12. Except for the last one, these are not
approximations to 0.5833333; they are just fractions that occur during evaluation
of the expression. You might want to see these fractions for debugging
purposes, but I don't want to see them.
You could also compute a fractional approximation to 0.5833333 by using
only five denominators
. You would
4/7, which is not as good an approximation to 0.5833333 (it's
0.5714285). Or your could use four denominators and get
(which is 0.6). You could use any number of denominators, and the more
denominators you use, the better the approximation (up to the limits of the
The assignment says to use one denominator, compute and print the results (fraction, decimal value, goodness, and percent error), then to use two denominators and print your results, then use three denominators and print your results, etc. N is the number of denominators to use. Each approximation will require a complete computation. Each approximation will be more accurate than the preceding one. Stop when either (1) you have used 12 denominators, or (2) the next unused denominator is greater than 20, whichever comes first.