Jump to content

Arithmetic-geometric mean/Calculate Pi: Difference between revisions

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 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:
===version 2===
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. */
parse arg a b digs . z=z-n*(x-a)**2; n=n+n; a=x /*Zobtain optional isnumbers used infrom the final calculationC.L. */
if digs=='' | manydigs==compare(a",old) " then digs= 100 /*howNo manyDIGS accuratespecified? digits computed? Then use default.*/
numeric digits digs if many==0 then many=d /*adjustREXX forwill theuse verylots lastof timedecimal digits. */
if a=='' | a=="," say right('iteration' j, 20)then a=1 right(many, 9) "digits" /*showNo A specified? Then use digitsdefault.*/
if b=='' | b=="," end /*j*/then b=1 / sqrt(2) /*No B specified? " " " /* [↑] stop if A equals OLD. */
say call AGM a,b /*displayinvoke aAGM blank line& for adon't show separatorA,B,result.*/
pi=a**2exit 0 / z /*computestick thea finishedfork in valueit, of we're pi.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. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
sqrtagm: procedure;: parse arg x,y; if x=0y then return 0;x d=digits(); numeric digits; h=d+6/*is it an equality case? */
say pi / 1 if y=0 then return 0 /*display the computed /*is value of pi. 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; if x=0 then return y / h=h%2+1; /* " " end " X " /*j*/
d= digits(); numeric digits do k=jd+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.add 5; more digs to ensure end /*kconvergence*/
numerictiny= '1e-' || (digits() - 1) d; return g/1<*construct a pretty tiny REXX number. */lang>
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+5 numeric digits d /*setrestore the numeric decimal digits to D+5original.*/
exit /*stick a fork in it,/*this is the we'reonly alloutput done.displayed ►─┐*/
say 'digits='right(d, 7)", iterations=" right(#, 3) /* ◄───────────────┘*/
numeric digits d return x/1 /*setnormalize the numeric decimal digitsX to the Dnew digits. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); m.=9; numeric form; h=d+6
numeric form; m.=9digits; 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>
iteration 1 1 digits
Line 1,295 ⟶ 1,300:
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491
</pre>
'''{{out|output''' |text=&nbsp; when using the number of digits: &nbsp; &nbsp; <tt> 100000 </tt>}}
<pre>
iteration 1 1 digits
Cookies help us deliver our services. By using our services, you agree to our use of cookies.