Arithmetic-geometric mean: Difference between revisions

no edit summary
(Dialects of BASIC moved to the BASIC section.)
imported>Lacika7
No edit summary
 
(11 intermediate revisions by 8 users not shown)
Line 239:
ELIF x + y = LONG 0.0 THEN LONG 0.0 CO Edge cases CO
ELSE
LONG REAL a := x, g := y;
LONG REAL epsilon := a + g;
LONG REAL next a := (a + g) / LONG 2.0, next g := long sqrt (a * g);
LONG REAL next epsilon := ABS (a - g);
WHILE next epsilon < epsilon
DO
print ((epsilon, " ", next epsilon, newline));
epsilon := next epsilon;
a := next a; g := next g;
next a := (a + g) / LONG 2.0; next g := long sqrt (a * g);
next epsilon := ABS (a - g)
OD;
a
a
FI
END;
Line 397:
 
=={{header|BASIC}}==
==={{header|ANSI BASIC}}===
{{works with|Decimal BASIC}}
<syntaxhighlight lang="basic">100 PROGRAM ArithmeticGeometricMean
110 FUNCTION AGM (A, G)
120 DO
130 LET TA = (A + G) / 2
140 LET G = SQR(A * G)
150 LET Tmp = A
160 LET A = TA
170 LET TA = Tmp
180 LOOP UNTIL A = TA
190 LET AGM = A
200 END FUNCTION
210 REM ********************
220 PRINT AGM(1, 1 / SQR(2))
230 END</syntaxhighlight>
{{out}}
<pre> .84721308479398 </pre>
 
==={{header|Applesoft BASIC}}===
Same code as [[#Commodore_BASIC|Commodore BASIC]]
The [[#BASIC|BASIC]] solution works without any changes.
 
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">print AGM(1, 1 / sqr(2))
Line 413 ⟶ 436:
end function</syntaxhighlight>
{{out}}
<pre>0.84721308479</pre>
0.84721308479
</pre>
 
==={{header|BBC BASIC}}===
Line 434 ⟶ 455:
</syntaxhighlight>
{{out}}
<pre>0.8472130847939792</pre>
<pre>
0.8472130847939792
</pre>
 
==={{header|Chipmunk Basic}}===
Line 466 ⟶ 485:
130 IF A<TA THEN 100
140 RETURN</syntaxhighlight>
 
==={{header|Craft Basic}}===
<syntaxhighlight lang="basic">let a = 1
let g = 1 / sqrt(2)
 
do
 
let t = (a + g) / 2
let g = sqrt(a * g)
let x = a
let a = t
let t = x
 
loopuntil a = t
 
print a</syntaxhighlight>
{{out| Output}}
<pre>0.85</pre>
 
==={{header|FreeBASIC}}===
Line 496 ⟶ 533:
{{out}}
<pre> 0.8472130847939792</pre>
 
==={{header|Gambas}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">Public Sub Main()
Print AGM(1, 1 / Sqr(2))
 
End
 
Function AGM(a As Float, g As Float) As Float
 
Dim t_a As Float
Do
t_a = (a + g) / 2
g = Sqr(a * g)
Swap a, t_a
Loop Until a = t_a
Return a
 
End Function</syntaxhighlight>
 
==={{header|GW-BASIC}}===
Line 536 ⟶ 595:
</syntaxhighlight>
{{out}}
<pre>0.84721308
0.84721308479397904</pre>
0.84721308
 
0.84721308479397904
==={{header|Minimal BASIC}}===
</pre>
{{trans|Commodore BASIC}}
{{works with|IS-BASIC}}
<syntaxhighlight lang="qbasic">10 LET A = 1
20 LET G = 1 / SQR(2)
30 GOSUB 60
40 PRINT A
50 STOP
60 LET T = A
70 LET A = (A + G) / 2
80 LET G = SQR(T * G)
90 IF A < T THEN 60
100 RETURN
110 END</syntaxhighlight>
{{out}}
<pre> .84721308</pre>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
The [[#Commodore BASIC|Commodore BASIC]] solution works without any changes.
 
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|PureBasic}}===
Line 558 ⟶ 638:
EndIf</syntaxhighlight>
{{out}}
<pre> 0.8472130847939792</pre>
<pre>
0.8472130847939792
</pre>
 
==={{header|QuickBASIC}}===
Line 577 ⟶ 655:
END FUNCTION</syntaxhighlight>
{{out}}
<pre>.8472131</pre>
 
.8472131
==={{header|Quite BASIC}}===
</pre>
{{trans|Commodore BASIC}}
<syntaxhighlight lang="qbasic">10 LET A = 1
20 LET G = 1 / SQR(2)
30 GOSUB 100
40 PRINT A
50 END
100 LET T = A
110 LET A = (A + G) / 2
120 LET G = SQR(T * G)
130 IF A < T THEN 100
140 RETURN</syntaxhighlight>
{{out}}
<pre>0.8472130847939792</pre>
 
==={{header|Run BASIC}}===
Line 647 ⟶ 738:
END</syntaxhighlight>
{{out}}
<pre>.84721308</pre>
.84721308
</pre>
 
==={{header|VBA}}===
Line 672 ⟶ 761:
==={{header|VBScript}}===
{{trans|BBC BASIC}}
<syntaxhighlight lang="vb">Function agm(a,g)
Function agm(a,g)
Do Until a = tmp_a
tmp_a = a
Line 682 ⟶ 770:
End Function
 
WScript.Echo agm(1,1/Sqr(2))</syntaxhighlight>
</syntaxhighlight>
{{Out}}
<pre>0.847213084793979</pre>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="vb">print AGM(1, 1 / sqrt(2))
end
 
sub AGM(a, g)
repeat
ta = (a + g) / 2
g = sqrt(a * g)
x = a
a = ta
ta = x
until a = ta
 
return a
end sub</syntaxhighlight>
{{out}}
<pre>0.847213</pre>
 
 
Line 1,247 ⟶ 1,352:
2
The arithmetic-geometric mean is 1,456791</pre>
 
=={{header|dc}}==
<syntaxhighlight lang="dc">>>> 200 k ? sbsa [lalb +2/ lalb *vsb dsa lb - 0!=:]ds:xlap
?> 1 1 2 v /</syntaxhighlight>
 
{{out}}
<pre>
.8472130847939790866064991234821916364814459103269421850605793726597\
34004834134759723200293994611229942122285625233410963097962665830871\
05969971363598338425117632681428906038970676860161665004828118868
</pre>
 
You can change the precision (200 by default)
 
=={{header|EasyLang}}==
{{trans|AWK}}
<syntaxhighlight lang=easylang>
func agm a g .
repeat
a0 = a
a = (a0 + g) / 2
g = sqrt (a0 * g)
until abs (a0 - a) < abs (a) * 1e-15
.
return a
.
numfmt 16 0
print agm 1 sqrt 0.5
</syntaxhighlight>
 
=={{header|EchoLisp}}==
Line 2,784 ⟶ 2,918:
 
say agm 1, 1/sqrt 2;</syntaxhighlight>
 
We can also get a bit fancy and use a converging sequence of complex numbers:
 
<syntaxhighlight lang=raku>sub agm {
($^z, {(.re+.im)/2 + (.re*.im).sqrt*1i} ... * ≅ *)
.tail.re
}
say agm 1 + 1i/2.sqrt</syntaxhighlight>
 
=={{header|Raven}}==
Line 3,390 ⟶ 3,533:
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="ecmascriptwren">var eps = 1e-14
 
var agm = Fn.new { |a, g|
Anonymous user