From 01665dddeb50b8c741c00c991088431b149e18d9 Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <vu3rdd@gmail.com>
Date: Wed, 19 May 2010 18:41:04 +0530
Subject: [PATCH] Solution to 1.39

---
 src/sicp/ex1_39.clj | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
 create mode 100644 src/sicp/ex1_39.clj

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.  
+)
-- 
2.45.2