Trigonometric functions: Difference between revisions

m
→‎{{header|REXX}}: optimized some of the subroutines.
m ({{out}})
m (→‎{{header|REXX}}: optimized some of the subroutines.)
Line 3,319:
<br>extra digits is safer in case the argument is close to an asymptotic point or a multiple or fractional part of pi or somesuch.
<br>It should also be noted that both the '''pi''' and '''e''' constants have only around 77 decimal digits as included here, if more
<br>precision is needed, those constants should be extended. &nbsp; Both '''pi''' and '''e''' could've been shown with more precision,
<br>but having large precision numbers would add to this REXX program's length. &nbsp; If anybody wishes to see this REXX version of
<br>extended digits for '''pi''' or '''e''', I could extend them to any almost any precision (as a REXX constant). &nbsp; Normally, a REXX
<br>(external) subroutine is used for such purposes so as to not make the program using the constant unwieldlyunwieldy large.
<lang rexx>/*REXX program demonstrates some common trig functions (30 digits shown)*/
showdigs=30 /*show only 30 digits of number. */
Line 3,334:
' cos='show(cosD(J))
/*don't let TAN go postal.*/
if abs(j)\==90 then stuff=stuff ' tan='show(tanD(j))
say stuff
end /*j*/
Line 3,356:
 
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 $fuzz(5, 3)
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)
 
Line 3,375:
g=.5*(g+x/g); end /*k*/; numeric digits d; return g/1
 
e: e=2.7182818284590452353602874713526624977572470936999595749669676277240766303535
e: return,
return e /*Note: the actual E subroutine returns E's accuracy that */
2.7182818284590452353602874713526624977572470936999595749669676277240766303535
/*Note: the actual E subroutine returns E's accuracy that */
/*matches the current NUMERIC DIGITS, up to 1 million digits.*/
/*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
 
pi: pi=3.1415926535897932384626433832795028841971693993751058209749445923078164062862
pi: return, /*a bit of overkill, but hey !! */
return pi /*Note: the actual PI subroutine returns PI's accuracy that */
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. */
Line 3,406 ⟶ 3,404:
show: return left(left('',arg(1)>=0)format(arg(1),,showdigs)/1,showdigs)
tellErr: say; say '*** error! ***'; say; say arg(1); say; exit 13
tanErr: call tellErr 'tan('||x") causes division by zero, X=" || x
AsinErr: call tellErr 'Asin(x), X must be in the range of -1 ──► +1, X=' || x
AcosErr: call tellErr 'Acos(x), X must be in the range of -1 ──► +1, X=' || x
sqrtErr: call tellErr "sqrt(x), X can't be negative, X=" || x</lang>
Programming note:
/* ┌───────────────────────────────────────────────────────────────┐
<pre>
/* ┌───────────────────────────────────────────────────────────────┐
│ Not included here are: (among others): │
│ some of the usual higher-math functions normally associated │
Line 3,419:
│ Angle conversions/normalizations: degrees/radians/grads/mils │
│ [a circle = 2 pi radians, 360 degrees, 400 grads, 6400 mils].│
Some of the other trig functions (hypens added intentially):
│ Some of the other trigonometric functions (hyphens added │
│ intentionally): │
│ │
│ CHORD │
│ COT (co-tangent) │
Line 3,434 ⟶ 3,437:
│ COS/SIN/TAN cardinal (damped COS/SIN/TAN function) │
│ COS/SIN integral │
│ and all pertinent of the above's inverses (AVSN, ACVS...···) │
└───────────────────────────────────────────────────────────────┘ */</lang>
</pre>
{{out}}
<pre>