Jump to content

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 &nbsp; &nbsp; <big><big>&pi;</big></big> &nbsp; should be at least as accurate as &nbsp; '''digs'''.
:::* If any formula needs any high math functions, they need to be included.
<lang rexx>/*REXX program evaluates some someMachin-like expressionsformulas and verifies their veracity.*/
parse arg digs .; if digs=='' then digs=100 /*use default for decimal digs?*/
numeric digits digs+10; numeric fuzz 3; pi=pi(); @.=
@.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 of the"Machin-like" formulas. */
interpret 'answer=' "(" @.j ")" /*this is the heavy lifting.*/
say right(word('bad OK',answer+1),3)": " space(@.j,0)
end /*j*/ /* [↑] show OK |or bad, formula.and the formula*/
exit /*stick a fork in it, we're all done. */
/*────broutines───────────────────────────────────────────────────────────────*/
/*──────────────────────────────────subroutines─────────────────────────*/
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
tellErr: say; say '*** error! ***'; say; say arg(1); say; exit 13</lang>
 
Acos: procedure; parse arg x; if x<-1 | x>1 then call AcosErr
return .5*pi()-Asin(x)
 
Asin: procedure expose $.; parse arg x; 1 z if x<-1 |o x>1 p; then call AsinErra=abs(x); saa=xa*xa
if a>1 then call AsinErr x /*X argument is out of valid range. */
if abs(x)a>=sqrt(2)*.5 then return sign(x)*Acosacos(sqrt(1-saa)); z=x;, o=x; p=z'-ASIN')
do j=2 by 2 until p=z; p=z; o=o*saa*(j-1)/j; z=z+o/(j+1); if z=p then leave; p=z; end
return z
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>
pi: return,
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651
 
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>
Cookies help us deliver our services. By using our services, you agree to our use of cookies.