Anonymous user
Continued fraction: Difference between revisions
→{{header|Ada}}: -- revised solution using a (generic) package, to become more Ada-typical
m (→{{header|Perl}}: yet another simplification) |
(→{{header|Ada}}: -- revised solution using a (generic) package, to become more Ada-typical) |
||
Line 18:
=={{header|Ada}}==
<lang Ada>with Ada.Text_IO; use Ada.Text_IO;▼
procedure ContFract is▼
type Floaty is digits 15;▼
package FIO is new Ada.Text_IO.Float_IO (Floaty);▼
Specification and implementation of a generic Cont_Frac package:
<lang Ada>generic
type Float_Y is
with function A(J: Natural) return Float_Y;
with function B(J: Positive) return Float_Y;
package Cont_Frac is
coefB := 1;▼
when Napier =>▼
when Pi =>▼
function Calc
end Cont_Frac;</lang>
<lang Ada>package body Cont_Frac is
function Calc(N: Natural) return Float_Y is
begin
for
end loop;
end Calc;
end Cont_Frac;
FIO.Put (Calc (Sqrt2, 200), Exp => 0); New_Line;▼
FIO.Put (Calc (Napier, 200), Exp => 0); New_Line;▼
FIO.Put (Calc (Pi, 10000), Exp => 0); New_Line;▼
</lang>
The main program:
-- packages Sqrt_2, Napier and Pi including coefficient-functions
-- Sqrt_2
function S2_A(J: Natural) return Flt is (if J > 0 then 2.0 else 1.0);
function S2_B(J: Positive) return Flt is (1.0);
package Sqrt_2 is new Cont_Frac(Flt, S2_A, S2_B);
function Na_A(J: Natural) return Flt is (if J > 0 then Flt(J) else 2.0);
function NA_B(J: Positive) return Flt is (Flt(if J > 1 then J-1 else 1));
package Napier is new Cont_Frac(Flt, Na_A, Na_B);
function Pi_A(J: Natural) return Flt is (if J > 0 then 6.0 else 3.0);
function Pi_B(J: Positive) return Flt is (Flt((2*J-1)**2));
package Pi is new Cont_Frac(Flt, Pi_A, Pi_B);
-- packages to output the results
package TIO renames Ada.Text_IO;
begin -- compute the constants and output them
end Main_Cont_Frac;</lang>
{{out}}
<pre> 1.41421356237310
2.71828182845905
3.14159265358954</pre>
=={{header|Axiom}}==
|