Anonymous user
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==
numeric digits digs /*REXX will use lots of decimal digits.*/
if a=='' | a==
if b=='' | b==
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 /* "
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;
numeric digits
▲ numeric digits d; return (g/1)i</lang>
'''outputs''' 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==
|