Anonymous user
Arithmetic-geometric mean/Calculate Pi: Difference between revisions
Arithmetic-geometric mean/Calculate Pi (view source)
Revision as of 21:08, 15 January 2021
, 3 years ago→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.
numeric digits d+5 /*set the numeric decimal digits to D+5*/▼
For 1,005 decimal digits, it is over '''68''' times faster than version 3.
<lang rexx>/*REXX program calculates the AGM (arithmetic─geometric mean) of two (real) numbers. */
parse arg a b digs .
if digs=='' |
numeric digits digs
if a=='' | a==","
if b=='' | b==","
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. */▼
/*──────────────────────────────────────────────────────────────────────────────────────*/
numeric form; m.=9; parse value format(x,2,1,,0) 'E0' with g "E" _ .; g=g *.5'e'_ %2▼
d= digits(); numeric digits
ox= x + 1
'''output''' using the default number of digits: <tt> 500 </tt>▼
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>
iteration 1 1 digits
Line 1,295 ⟶ 1,300:
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491
</pre>
<pre>
iteration 1 1 digits
|