23 lines
453 B
Scheme
23 lines
453 B
Scheme
|
(define (cuberoot_iter new_guess last_guess x)
|
||
|
(if (good_enough? new_guess last_guess)
|
||
|
new_guess
|
||
|
(cuberoot_iter (improve new_guess x) new_guess x)
|
||
|
)
|
||
|
)
|
||
|
|
||
|
(define (improve guess x)
|
||
|
(/ (+ (/ x (* guess guess)) (* 2 guess)) 3))
|
||
|
|
||
|
|
||
|
(define (good_enough? guess last_guess)
|
||
|
(< (absolute (- guess last_guess)) 0.000000000000001))
|
||
|
|
||
|
(define (absolute x)
|
||
|
(if (< x 0)
|
||
|
(- 0 x)
|
||
|
x))
|
||
|
|
||
|
(define (cbrt x) (cuberoot_iter (/ x 2.0) x x))
|
||
|
|
||
|
(print (cbrt 27))
|