)
;; 1.1.7 Square root finding using Newton's method
-(defn sqrt-iter [guess x]
- (if (good-enough? guess x)
- guess
- (sqrt-iter (improve guess x)
- x)))
+(defn average [x y]
+ (/ (+ x y) 2))
(defn improve [guess x]
(average guess (/ x guess)))
-(defn average [x y]
- (/ (+ x y) 2))
-
(defn good-enough? [guess x]
(< (abs (- (square guess) x)) 0.001))
+(defn sqrt-iter [guess x]
+ (if (good-enough? guess x)
+ guess
+ (sqrt-iter (improve guess x)
+ x)))
+
(defn sqrt [x]
(sqrt-iter 1.0 x))
;; limited precision. This makes our test inadequate for very large numbers.
;; Explain these statements, with examples showing how the test fails for small
;; and large numbers.
-user> (sqrt (square 0.001))
-0.031260655525445276
-user> (sqrt (square 0.2))
-0.20060990407779591
-user> (sqrt (square 0.01))
-0.03230844833048122
-user> (sqrt (square 0.02))
-0.0354008825558513
-user> (sqrt (square 10))
-10.000000000139897
-user> (sqrt (square 100))
-100.00000025490743
-user> (sqrt (square 200))
-200.000000510076
-user> (sqrt (square 2))
-2.0000000929222947
-user> (sqrt (square 0.1))
-0.10032578510960607
-user> (sqrt (square 0.01))
-0.03230844833048122
-user> (sqrt (square 10000))
-10000.0
-user> (sqrt (square 20000))
-20000.0
-user> (sqrt (square 200000))
-200000.0
-user> (sqrt (square 20000000))
-2.0E7
-user> (sqrt (square 20000000000))
-2.0E10
-user> (sqrt (square 200000.012))
-200000.012
-user> (sqrt (square 2000000.123))
-2000000.123
-user> (sqrt (square 200000000.123))
-2.00000000123E8
-user> (sqrt (square 2000000000.123))
-2.000000000123E9
-user> (sqrt (square 20000000000.123))
-2.0000000000123E10
-user> (sqrt (square 2000000000000.123))
-2.000000000000123E12
-
+(comment
+ user> (sqrt (square 0.001))
+ 0.031260655525445276
+ user> (sqrt (square 0.2))
+ 0.20060990407779591
+ user> (sqrt (square 0.01))
+ 0.03230844833048122
+ user> (sqrt (square 0.02))
+ 0.0354008825558513
+ user> (sqrt (square 10))
+ 10.000000000139897
+ user> (sqrt (square 100))
+ 100.00000025490743
+ user> (sqrt (square 200))
+ 200.000000510076
+ user> (sqrt (square 2))
+ 2.0000000929222947
+ user> (sqrt (square 0.1))
+ 0.10032578510960607
+ user> (sqrt (square 0.01))
+ 0.03230844833048122
+ user> (sqrt (square 10000))
+ 10000.0
+ user> (sqrt (square 20000))
+ 20000.0
+ user> (sqrt (square 200000))
+ 200000.0
+ user> (sqrt (square 20000000))
+ 2.0E7
+ user> (sqrt (square 20000000000))
+ 2.0E10
+ user> (sqrt (square 200000.012))
+ 200000.012
+ user> (sqrt (square 2000000.123))
+ 2000000.123
+ user> (sqrt (square 200000000.123))
+ 2.00000000123E8
+ user> (sqrt (square 2000000000.123))
+ 2.000000000123E9
+ user> (sqrt (square 20000000000.123))
+ 2.0000000000123E10
+ user> (sqrt (square 2000000000000.123))
+ 2.000000000000123E12
+ )
;; An alternative strategy for implementing good-enough? is to watch how guess
;; changes from one iteration to the next and to stop when the change is a very
;; small fraction of the guess.
(defn sqrt [x]
(sqrt-iter x 1.0 x))
-
+(comment
user> (sqrt (square 0.01))
0.010000000025490743
user> (sqrt (square 0.001))
3.000000001396984
user> (sqrt 81)
9.000000000007091
-
+)
;; exercise 1.8: cube root
(defn cube [x]
(* x x x))
(defn cuberoot [x]
(cubert-iter x 1.0 x))
+(comment
user> (cuberoot (cube 2))
2.000000000012062
user> (cuberoot (cube 10))
user> (cuberoot (cube 0.0001))
1.000000000000001E-4
user>
+)
+;; section 1.1.8
+;; hiding the non-public procedure definitions
+(defn- sqrt-iter [guess x]
+ (if (good-enough? guess x)
+ guess
+ (sqrt-iter (improve guess x)
+ x)))
+
+(defn- improve [guess x]
+ (average guess (/ x guess)))
+
+(defn- average [x y]
+ (/ (+ x y) 2))
+
+(defn- good-enough? [guess x]
+ (< (abs (- (square guess) x)) 0.001))
+
+(defn sqrt [x]
+ (sqrt-iter 1.0 x))