立方根の計算
Posted feedbacks - Scheme
SICPそのまんま(exercise 1.8) ただし、exercise 1.7 を考慮しているので 1.0e-30のような小さい数でも対応できる
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 29 30 | (define (cubic-root-iter old-guess new-guess x)
(if (cubic-good-enough? old-guess new-guess)
new-guess
(cubic-root-iter new-guess (cubic-improve new-guess x) x)))
(define (cubic-improve guess x)
(+ (/ x (* 3 (square guess))) (/ (* 2 guess) 3)))
(define (cubic-good-enough? old-guess new-guess)
(< (abs (- (/ old-guess new-guess) 1.0)) 1.0e-12))
(define (cubic-root x)
(cubic-root-iter 1.0 x x))
(define (square x) (* x x))
(define (cube x) (* x x x))
#|
gosh> (cube (cubic-root 10.0))
10.000000000000002
gosh> (cube (cubic-root 100.0))
100.00000000000003
gosh> (cube (cubic-root 1000.0))
1000.0
gosh> (cube (cubic-root 1.0e-30))
1.0000000000000006e-30
gosh> (cube (cubic-root 1.0e-60))
9.999999999999998e-61
gosh>
|#
|


にしお
#3411()
Rating1/1=1.00
ただし、このお題の趣旨は実数区間での探索なので、 立方根関数があっても使ってはいけません。 指数関数と対数関数も禁止します。
Pythonで表現した入出力の例:
[ reply ]