Talk:Arithmetic-geometric mean: Difference between revisions

m
→‎rapidity: changed some comments and enhanced some IFs, optimized the SQRT function.
m (→‎rapidity: changed some comments and enhanced some IFs, optimized the SQRT function.)
Line 59:
 
The modified REXX program is:
<lang rexx>/*REXX program calculates the AGM (arithmetic─geometric mean) of two (real) numbers. */
parse arg a b digs . /*obtain optional numbers from the C.L.*/
if digs=='' | digs=='",'" then digs=100 /*No DIGS specified? Then use default.*/
numeric digits digs /*REXX will use lots of decimal digits.*/
if a=='' | a=='",'" then a=1 /*No A specified? Then use default.*/
if b=='' | b=='",'" then b=1 / sqrt(2) /*No B specified? " " " */
call AGM a,b /*invoke AGM & don't show A,B,result.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
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 x=0 then return y/2 /* " X " " X " */
d=digits(); numeric digits d+5 /*add 5 more digs to ensure convergence*/
tiny='1e-' || (digits()-1); /*construct a pretty tiny REXX number. */
ox=x+1
do #=1 while ox\=x & abs(ox)>tiny; ox=x; oy=y
x=(ox+oy)/2; y=sqrt(ox*oy)
end /*while ··· */
 
numeric digits d /*restore numeric digits to original.*/
/*this is the only output displayed ►─┐*/
say 'digits='right(d, 7)", iterations=" right(#, 3) /* ◄───────────────┘*/
return x/1 /*normalize X to the new digits. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); im.=9; numeric form; m. h=9d+6
numeric digits 9; ifparse value format(x<,2,1,,0) 'E0' then do;with x=-x; i=g 'iE'; end_ .; g=g numeric*.5'e'_ form;% h=d+2
parse value format(x,2,1,,0) 'E0' with g do j=0 while h>9; 'E' _ m.j=h; g h=g*.5'E'_h%2+1; end /*j*/
do j=0 while h>9; do m.k=j=h;+5 to 0 by -1; numeric digits m.k; hg=h%2(g+1x/g)*.5; end /*jk*/
numeric digits d; return (g/1)i</lang>
do k=j+5 to 0 by -1; numeric digits m.k; g=.5*(g+x/g); end /*k*/
numeric digits d; return (g/1)i</lang>
'''outputs''' &nbsp; with a specified number of digits, and with the various outputs being combined/reduced/edited:
<pre>
Line 103 ⟶ 102:
</pre>
Needless to say, the CPU time increased everytime the digits were doubled.
 
 
==Formulae hidden to most browsers by under-tested cosmetic edits at 21:25, 14 April 2016==