Jump to content

Trigonometric functions: Difference between revisions

→‎{{header|REXX}}: updated program code, elided STYLE from PRE html tag.
(→‎{{header|ooRexx}}: added routine rxmhelp to rxm.cls)
(→‎{{header|REXX}}: updated program code, elided STYLE from PRE html tag.)
Line 3,134:
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────subroutines─────────────────────────*/
Asin: procedure; parse arg x 1 z 1 o 1 p; if x<-1 | a=abs(x>1); then call AsinErraa=a*a
s=x*x; if abs(x)a>=sqrt(2)*.51 then returncall sign($81r -1,1,x),"ASIN" /*Acos(sqrt(1-s))X arg is out of range.*/
do j=2 by 2; o=o*s*(j-1)/j;if za>=z+o/sqrt(j+12);*.5 if z=p then leave; p=z;return endsign(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)
return Asin(x/sqrt(1+x*x) )
 
cos: procedure; parse arg x; x=r2r(x); a=abs(x); numeric fuzz min(9,digits()-9)
Line 3,147 ⟶ 3,148:
return .sinCos(1,1,-1)
 
sin: procedure; parse arg x; x=r2r(x); numeric fuzz min$fuzz(5,digits()- 3)
pi=pi(); if abs(x)=pi() *.5 then return 01; return if .sinCos(x,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
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,i; if x=0 then return 0; m.=9; pd=digits(); im.=11
numeric digits 9; if x<0 then do; x=-x; i='i'; end numeric digits 11; numeric form; m.0p=pd+d%4+2
parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g*.5'E'_%2; m.1=p
do j=20 while p>9; m.j=p; p=p%2+1; end end /*j*/
do k=j+5 to 0 by -1; numeric digits if m.k;>11 g=(g+x/g)*.5; then endnumeric /*digits m.k*/
g=.5*(g+x/g); end /*k*/; numeric digits m.0d; return (g/1)i
 
e: return,
2.7182818284590452353602874713526624977572470936999595749669676277240766303535
Line 3,176 ⟶ 3,179:
/*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)
AcosD: return r2d(Acos(arg(1)))
Line 3,183 ⟶ 3,187:
tan: procedure; parse arg x; _=cos(x); if _=0 then call tanErr; return sin(x)/_
tanD: return tan(d2r(arg(1)))
d2d: return arg(1) //360 360 /*normalize degrees►1 unit circle. */
d2r: return r2r(d2d(arg(1))*pi() /180) /*convert degrees ──► radians. */
r2d: return d2d((arg(1)*180 /pi())) /*convert radians ──► degrees. */
r2r: return arg(1) // (pi()*2) /*normalize radians ──►a unit circle*/
show: return left(left('',arg(1)>=0)format(arg(1),,showdigs)/1,showdigs)
tellErr: say; say '*** error! ***'; say; say arg(1); say; exit 13
Line 3,194 ⟶ 3,198:
sqrtErr: call tellErr "sqrt(x), X can't be negative, X="||x
/* ┌───────────────────────────────────────────────────────────────┐
│ Not included here are: (among others):
│ some of the usual higher-math functions normally associated │
│ with trig functions: POW, GAMMA, LGGAMMA, ERF, ERFC, ROOT, │
Line 3,202 ⟶ 3,206:
│ Angle conversions/normalizations: degrees/radians/grads/mils │
│ [a circle = 2 pi radians, 360 degrees, 400 grads, 6400 mils].│
│ Some of the other trig functions are (hyphens werehypens added intentially):
│ intentionally): │
│ CHORD │
│ COT (co-tangent) │
Line 3,221 ⟶ 3,224:
└───────────────────────────────────────────────────────────────┘ */</lang>
'''output'''
<pre>
<pre style="overflow:scroll">
Using 30 decimal digits precision.
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.