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 FormType is (Sqrt2, Napier, Pi);
type Floaty is digits 15;
package FIO is new Ada.Text_IO.Float_IO (Floaty);
 
Specification and implementation of a generic Cont_Frac package:
procedure GetCoefs (form : FormType; n : Natural;
<lang Ada>generic
coefA : out Natural; coefB : out Natural)
type Float_Y is begindigits <>;
with function A(J: Natural) return Float_Y;
case form is
with function B(J: Positive) return Float_Y;
when Sqrt2 =>
package Cont_Frac is
if n > 0 then coefA := 2; else coefA := 1; end if;
coefB := 1;
when Napier =>
if n > 0 then coefA := n; else coefA := 2; end if;
if n > 1 then coefB := n - 1; else coefB := 1; end if;
when Pi =>
if n > 0 then coefA := 6; else coefA := 3; end if;
coefB := (2*n - 1)**2;
end case;
end GetCoefs;
 
function Calc (form : FormType; n N: Natural) return Floaty isFloat_Y;
 
A, B : Natural;
end Cont_Frac;</lang>
Temp : Floaty := 0.0;
 
<lang Ada>package body Cont_Frac is
 
function Calc(N: Natural) return Float_Y is
Temp : coefBFloat_Y := 10.0;
begin
for niI in reverse Natural range 1 .. nN loop
GetCoefsTemp := B(form,I) ni,/ (A,(I) B+ Temp);
Temp := Floaty (B) / (Floaty (A) + Temp);
end loop;
GetCoefsreturn A(form, 0,) A,+ B)Temp;
return Floaty (A) + Temp;
end Calc;
 
end Cont_Frac;
begin
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;
end ContFract;
</lang>
 
The main program:
<lang Ada>with Ada.Text_IO; usewith Ada.Text_IOCont_Frac;
procedure ContFractMain_Cont_Frac is
 
type FloatyFlt is digits 15;
 
-- 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);
 
when-- Napier =>
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);
 
when-- Pi =>
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 FIO is new Ada.Text_IO.Float_IO (FloatyFlt);
package TIO renames Ada.Text_IO;
 
begin -- compute the constants and output them
FIO.Put (Sqrt_2.Calc (Sqrt2, 200), Exp => 0); TIO.New_Line;
FIO.Put (Napier.Calc (Napier, 200), Exp => 0); TIO.New_Line;
FIO.Put (Pi.Calc (Pi, 10000), Exp => 0); TIO.New_Line;
end Main_Cont_Frac;</lang>
 
{{out}}
<pre> 1.41421356237310
2.71828182845905</pre>
3.14159265358954</pre>
 
=={{header|Axiom}}==
Anonymous user