2018-12-26 牛顿法求解平方根

用牛顿法求平方根

(define (average x y)
    (/ (+ x y) 2)
)

(define (improve guess x)
    (average guess (/ x guess))
)

(define (sqrt-iter guess x)
    (if (good-enough? guess x)
        guess
        (sqrt-iter(improve guess x)
            x
        )
    )
)

(define square x
    (* x x)
)

(define (good-enough? guess x)
    (< (abs (- (square guess) x ) 0.001 ))
)

(define (sqrt x)
    (sqrt-iter 1.0 x)
)

你可能感兴趣的:(2018-12-26 牛顿法求解平方根)