Trigonometric functions: Difference between revisions

→‎{{header|REXX}}: fixed aboundry problem when ASIN arg is near √2.
m (→‎{{header|REXX}}: expanded some subroutine source code to the original, changed some wording in comments.)
(→‎{{header|REXX}}: fixed aboundry problem when ASIN arg is near √2.)
Line 2,012:
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────subroutines─────────────────────────*/
Asin: procedure; parse arg x; 1 z 1 o 1 p; if x<-1 | x>1 then call AsinErr; s=x*x
s=x*x; if abs(x)>=sqrt(2)*.75 then return sign(x)*Acos(sqrt(1-s)); z=x; o=x; p=z
do j=2 by 2; o=o*s*(j-1)/j; z=z+o/(j+1); if z=p then leave; p=z; end
return z
 
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)
if a=pi() then return -1; if a=pi()*.5 | a=pi()*2 then return 0
pi3=pi()/3; if a=pi3 then return .5; if a=2*pi3 then return -.5
return .sinCos(1,1,-1)
 
sin: procedure; parse arg x; x=r2r(x); numeric fuzz min(5,digits()-3)
if abs(x)=pi() then return 0; return .sinCos(x,x,1)
 
.sinCos: parse arg z 1 p,_,i; x=x*x
Line 2,043:
/*If more than 1 million digits are required, be patient. */
 
exp: procedure; parse arg x; ix=x%1; if abs(x-ix)>.5 then ix=ix+sign(x); x=x-ix
z=1; _=1; w=z; do j=1; _=_*x/j; z=(z+_)/1; if z==w then leave; w=z; end
if z\==0 then z=e()**ix*z; return z
Line 2,049:
pi: return, /*a bit of overkill, but hey !! */
3.1415926535897932384626433832795028841971693993751058209749445923078164062862
/*Note: the actual PI subroutine returns PI's accuracy that */
/*matches the current NUMERIC DIGITS, up to 1 million digits.*/
/*John Machin's formula is used for calculating more digits. */
/*If more than 1 million digits are required, be patient. */
 
Acos: procedure; parse arg x; if x<-1|x>1 then call AcosErr; return .5*pi()-Asin(x)
AcosD: return r2d(Acos(arg(1)))
AsinD: return r2d(Asin(arg(1)))
cosD: return cos(d2r(arg(1)))
sinD: return sin(d2r(d2d(arg(1))))
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 /*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►1radians ──►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