1.7 Exercise 1.7

good-enough? is inadequate for computing the square roots of small numbers because it allows for too large a margin of error. The procedure terminates if (square guess) is within 0.001 of x. If 0.001 is large enough relative to x – or even larger than x – then guess will not necessarily be very close to the real answer.

As an example, (sqrt 0.0001) evaluates to 0.032. The square of this is 0.001024, which is within 0.001 of 0.0001, and is therefore an acceptable answer according to good-enough?, while it is actually not an acceptable answer at all.

good-enough? has problems with large square roots because the constant acceptable difference between the square of the guess of the square root and the actual number is too small to be realistically reached?

A better sqrt procedure using a good-enough? test based on observing the relative change between guesses and accepting a guess when this is small:

(define (sqrt x)
  (sqrt-iter x 1.0 0.0))

(The choice of starting guesses of 1 and 0 is arbitary.)

(define (sqrt-iter x guess previous-guess)
  (if (good-enough? guess previous-guess) guess
      (sqrt-iter x (improve guess x) guess)))
(define (good-enough? guess previous-guess)
  (< (abs (- guess previous-guess)) good-enough-factor))

I leave good-enough-factor to be defined somewhere else, to vary for testing.

Using these procedures and a good-enough-factor of 0.001, I can compute (sqrt 0.0001 to be approximately 0.010000714, which is very close to the correct answer. And with a good-enough-factor of 0.0000001, it evaluates to exactly 0.01.

TODO: Test very large numbers