Trigonometric functions: Difference between revisions

m
→‎{{header|REXX}}: optimized the COS and SIN functions, changed wording in the REXX section header.
No edit summary
m (→‎{{header|REXX}}: optimized the COS and SIN functions, changed wording in the REXX section header.)
Line 4,152:
 
=={{header|REXX}}==
The REXX language doesn't have any trig functions (or for that matter,   a square root [SQRT] function),   so if higher math
<br>functions are wanted, you'll have to roll your own. &nbsp; Some of the normal/regular trigonometric functions are included here.
a square root [SQRT] function), so if higher math <br>
┌──────────────────────────────────────────────────────────────────────────┐
functions are wanted, you have to roll your own.
│ One common method that ensures enough accuracy in REXX is specifying │
Some of the normal/regular trigonometric functions are included here.
│ more precision (via NUMERIC DIGITS nnn) than is needed, and then │
┌──────────────────────────────────────────────────────────────────────────┐
Onedisplaying the number commonof methoddigits that ensuresare enoughdesired, accuracy inor REXX isthe specifyingnumber(s)
morecould be re-normalized using the FORMAT BIF. 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-normalizedThe usingtechnique theused (below) FORMATis to BIF.set the numeric digits ten higher
than the desired digits, as specified by the SHOWDIGS variable.
└──────────────────────────────────────────────────────────────────────────┘
│ 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.
 
It should also be noted that both the &nbsp; '''pi''' &nbsp; and &nbsp; '''e''' &nbsp; constants have only around 77 decimal digits as included here, if more
<br>precision is needed, those constants should be extended. &nbsp; Both &nbsp; '''pi''' &nbsp; and &nbsp; '''e''' &nbsp; 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>of extended digits for &nbsp; '''pi'''&nbsp; or &nbsp; '''e''', I&nbsp; they could extendbe themextended to any almost any precision &nbsp; (as a REXX constant). &nbsp; Normally, a REXX
<br>a REXX (external) subroutine is used for such purposes so as to not make the program using the constant unwieldy large.
<lang rexx>/*REXX program demonstrates some common trig functions (30 decimal digits are shown).*/
showdigs= 25 /*show only 25 digits of number. */
Line 4,222 ⟶ 4,220:
return Asin(x / sqrt(1 + x*x) )
/*──────────────────────────────────────────────────────────────────────────────────────*/
cos: procedure; parse arg x; x= r2r(x); a=if abs(x);=0 then return 1; hpia= pi * .5abs(x)
numeric fuzz min(6, digits() - 3); if a=pi then return -1; pih= pi * .5
if a=hpipih | a=hpipih*3 then return 0; pit= pi/3; if a=pi / 3pit then return .5
if a=pipit *+ 2pit / 3 then return -.5; return .sinCos(1, -1)
/*──────────────────────────────────────────────────────────────────────────────────────*/
sin: procedure; parse arg x; x=r2r(x);if x=0 then return 0;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
/*──────────────────────────────────────────────────────────────────────────────────────*/
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); i=; m.=9; h= d+6
Line 4,255 ⟶ 4,253:
/*John Machin's formula is used for calculating more digits. */</lang>
Programming note:
╔═════════════════════════════════════════════════════════════════════════════╗
║ Functions that are not included here are (among others): ║
║ ║
║ some of the usual higher-math functions normally associated with trig ║
║ functions: POW, GAMMA, LGGAMMA, ERF, ERFC, ROOT, ATAN2, ║
║ LOG (LN), LOG2, LOG10, and all of the ║
║ hyperbolic trigonometric functions and their inverses (too many to list ║
║ here), ║
║ angle conversions/normalizations: degrees/radians/grads/mils: ║
║ a circle ≡ 2 pi radians ≡ 360 degrees ≡ 400 grads ≡ 6400 mils. ║
║ ║
║ Some of the other trigonometric functions are (hyphens added intentionally):║
║ ║
║ CHORD ║
║ COT (co-tangent) ║
║ CSC (co-secant) ║
║ CVC (co-versed cosine) ║
║ CVS (co-versed sine) ║
║ CXS (co-exsecant) ║
║ HAC (haver-cosine) ║
║ HAV (haver-sine ║
║ SEC (secant) ║
║ VCS (versed cosine or ver-cosine) ║
║ VSN (versed sine or ver-sine) ║
║ XCS (ex-secant) ║
║ COS/SIN/TAN cardinal (damped COS/SIN/TAN functions) ║
║ COS/SIN integral ║
║ ║
║ and all pertinent inverses of the above functions (AVSN, ACVS, ···). ║
╚═════════════════════════════════════════════════════════════════════════════╝
{{out|output}}