Continued fraction: Difference between revisions

no edit summary
m (→‎{{header|NetRexx}}: mention better computation at Rexx)
No edit summary
Line 189:
</pre>
 
=={{header|Common Lisp}}==
{{trans|C++}}
<lang lisp>(defun estimate-continued-fraction (generator n)
(let ((temp 0))
(loop for n1 from n downto 1
do (multiple-value-bind (a b)
(funcall generator n1)
(setf temp (/ b (+ a temp)))))
(+ (funcall generator 0) temp)))
 
(format t "sqrt(2) = ~a~%" (coerce (estimate-continued-fraction
(lambda (n)
(values (if (> n 0) 2 1) 1)) 20)
'double-float))
(format t "napier's = ~a~%" (coerce (estimate-continued-fraction
(lambda (n)
(values (if (> n 0) n 2)
(if (> n 1) (1- n) 1))) 15)
'double-float))
 
(format t "pi = ~a~%" (coerce (estimate-continued-fraction
(lambda (n)
(values (if (> n 0) 6 3)
(* (1- (* 2 n))
(1- (* 2 n))))) 10000)
'double-float))
</lang>
{{out}}
<pre>sqrt(2) = 1.4142135623730947d0
napier's = 2.7182818284590464d0
pi = 3.141592653589543d0</pre>
=={{header|D}}==
<lang d>import std.typecons;