2 (:use [sicp utils ex1_37]
3 [clojure.contrib test-is]))
5 ;; approximating tangent using lambert's cont fract
6 ;; if you look at the formula, -xtan (x) = -x^2/(1-(x^2/3-x^2/5-.... ))
7 (defn tan-approximation [x len]
9 (cont-frac (fn [k] (* -1.0 (square x)))
10 (fn [k] (- (* 2 k) 1))
14 user> (tan-approximation (/ Math/PI 4) 20)
16 user> (Math/tan (/ Math/PI 4))
18 user> (Math/tan (/ Math/PI 10))
20 user> (tan-approximation (/ Math/PI 10) 20)
22 user> (tan-approximation Math/PI 20)
23 -1.4135798584282297E-16
24 user> (Math/tan Math/PI)
25 -1.2246467991473532E-16
26 user> (Math/tan (/ Math/PI 2.0))
28 user> (tan-approximation (/ Math/PI 2) 20)