Wednesday, April 23, 2008

Exercise 1.7

"The good-enough? test used in computing square roots will not be very effective for finding the square roots of very small numbers." If x is much smaller than 0.001, good-enough? will return true for numbers that are significantly off. For example, it will return true if x is 0.000001 and guess is 0.0001, (abs (- (square guess) x)) = 0.00000999... and good-enough? returns true, even though the guess is off by a factor of 100.

"Also, in real computers, arithmetic operations are almost always performed with limited precision. This makes our test inadequate for very large numbers." Limited precision means that there is a limit to the number of significant figures in our calculations. Suppose the limit is 7 significant figures, x is 10000000000000000000000000000000000000000000000000000 and guess is 100000000000000000001000000. (abs (- (square guess) x)) = 0 and good-enough? returns true, even though guess is off by 1000000.

(define (good-enough? guess old-guess)
(< (abs (- guess old-guess)) (* 0.001 guess)))

(define (sqrt-iter guess x)
(if (good-enough? guess (improve guess x))
(improve guess x)
(sqrt-iter (improve guess x) x)))

No comments: