24 lines
508 B
Racket
24 lines
508 B
Racket
(define (square x) (* x x))
|
|
|
|
(define (cubic-iter guess guess-prev x)
|
|
(if (good-enough? guess guess-prev)
|
|
guess
|
|
(cubic-iter (improve guess x) guess
|
|
x)))
|
|
|
|
(define (improve guess x)
|
|
(average (* 2 guess) (/ x (square guess))))
|
|
|
|
(define (average x y)
|
|
(/ (+ x y) 3))
|
|
|
|
(define (good-enough? guess guess-prev)
|
|
(< (abs (- guess guess-prev)) 0.001))
|
|
|
|
(define (cubic-root x)
|
|
(if (= x 0) x (cubic-iter 1.0 999 x)))
|
|
|
|
(cubic-root 1892379832.0)
|
|
(cubic-root 8.0)
|
|
(cubic-root 27.0)
|