Skip to content

Latest commit

 

History

History
32 lines (22 loc) · 1.59 KB

7.md

File metadata and controls

32 lines (22 loc) · 1.59 KB

Exercise 1.7

The acceptance criteria 0.001 of our good-enough? test is not adjusted to be proportional to our guesses, if we have a huge guess/radicand, 0.001 might be too small, if we have a small guess/radicand, 0.001 might be too big and give inaccurate results.

Let's say if the radicand is 0.000000001, then a precision of 0.001 is of many orders of magnitude bigger than our radicand, the result which it computes will not give a good enough precision to our problem.

Further more, in real computers, numbers are represented in finite amount of memory, so when numbers get very large or when there are many decimal places, approximation needs to be made. In large numbers, the sqrt-iter function might be stuck in an infinite loop because the iteration runs out of memeory for real numbers and the guesses can never get lower than an absolute value that is arbitrary.

Instead of having a set value as the acceptance criteria, we can use a good-enough test computed based on the change as a proportion to the guess itself. This will test how much an iteration has changed the guess value and the new improved value of square root. If the value is small in proportion to the guess, we are close to the answer we seek.

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

(define (improve guess x)
  (average guess (/ x guess)))

(define (average x y)
  (/ (+ x y) 2))

(define (good-enough? guess x)
  (< (abs (/ (- (square guess) x) guess)) 0.00001))

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

(define (square x)
  (* x x))