Jump to content

Trigonometric functions: Difference between revisions

m
→‎{{header|REXX}}: optimized a function.
m (→‎{{header|REXX}}: optimized a function.)
Line 3,988:
functions are wanted, you have to roll your own.
Some of the normal/regular trigonometric functions are included here.
┌──────────────────────────────────────────────────────────────────────────┐
│ One common method that ensures enough accuracy in REXX is specifying │
│ more precision (via NUMERIC DIGITS nnn) than is needed, and then │
│ displaying the number of digits that are desired, or the number(s) │
│ could be re-normalized using the FORMAT BIF. │
│ │
│ The technique used (below) is to set the numeric digits ten higher │
│ than the desired digits, as specified by the SHOWDIGS variable. │
└──────────────────────────────────────────────────────────────────────────┘
Most math (POW, EXP, LOG, LN, GAMMA, etc.), trigonometric, and hyperbolic functions need only five extra digits, but ten
<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
Line 4,050 ⟶ 4,051:
AcosErr: call tellErr 'Acos(x), X must be in the range of -1 ──► +1, X=' || x
/*──────────────────────────────────────────────────────────────────────────────────────*/
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); hpi= pi * .5
numeric fuzz min(6, digits() - 3); if a=pi() then return -1
if a=hpi | a=hpi*3 then return 0; if a=pi() / 3 then return .5
if a=pi() * 2 / 3 then return -.5; return .sinCos(1, -1)
/*──────────────────────────────────────────────────────────────────────────────────────*/
sin: procedure; parse arg x; x=r2r(x); numeric fuzz min(5, max(1, digits() -3))
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, 1)
/*──────────────────────────────────────────────────────────────────────────────────────*/
.sinCos: parse arg z 1 _,i; q= x*x
do k=2 by 2 until p=z; p= z; _= - _ * q / (k * (k+i) ); z= z + _; end
return z
/*──────────────────────────────────────────────────────────────────────────────────────*/
Cookies help us deliver our services. By using our services, you agree to our use of cookies.