Trigonometric functions: Difference between revisions

Content added Content deleted
(→‎{{header|ooRexx}}: added routine rxmhelp to rxm.cls)
(→‎{{header|REXX}}: updated program code, elided STYLE from PRE html tag.)
Line 3,134: Line 3,134:
exit /*stick a fork in it, we're done.*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────subroutines─────────────────────────*/
/*──────────────────────────────────subroutines─────────────────────────*/
Asin: procedure; parse arg x 1 z 1 o 1 p; if x<-1 | x>1 then call AsinErr
Asin: procedure; parse arg x 1 z 1 o 1 p; a=abs(x); aa=a*a
s=x*x; if abs(x)>=sqrt(2)*.5 then return sign(x)*Acos(sqrt(1-s))
if a>1 then call $81r -1,1,x,"ASIN" /*X arg is out of range.*/
do j=2 by 2; o=o*s*(j-1)/j; z=z+o/(j+1); if z=p then leave; p=z; end
if a>=sqrt(2)*.5 then return sign(x)*acos(sqrt(1-aa), '-ASIN')
do j=2 by 2 until p=z; p=z; o=o*aa*(j-1)/j; z=z+o/(j+1); end
return z
return z /* [↑] compute until no noise.*/


Atan: procedure; parse arg x; if abs(x)=1 then return pi()*.25*sign(x)
Atan: procedure; parse arg x; if abs(x)=1 then return pi() * .25 * sign(x)
return Asin(x/sqrt(1+x*x))
return Asin(x/sqrt(1+x*x) )


cos: procedure; parse arg x; x=r2r(x); a=abs(x); numeric fuzz min(9,digits()-9)
cos: procedure; parse arg x; x=r2r(x); a=abs(x); numeric fuzz min(9,digits()-9)
Line 3,147: Line 3,148:
return .sinCos(1,1,-1)
return .sinCos(1,1,-1)


sin: procedure; parse arg x; x=r2r(x); numeric fuzz min(5,digits()-3)
sin: procedure; parse arg x; x=r2r(x); numeric fuzz $fuzz(5, 3)
if abs(x)=pi() then return 0; return .sinCos(x,x,1)
pi=pi(); if x=pi*.5 then return 1; if x==pi*1.5 then return -1
if abs(x)=pi | x=0 then return 0; return .sinCos(x, x, +1)


.sinCos: parse arg z 1 p,_,i; x=x*x
.sinCos: parse arg z 1 p,_,i; x=x*x
do k=2 by 2; _=-_*x/(k*(k+i));z=z+_;if z=p then leave;p=z;end; return z
do k=2 by 2; _=-_*x/(k*(k+i));z=z+_;if z=p then leave;p=z;end; return z


sqrt: procedure; parse arg x; if x=0 then return 0; m.=9; p=digits(); i=
sqrt: procedure; parse arg x,i; if x=0 then return 0; d=digits(); m.=11
numeric digits 9; if x<0 then do; x=-x; i='i'; end; numeric form; m.0=p
if x<0 then i='i'; numeric digits 11; numeric form; p=d+d%4+2
parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g*.5'E'_%2; m.1=p
parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g*.5'E'_%2
do j=2 while p>9; m.j=p; p=p%2+1; end /*j*/
do j=0 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=(g+x/g)*.5; end /*k*/
do k=j+5 to 0 by -1; if m.k>11 then numeric digits m.k
numeric digits m.0; return (g/1)i
g=.5*(g+x/g); end /*k*/; numeric digits d; return g/1

e: return,
e: return,
2.7182818284590452353602874713526624977572470936999595749669676277240766303535
2.7182818284590452353602874713526624977572470936999595749669676277240766303535
Line 3,176: Line 3,179:
/*If more than 1 million digits are required, be patient. */
/*If more than 1 million digits are required, be patient. */


$fuzz: return min(arg(1), max(1, digits() - arg(2) ) )
Acos: procedure; parse arg x; if x<-1|x>1 then call AcosErr; return .5*pi()-Asin(x)
Acos: procedure; parse arg x; if x<-1|x>1 then call AcosErr; return .5*pi()-Asin(x)
AcosD: return r2d(Acos(arg(1)))
AcosD: return r2d(Acos(arg(1)))
Line 3,183: Line 3,187:
tan: procedure; parse arg x; _=cos(x); if _=0 then call tanErr; return sin(x)/_
tan: procedure; parse arg x; _=cos(x); if _=0 then call tanErr; return sin(x)/_
tanD: return tan(d2r(arg(1)))
tanD: return tan(d2r(arg(1)))
d2d: return arg(1)//360 /*normalize degrees►1 unit circle. */
d2d: return arg(1) // 360 /*normalize degrees►1 unit circle. */
d2r: return r2r(d2d(arg(1))*pi()/180) /*convert degrees ──► radians. */
d2r: return r2r(d2d(arg(1))*pi() /180) /*convert degrees ──► radians. */
r2d: return d2d((arg(1)*180/pi())) /*convert radians ──► degrees. */
r2d: return d2d((arg(1)*180 /pi())) /*convert radians ──► degrees. */
r2r: return arg(1)//(pi()*2) /*normalize radians ──►a unit circle*/
r2r: return arg(1) // (pi()*2) /*normalize radians ──►a unit circle*/
show: return left(left('',arg(1)>=0)format(arg(1),,showdigs)/1,showdigs)
show: return left(left('',arg(1)>=0)format(arg(1),,showdigs)/1,showdigs)
tellErr: say; say '*** error! ***'; say; say arg(1); say; exit 13
tellErr: say; say '*** error! ***'; say; say arg(1); say; exit 13
Line 3,194: Line 3,198:
sqrtErr: call tellErr "sqrt(x), X can't be negative, X="||x
sqrtErr: call tellErr "sqrt(x), X can't be negative, X="||x
/* ┌───────────────────────────────────────────────────────────────┐
/* ┌───────────────────────────────────────────────────────────────┐
│ Not included here are (among others):
│ Not included here are: (among others): │
│ some of the usual higher-math functions normally associated │
│ some of the usual higher-math functions normally associated │
│ with trig functions: POW, GAMMA, LGGAMMA, ERF, ERFC, ROOT, │
│ with trig functions: POW, GAMMA, LGGAMMA, ERF, ERFC, ROOT, │
Line 3,202: Line 3,206:
│ Angle conversions/normalizations: degrees/radians/grads/mils │
│ Angle conversions/normalizations: degrees/radians/grads/mils │
│ [a circle = 2 pi radians, 360 degrees, 400 grads, 6400 mils].│
│ [a circle = 2 pi radians, 360 degrees, 400 grads, 6400 mils].│
│ Some of the other trig functions are (hyphens were added
│ Some of the other trig functions (hypens added intentially):
│ intentionally): │
│ CHORD │
│ CHORD │
│ COT (co-tangent) │
│ COT (co-tangent) │
Line 3,221: Line 3,224:
└───────────────────────────────────────────────────────────────┘ */</lang>
└───────────────────────────────────────────────────────────────┘ */</lang>
'''output'''
'''output'''
<pre>
<pre style="overflow:scroll">
Using 30 decimal digits precision.
Using 30 decimal digits precision.