1.6
由于scheme应用序求值的性质,该函数会陷入循环(一直计算 improve guess x)
1.7
值过小,0.001这个精度远远不够...
值过大,由于浮点数精度有限,(比如使用IEEE754浮点数标准, 32位浮点数表示123456789这样的数会有严重的精度丢失), 所以无法得出正确求两个较大的数的差.
1 (define (abs x) 2 (if (>= x 0) 3 x 4 (- x))) 5 6 (define (average a b) 7 (/ (+ a b) 2)) 8 9 (define (improve guess x) 10 (average guess 11 (/ x guess))) 12 13 (define (good-enough? guess x) 14 (< (/ (abs (- (improve guess x) 15 guess)) 16 guess) 17 0.01)) 18 19 (define (sqrt-iter guess x) 20 (cond ((good-enough? guess x) guess) 21 (else (sqrt-iter (improve guess x) x)) 22 )) 23 24 25 (define (sqrt x) 26 (sqrt-iter 1.0 x))
---> 原则上可以的,但是其实精度损失是肯定的...
1.8
1 (define (improve2 guess x) 2 (/ (+ (/ x 3 (* guess guess)) 4 (* 2 guess)) 5 3)) 6 7 (define (good-enough2? guess x) 8 (< (/ (abs (- (improve2 guess x) 9 guess)) 10 guess) 11 0.01)) 12 13 (define (cbrt-iter guess x) 14 (cond ((good-enough2? guess x) guess) 15 (else (cbrt-iter (improve2 guess x) 16 x)))) 17 18 (define (cbrt x) 19 (cbrt-iter 1.0 x))
时间: 2024-08-25 09:05:10