-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathch1-1-7-square-roots.scm
55 lines (31 loc) · 1.2 KB
/
ch1-1-7-square-roots.scm
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
; ----------------------------------------------------------
; FINDING SQUARE ROOTS
; ----------------------------------------------------------
; Mathematically, we can define WHAT IS a square root:
; _/x = the y such that y>=0 and y^2 = x
; BUT Programmatically, we need a way - i.e. HOW TO arrive
; at a square-root, with reasonable accuracy.
; Newton's method of successive approximation is 1 such way.
; Initially GUESS that some y is the square root of x
; Now find a BETTER GUESS closer to the square root by
; averaging guess y with x/y till Good Enough accuracy
; is achieved.
; ----------------------------------------------------------
; The basic strategy, expressed as a Procedure:
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x)
x)))
; New guess: Avg guess with quotient of (radicand x)/(guess)
(define (improve guess x)
(average guess (/ x guess)))
; where
(define (average x y)
(/ (+ x y) 2))
; and good enough accuracy is (let's say) 0.0001
; [Note: this will fail for sqrt of very small numbers]
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.0001))
(define (sqrt x)
(sqrt-iter 1.0 x))