Talk:Arithmetic-geometric mean: Difference between revisions
Content added Content deleted
m (→rapidity: changed some comments and enhanced some IFs, optimized the SQRT function.) |
m (→rapidity of convergence: changed comments and whitespace, used a template for output.) |
||
Line 49: | Line 49: | ||
const ε = 1e-14</lang> |
const ε = 1e-14</lang> |
||
==rapidity== |
==rapidity of convergence== |
||
From this Rosetta Code task's prologue: |
From this Rosetta Code task's prologue: |
||
Line 55: | Line 55: | ||
:: <big><big>Since the limit of <math>a_n-g_n</math> tends (rapidly) to zero with iterations, this is an efficient method.</big></big> |
:: <big><big>Since the limit of <math>a_n-g_n</math> tends (rapidly) to zero with iterations, this is an efficient method.</big></big> |
||
<br>With this in mind, |
<br>With this in mind, the REXX entry was modified (and suppressed the normal output being displayed), and |
||
<br>added a display of the iteration count along with the number of decimal digits being used. |
<br>added a display of the iteration count along with the number of decimal digits being used. |
||
Line 70: | Line 70: | ||
agm: procedure: parse arg x,y; if x=y then return x /*is it an equality case? */ |
agm: procedure: parse arg x,y; if x=y then return x /*is it an equality case? */ |
||
if y=0 then return 0 /*is value of Y zero? */ |
if y=0 then return 0 /*is value of Y zero? */ |
||
if x=0 then return y/ |
if x=0 then return y / 2 /* " " " X " */ |
||
d=digits(); numeric digits d+5 /*add 5 more digs to ensure convergence*/ |
d=digits(); numeric digits d+5 /*add 5 more digs to ensure convergence*/ |
||
tiny='1e-' || (digits()-1); |
tiny='1e-' || (digits() - 1); /*construct a pretty tiny REXX number. */ |
||
ox=x+1 |
ox=x + 1 |
||
do #=1 while ox\=x & abs(ox)>tiny; |
do #=1 while ox\=x & abs(ox)>tiny; ox=x; oy=y |
||
x=(ox+oy)/2; y=sqrt(ox*oy) |
x=(ox+oy)/2; y=sqrt(ox*oy) |
||
end /* |
end /*#*/ |
||
numeric digits d /*restore numeric digits to original.*/ |
numeric digits d /*restore numeric digits to original.*/ |
||
/*this is the only output displayed ►─┐*/ |
/*this is the only output displayed ►─┐*/ |
||
Line 85: | Line 84: | ||
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); m.=9; numeric form; h=d+6 |
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); m.=9; numeric form; h=d+6 |
||
numeric digits; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g *.5'e'_ % 2 |
numeric digits; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g *.5'e'_ % 2 |
||
do j=0 while h>9; m.j=h; h=h % 2 + 1; end /*j*/ |
|||
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/; return g</lang> |
|||
⚫ | |||
return g</lang> |
|||
⚫ | |||
<pre> |
<pre> |
||
digits= 100, iterations= 9 |
digits= 100, iterations= 9 |
||
Line 101: | Line 99: | ||
digits= 51200, iterations= 18 |
digits= 51200, iterations= 18 |
||
</pre> |
</pre> |
||
The CPU time quadrupled (about) everytime the digits were doubled. |
|||
==Formulae hidden to most browsers by under-tested cosmetic edits at 21:25, 14 April 2016== |
==Formulae hidden to most browsers by under-tested cosmetic edits at 21:25, 14 April 2016== |