Arithmetic-geometric mean/Calculate Pi: Difference between revisions

m
→‎{{header|RPL}}: highlighted syntax
m (→‎{{header|Wren}}: Minor tidy)
m (→‎{{header|RPL}}: highlighted syntax)
 
(4 intermediate revisions by 3 users not shown)
Line 18:
 
The purpose of this task is to demonstrate how to use this approximation in order to compute a large number of decimals of <math>\pi</math>.
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">
-- Use the Arithmetic-geometric mean to calculate Pi
-- J. Carter 2024 May
 
with Ada.Numerics.Generic_Elementary_Functions;
with Ada.Text_IO;
with System;
 
procedure AGM_Pi is
type Real is digits System.Max_Digits;
 
package Math is new Ada.Numerics.Generic_Elementary_Functions (Float_Type => Real);
 
A : Real := 1.0;
B : Real := Math.Sqrt (0.5);
T : Real := 0.25;
N : Real := 1.0;
Prev_A : Real;
Pi : Real;
Prev_Pi : Real := 0.0;
begin -- AGM_Pi
Calculate : loop
Prev_A := A;
A := (A + B) / 2.0;
B := Math.Sqrt (Prev_A * B);
T := T - N * (A - Prev_A) ** 2;
N := N + N;
Pi := (A + B) ** 2 / (4.0 * T);
Ada.Text_IO.Put_Line (Item => Pi'Image);
 
exit Calculate when abs (Prev_Pi - Pi) < 10.0 ** (-(Real'Digits - 1) );
 
Prev_Pi := Pi;
end loop Calculate;
end AGM_Pi;
</syntaxhighlight>
{{out}}
<pre>
3.14057925052216825E+00
3.14159264621354228E+00
3.14159265358979324E+00
3.14159265358979324E+00
</pre>
 
=={{header|BASIC}}==
Line 59 ⟶ 104:
{{out}}
<pre>3.141592653589794</pre>
 
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">100 PROGRAM "PI.bas"
110 LET DIGITS=10
120 LET AN,PN=1
130 LET BN=SQR(.5)
140 LET TN=.5^2
150 DO WHILE PN<=DIGITS
160 LET PREVAN=AN
170 LET AN=(BN+AN)/2
180 LET BN=SQR(BN*PREVAN)
190 LET PREVAN=PREVAN-AN
200 LET TN=TN-(PN*PREVAN^2)
210 LET PN=PN+PN
220 LOOP
230 PRINT (AN+BN)^2/(TN*4)</syntaxhighlight>
 
==={{header|True BASIC}}===
Line 1,083 ⟶ 1,144:
pi[7, 100]
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628046852228654</syntaxhighlight>
 
=={{header|MATLAB}}==
{{trans|Julia}}
<syntaxhighlight lang="MATLAB">
 
clear all;close all;clc;
testMakePi();
 
 
function [a, g] = agm1step(x, y)
a = (x + y) / 2;
g = sqrt(x * y);
end
 
function [a, g, s, k] = approxPiStep(x, y, z, n)
[a, g] = agm1step(x, y);
k = n + 1;
s = z + 2^(k + 1) * (a^2 - g^2);
end
 
function pi_approx = approxPi(a, g, s)
pi_approx = 4 * a^2 / (1 - s);
end
 
function testMakePi()
digits(512); % Set the precision for variable-precision arithmetic
a = vpa(1.0);
g = 1 / sqrt(vpa(2.0));
s = vpa(0.0);
k = 0;
oldPi = vpa(0.0);
% Define a small value as a threshold for convergence
convergence_threshold = vpa(10)^(-digits);
 
fprintf(' k Error Result\n');
for i = 1:100
[a, g, s, k] = approxPiStep(a, g, s, k);
estPi = approxPi(a, g, s);
if abs(estPi - oldPi) < convergence_threshold
break;
end
oldPi = estPi;
err = abs(vpa(pi) - estPi);
fprintf('%4d%10.1e', i, double(err));
fprintf('%70.60f\n', double(estPi));
end
end
</syntaxhighlight>
{{out}}
<pre>
k Error Result
1 4.6e-02 3.187672642712108483920019352808594703674316406250000000000000
2 8.8e-05 3.141680293297653303596916884998790919780731201171875000000000
3 3.1e-10 3.141592653895446396461466065375134348869323730468750000000000
4 3.7e-21 3.141592653589793115997963468544185161590576171875000000000000
5 5.5e-43 3.141592653589793115997963468544185161590576171875000000000000
6 1.2e-86 3.141592653589793115997963468544185161590576171875000000000000
7 5.8e-174 3.141592653589793115997963468544185161590576171875000000000000
8 0.0e+00 3.141592653589793115997963468544185161590576171875000000000000
9 0.0e+00 3.141592653589793115997963468544185161590576171875000000000000
</pre>
 
=={{header|МК-61/52}}==
Line 1,761 ⟶ 1,883:
+ SQ ROT 4 * /
SWAP DROP
≫ ≫ ‘'''<span style="color:blue">AGMPI</span>'''’ STO
≪ { } 1 5 '''FOR''' d d <span style="color:blue">AGMPI</span> '''AGMPINEXT''' NEXT
‘'''<span style="color:blue">TASK</span>'''’ STO
|
'''<span style="color:blue">AGMPI'''</span> ''( digits -- ~pi )''
tn = 0.5 ^ 2 : pn = 1.0 : an = 1.0 : bn = sqrt(0.5)
while pn <= digits
1,150

edits