SICP 全笔记

Exercise 1.39. A continued fraction representation of the tangent function was published in 1770 by the German mathematician J.H. Lambert:

where x is in radians. Define a procedure (tan-cf x k) that computes an approximation to the tangent function based on Lambert’s formula. K specifies the number of terms to compute, as in exercise 1.37.

与练习 1.38 一样,我们需要把这个 tan 的式子稍作改动,以适应我们的 cont-frac 过程:

$$-x * tanx = \frac{-x^2}{1+{\frac{-x^2}{3+\frac{-x^2}{\dots}}}} $$ 于是我们可以如下定义 tan-cf(需要注意的是 cont-frac 的分子在这里是 $-x^2$ )。

(load "37-cont-frac.scm")

(define (tan-cf x k)
  (let ((xx (* x x)))
    (/ (cont-frac (lambda (i) (- 0 xx))
                  (lambda (i) (- (* 2 i) 1))
                  k)
       (- 0 x))))

(tan-cf (/ 3.1415926 4) 100)
;Value: .9999999732051038
; the built-in tan will produce
; 1 ]=> (tan (/ 3.1415926 4))
;Value: .9999999732051039