From: Ramakrishnan Muthukrishnan Date: Wed, 19 May 2010 13:11:04 +0000 (+0530) Subject: Solution to 1.39 X-Git-Url: https://git.rkrishnan.org/%5B/%5D%20/uri/flags/install-details.html?a=commitdiff_plain;h=01665dddeb50b8c741c00c991088431b149e18d9;p=sicp.git Solution to 1.39 --- diff --git a/src/sicp/ex1_39.clj b/src/sicp/ex1_39.clj new file mode 100644 index 0000000..d776be6 --- /dev/null +++ b/src/sicp/ex1_39.clj @@ -0,0 +1,30 @@ +(ns sicp.ex1_39 + (:use [sicp utils ex1_37] + [clojure.contrib test-is])) + +;; approximating tangent using lambert's cont fract +;; if you look at the formula, -xtan (x) = -x^2/(1-(x^2/3-x^2/5-.... )) +(defn tan-approximation [x len] + (* (/ -1.0 x) + (cont-frac (fn [k] (* -1.0 (square x))) + (fn [k] (- (* 2 k) 1)) + len))) + +(comment +user> (tan-approximation (/ Math/PI 4) 20) +1.0 +user> (Math/tan (/ Math/PI 4)) +0.9999999999999999 +user> (Math/tan (/ Math/PI 10)) +0.3249196962329063 +user> (tan-approximation (/ Math/PI 10) 20) +0.32491969623290634 +user> (tan-approximation Math/PI 20) +-1.4135798584282297E-16 +user> (Math/tan Math/PI) +-1.2246467991473532E-16 +user> (Math/tan (/ Math/PI 2.0)) +1.633123935319537E16 +user> (tan-approximation (/ Math/PI 2) 20) +; Evaluation aborted. +)