Arithmetic-geometric mean/Calculate Pi: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added a comment.)
m (→‎version 2: added wording to the REXX version 2 section header, added/changed whitespace and comments, added a template for the output sections.)
Line 1,256: Line 1,256:
===version 2===
===version 2===
This REXX version shows the accurate (correct) number of digits in each iteration of the calculation of pi.
This REXX version shows the accurate (correct) number of digits in each iteration of the calculation of pi.
<lang rexx>/*REXX program calculates value of pi using the AGM algorithm (with running digits).*/
parse arg d .; if d=='' | d=="," then d=500 /*D not specified? Then use default. */
numeric digits d+5 /*set the numeric decimal digits to D+5*/
z=1/4; a=1; g=sqrt(1/2) /*calculate some initial values. */
n=1


For 1,005 decimal digits, &nbsp; it is over &nbsp; '''68''' &nbsp; times faster than version 3.
do j=1 until a==old; old=a /*keep calculating until no more noise.*/
<lang rexx>/*REXX program calculates the AGM (arithmetic─geometric mean) of two (real) numbers. */
x=(a+g)*.5; g=sqrt(a*g) /*calculate the next set of terms. */
z=z-n*(x-a)**2; n=n+n; a=x /*Z is used in the final calculation. */
parse arg a b digs . /*obtain optional numbers from the C.L.*/
many=compare(a,old) /*how many accurate digits computed? */
if digs=='' | digs=="," then digs= 100 /*No DIGS specified? Then use default.*/
if many==0 then many=d /*adjust for the very last time. */
numeric digits digs /*REXX will use lots of decimal digits.*/
say right('iteration' j, 20) right(many, 9) "digits" /*show digits.*/
if a=='' | a=="," then a=1 /*No A specified? Then use default.*/
end /*j*/ /* [↑] stop if A equals OLD. */
if b=='' | b=="," then b=1 / sqrt(2) /*No B specified? " " " */
say /*display a blank line for a separator.*/
call AGM a,b /*invoke AGM & don't show A,B,result.*/
pi=a**2 / z /*compute the finished value of pi. */
exit 0 /*stick a fork in it, we're all done. */
numeric digits d /*set the numeric decimal digits to D.*/
say pi / 1 /*display the computed value of pi. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); numeric digits; h=d+6
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? */
numeric form; m.=9; 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*/
if x=0 then return y / 2 /* " " " X " */
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/
d= digits(); numeric digits d+5 /*add 5 more digs to ensure convergence*/
numeric digits d; return g/1</lang>
tiny= '1e-' || (digits() - 1) /*construct a pretty tiny REXX number. */
ox= x + 1
'''output''' &nbsp; using the default number of digits: &nbsp; <tt> 500 </tt>
do #=1 while ox\=x & abs(ox)>tiny; ox= x; oy= y
x= (ox+oy)/2; y= sqrt(ox*oy)
end /*#*/
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(); 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
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>
{{out|output|text=&nbsp; when using the default number of digits: &nbsp; &nbsp; <tt> 500 </tt>}}
<pre>
<pre>
iteration 1 1 digits
iteration 1 1 digits
Line 1,295: Line 1,300:
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491
</pre>
</pre>
'''output''' &nbsp; using the number of digits: &nbsp; <tt> 100000 </tt>
{{out|output|text=&nbsp; when using the number of digits: &nbsp; &nbsp; <tt> 100000 </tt>}}
<pre>
<pre>
iteration 1 1 digits
iteration 1 1 digits