Continued fraction: Difference between revisions
Content deleted Content added
m →{{header|Perl}}: missing semcolon |
OCaml example |
||
Line 629: | Line 629: | ||
See Rexx for a better computation |
See Rexx for a better computation |
||
=={{header|OCaml}}== |
|||
<lang ocaml>let pi = 3, fun n -> ((2*n-1)*(2*n-1), 6) |
|||
and nap = 2, fun n -> (max 1 (n-1), n) |
|||
and root2 = 1, fun n -> (1, 2) in |
|||
let eval (i,f) k = |
|||
let rec frac n = |
|||
let a, b = f n in |
|||
(float a) /. ((float b) +. |
|||
if n >= k then 0.0 else frac (n+1)) in |
|||
(float i) +. frac 1 in |
|||
Printf.printf "sqrt(2)\t= %.15f\n" (eval root2 1000); |
|||
Printf.printf "e\t= %.15f\n" (eval nap 100); |
|||
Printf.printf "pi\t= %.15f\n" (eval pi 1000);</lang> |
|||
Output: |
|||
<pre>sqrt(2) = 1.414213562373095 |
|||
e = 2.718281828459046 |
|||
pi = 3.141592653340542</pre> |
|||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |