Arithmetic-geometric mean/Calculate Pi: Difference between revisions

m
→‎{{header|REXX}}: added whitespace and comments, used a template for the output sections.
m (→‎version 2: added wording to the REXX version 2 section header, added/changed whitespace and comments, added a template for the output sections.)
m (→‎{{header|REXX}}: added whitespace and comments, used a template for the output sections.)
Line 1,223:
=={{header|REXX}}==
{{trans|Ruby}}
 
Programming note:   the number of digits to be used in the calculations can be specified on the C.L. ('''c'''ommand '''l'''ine).
 
<br>Whatever number of digits used, the actual number of digits is five larger than specified, and then the result is rounded to the requested number of digits.
===version 1===
<lang rexx>/*REXX program calculates the value of pi using the AGM algorithm. */
parse arg d .; if d=='' | d=="," then d=500 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
do j=1 until a==old; old= a /*keep calculating until no more noise.*/
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. */
end /*j*/ /* [↑] stop if A equals OLD. */
 
pi= a**2 / z /*compute the finished value of pi. */
numeric digits d /*set the numeric decimal digits to D.*/
say pi / 1 /*display the computed value of pi. */
exit 0 /*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
Line 1,248 ⟶ 1,250:
Programming note: &nbsp; the &nbsp; '''sqrt''' &nbsp; subroutine (above) is optimized for larger ''digits''.
 
'''{{out|output''' |text=&nbsp; when using the default number of digits: &nbsp; &nbsp; <tt> 500 </tt>}}
<pre>
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491