24 lines
483 B
Racket
24 lines
483 B
Racket
(define (sqrt-iter guess guess-prev x)
|
|
(if (good-enough? guess guess-prev)
|
|
guess
|
|
(sqrt-iter (improve guess x) guess
|
|
x)))
|
|
|
|
(define (improve guess x)
|
|
(average guess (/ x guess)))
|
|
|
|
(define (average x y)
|
|
(/ (+ x y) 2))
|
|
|
|
(define (good-enough? guess guess-prev)
|
|
(< (abs (- guess guess-prev)) 0.001))
|
|
|
|
(define (sqrt-new x)
|
|
(if (= x 0) x (sqrt-iter 1.0 999 x)))
|
|
|
|
(sqrt-new 16)
|
|
(sqrt-new 1.0004)
|
|
(sqrt-new 1)
|
|
(sqrt-new 134895724398)
|
|
(sqrt-new 0)
|