Anonymous user
Check Machin-like formulas: Difference between revisions
m
→{{header|REXX}}: added/changed whitespace and comments, simplified some functions.
(Add Perl 5) |
m (→{{header|REXX}}: added/changed whitespace and comments, simplified some functions.) |
||
Line 966:
:::* The value of <big><big>π</big></big> should be at least as accurate as '''digs'''.
:::* If any formula needs any high math functions, they need to be included.
<lang rexx>/*REXX program
parse arg digs .; if digs=='' then digs=100
numeric digits digs+10;
@.1 = 'pi/4 = atan(1/2) + atan(1/3)'
@.2 = 'pi/4 = 2*atan(1/3) + atan(1/7)'
Line 987:
@.17 = 'pi/4 = 88*atan(1/172) + 51*atan(1/239) + 32*atan(1/682) + 44*atan(1/5357) + 68*atan(1/12944)'
do j=1 while @.j\=='' /*evaluate each
interpret 'answer=' "(" @.j ")"
say right(word('bad OK',answer+1),3)": " space(@.j,0)
end /*j*/ /* [↑] show OK
exit /*stick a fork in it, we're all done. */
/*────broutines───────────────────────────────────────────────────────────────*/
pi: return 3.14159265358979323846264338327950288419716939937510582097494459 ||,
230781640628620899862803482534211706798214808651
AcosErr: call tellErr 'Acos(x), X must be in the range of -1 ──► +1, X='||x▼
AsinErr: call tellErr 'Asin(x), X must be in the range of -1 ──► +1, X='||x▼
tanErr: call tellErr 'tan(' || x") causes division by zero, X=" || x▼
Acos: procedure; parse arg x; if x<-1 | x>1 then call AcosErr
return .5*pi()-Asin(x)
Asin: procedure expose $.;
if a>1 then call AsinErr x /*X argument is out of valid range. */
if
do j=2
return z /* [↑] compute until no more noise. */
Atan: procedure; parse arg x; if abs(x)=1 then return pi()/4*sign(x)
return Asin(x/sqrt(1+x**2))
sqrt: procedure; parse arg x; if x=0 then return 0; m.=9; p=digits(); i=
numeric digits 9; if x<0 then do; x=-x; i='i'; end; numeric form; m.0=p
parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g*.5'E'_%2; m.1=p
do j=2 while p>9; m.j=p; p=p%2+1; end /*j*/
do k=j+5 to 0 by -1; numeric digits m.k; g=.5*(g+x/g); end /*k*/
numeric digits m.0; return (g/1)i</lang>
▲AcosErr: call tellErr 'Acos(x), X must be in the range of -1 ──► +1, X='||x
▲AsinErr: call tellErr 'Asin(x), X must be in the range of -1 ──► +1, X='||x
▲tanErr: call tellErr 'tan(' || x") causes division by zero, X=" || x
▲tellErr: say; say '*** error! ***'; say; say arg(1); say; exit 13</lang>
'''output'''
<pre>
|