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. */ |
|||
⚫ | |||
z=1/4; a=1; g=sqrt(1/2) /*calculate some initial values. */ |
|||
n=1 |
|||
For 1,005 decimal digits, it is over '''68''' 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 . /*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 0 /*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 x=0 then return y / 2 /* " " " 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 /*#*/ |
|||
⚫ | |||
⚫ | |||
say 'digits='right(d, 7)", iterations=" right(#, 3) /* ◄───────────────┘*/ |
|||
⚫ | |||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); m.=9; numeric form; h=d+6 |
|||
⚫ | |||
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> |
|||
⚫ | |||
<pre> |
<pre> |
||
iteration 1 1 digits |
iteration 1 1 digits |
||
Line 1,295: | Line 1,300: | ||
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491 |
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491 |
||
</pre> |
</pre> |
||
{{out|output|text= when using the number of digits: <tt> 100000 </tt>}} |
|||
<pre> |
<pre> |
||
iteration 1 1 digits |
iteration 1 1 digits |