Euler's constant 0.5772...: Difference between revisions

m
Moved Algol W to the right place
(Added Algol W)
m (Moved Algol W to the right place)
 
Line 30:
 
__TOC__
 
=={{header|Ada}}==
Three solutions here: the Vacca series, then two useful approximations for generally-used Ada types
Float and Long_Float.
<syntaxhighlight lang="ada">
with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;
with Ada.Numerics.Long_Elementary_Functions; use Ada.Numerics.Long_Elementary_Functions;
with Ada.Text_IO; use Ada.Text_IO;
 
procedure Eulers_Constant is
 
function Euler_Vacca (Iterations : Integer) return Long_Float is
Gamma : Long_Float := 1.0;
Term : Long_Float;
Power : Long_Integer;
Sign : Long_Float;
begin
Gamma := 0.5 - (1.0 / 3.0);
for I in 2 .. Iterations loop
Power := 2 ** Natural (I);
Sign := -1.0;
Term := 0.0;
for Domin in Power .. (2 * Power - 1) loop
Sign := - (Sign);
Term := Term + Sign / Long_Float (Domin);
end loop;
Gamma := Gamma + (Long_Float (I) * Term);
end loop;
return Gamma;
end Euler_Vacca;
 
-- Ada Float type is IEEE 754 32-bit, giving 9 decimal digits of precision
Euler_Castellanos_Float : constant Float :=
(((80.0 ** 3) + 92.0) /
(61.0 ** 4)) ** (1.0 / 6.0);
 
-- Ada Long_Float type is IEEE 754 32-bit, giving 14 decimal digits of precision
Euler_Castellanos_Long_Float : constant Long_Float :=
(990.0 ** 3 - 55.0 ** 3 - 79.0 ** 2 - 16.0) /
70.0 ** 5;
 
Iters : Integer;
begin
Put_Line ("Its. Vacca");
Iters := 2;
while Iters <= 32 loop
Put_Line (Iters'Image & " " & Euler_Vacca (Iters)'Image);
Iters := Iters + 2;
end loop;
Put_Line ("Castellanos approximation for standard Float (9 digits): " & Euler_Castellanos_Float'Image);
Put_Line ("Castellanos approximation for Long Float (14 digits): " & Euler_Castellanos_Long_Float'Image);
end Eulers_Constant;
</syntaxhighlight>
{{out}}
<pre>
Its. Vacca
2 3.14285714285714E-01
4 4.82164184398886E-01
6 5.45853770405349E-01
8 5.67441138957738E-01
10 5.74285301882304E-01
12 5.76361123043496E-01
14 5.76971520706463E-01
16 5.77147000098518E-01
18 5.77196591397621E-01
20 5.77210419691580E-01
22 5.77214234389975E-01
24 5.77215277471336E-01
26 5.77215560593404E-01
28 5.77215636961856E-01
30 5.77215657450952E-01
32 5.77215662922473E-01
Castellanos approximation for standard Float (9 digits): 5.77216E-01
Castellanos approximation for Long Float (14 digits): 5.77215664901529E-01
</pre>
 
=={{header|ALGOL W}}==
Line 151 ⟶ 226:
 
C = 0.57721566490153286...
</pre>
 
=={{header|Ada}}==
Three solutions here: the Vacca series, then two useful approximations for generally-used Ada types
Float and Long_Float.
<syntaxhighlight lang="ada">
with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;
with Ada.Numerics.Long_Elementary_Functions; use Ada.Numerics.Long_Elementary_Functions;
with Ada.Text_IO; use Ada.Text_IO;
 
procedure Eulers_Constant is
 
function Euler_Vacca (Iterations : Integer) return Long_Float is
Gamma : Long_Float := 1.0;
Term : Long_Float;
Power : Long_Integer;
Sign : Long_Float;
begin
Gamma := 0.5 - (1.0 / 3.0);
for I in 2 .. Iterations loop
Power := 2 ** Natural (I);
Sign := -1.0;
Term := 0.0;
for Domin in Power .. (2 * Power - 1) loop
Sign := - (Sign);
Term := Term + Sign / Long_Float (Domin);
end loop;
Gamma := Gamma + (Long_Float (I) * Term);
end loop;
return Gamma;
end Euler_Vacca;
 
-- Ada Float type is IEEE 754 32-bit, giving 9 decimal digits of precision
Euler_Castellanos_Float : constant Float :=
(((80.0 ** 3) + 92.0) /
(61.0 ** 4)) ** (1.0 / 6.0);
 
-- Ada Long_Float type is IEEE 754 32-bit, giving 14 decimal digits of precision
Euler_Castellanos_Long_Float : constant Long_Float :=
(990.0 ** 3 - 55.0 ** 3 - 79.0 ** 2 - 16.0) /
70.0 ** 5;
 
Iters : Integer;
begin
Put_Line ("Its. Vacca");
Iters := 2;
while Iters <= 32 loop
Put_Line (Iters'Image & " " & Euler_Vacca (Iters)'Image);
Iters := Iters + 2;
end loop;
Put_Line ("Castellanos approximation for standard Float (9 digits): " & Euler_Castellanos_Float'Image);
Put_Line ("Castellanos approximation for Long Float (14 digits): " & Euler_Castellanos_Long_Float'Image);
end Eulers_Constant;
</syntaxhighlight>
{{out}}
<pre>
Its. Vacca
2 3.14285714285714E-01
4 4.82164184398886E-01
6 5.45853770405349E-01
8 5.67441138957738E-01
10 5.74285301882304E-01
12 5.76361123043496E-01
14 5.76971520706463E-01
16 5.77147000098518E-01
18 5.77196591397621E-01
20 5.77210419691580E-01
22 5.77214234389975E-01
24 5.77215277471336E-01
26 5.77215560593404E-01
28 5.77215636961856E-01
30 5.77215657450952E-01
32 5.77215662922473E-01
Castellanos approximation for standard Float (9 digits): 5.77216E-01
Castellanos approximation for Long Float (14 digits): 5.77215664901529E-01
</pre>
 
3,049

edits