-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex_1_07.clj
28 lines (23 loc) · 1.02 KB
/
ex_1_07.clj
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
(ns sicp.chapter-1.part-1.ex-1-07
(:require
[sicp.chapter-1.part-1.book-1-1 :as b11]))
; Exercise 1.7
; The `good-enough?` test used in computing square roots will not be very effective
; for finding the square roots of very small numbers.
; Also, in real computers, arithmetic operations are almost always performed with 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.
; 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.
; Design a square-root procedure that uses this kind of end test.
; Does this work better for small and large numbers?
; Imporovments
(defn good-enough-v2?
[guess1 guess2]
(< (abs (- guess1 guess2)) 0.001))
(defn sqrt-iter-v2
[guess x]
(if (good-enough-v2? guess (b11/improve guess x))
guess
(sqrt-iter-v2 (b11/improve guess x) x)))