-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise1-07.lisp
30 lines (30 loc) · 1.06 KB
/
exercise1-07.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/guile
!#
(define (square x)
(* x x))
(define (average x y)
(/ (+ x y) 2))
(define (improve guess x)
(average guess (/ x guess)))
(define (new-sqrt-iter guess x good-enough?)
(if (good-enough? guess x)
guess
(new-sqrt-iter (improve guess x) x good-enough?)))
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (new-good-enough? guess x)
(< (abs (- (square guess) x)) (* 0.001 x)))
(display (rationalize (new-sqrt-iter 1 5 good-enough?) 0.0000001))
(newline)
(display (rationalize (new-sqrt-iter 1 5 new-good-enough?) 0.0000001))
(newline)
(display (rationalize (new-sqrt-iter 1 50000 good-enough?) 0.0000001))
(newline)
(display (rationalize (new-sqrt-iter 1 50000 new-good-enough?) 0.0000001))
(newline)
(display (rationalize (new-sqrt-iter 1 0.000001 good-enough?) 0.0000001))
(newline)
(display (rationalize (new-sqrt-iter 1 0.000001 new-good-enough?) 0.0000001))
# the new function works better on small numbers, since the
# epsilon is scaled down proportionally, but worse on large
# numbers, since e.g. 0.001 * 50000 = 50 >>> 0.001