Code - Math - Trigonometry

Circular Functions

Both ASP and PHP contain the three basic trigonometry functions: sine, cosine, and tangent. There are three more basic trigonometry functions: cosecant, secant, and cotangent.

Cosecant
ASP PHP
  1. function csc(x)
  2.     csc = 1 / sin(x)
  3. end function
  1. function csc($x)
  2. {
  3.     return (1 / sin($x));
  4. }
Secant
ASP PHP
  1. function sec(x)
  2.     sec = 1 / cos(x)
  3. end function
  1. function sec($x)
  2. {
  3.     return (1 / cos($x));
  4. }
Cotangent
ASP PHP
  1. function cot(x)
  2.     cot = 1 / tan(x)
  3. end function
  1. function cot($x)
  2. {
  3.     return (1 / tan($x));
  4. }

Besides the basic trigonmetric functions, there are some special functions which are rarely used anymore: versed sine, coversed sine, haversed sine, hacoversed sine, exsecant, and excosecant.

Versed Sine
ASP PHP
  1. function versin(x)
  2.     versin = 1 - cos(x)
  3. end function
  1. function versin($x)
  2. {
  3.     return (1 - cos($x));
  4. }
Coversed Sine
ASP PHP
  1. function coversin(x)
  2.     coversin = 1 - sin(x)
  3. end function
  1. function coversin($x)
  2. {
  3.     return (1 - sin($x));
  4. }
Haversed Sine
ASP PHP
  1. function haversin(x)
  2.     haversin = versin(x) / 2
  3. end function
  1. function haversin($x)
  2. {
  3.     return (versin($x) / 2);
  4. }
Hacoversed Sine
ASP PHP
  1. function hacoversin(x)
  2.     hacoversin = coversin(x) / 2
  3. end function
  1. function hacoversin($x)
  2. {
  3.     return (coversin($x) / 2);
  4. }
Exsecant
ASP PHP
  1. function exsec(x)
  2.     exsec = sec(x) - 1
  3. end function
  1. function exsec($x)
  2. {
  3.     return (sec($x) - 1);
  4. }
Excosecant
ASP PHP
  1. function excsc(x)
  2.     excsc = csc(x) - 1
  3. end function
  1. function excsc($x)
  2. {
  3.     return (csc($x) - 1);
  4. }

Each of the six basic trigonometric functions has an inverse function. The inverse functions for sine, cosine, and tangent are already defined natively in PHP. The inverse function for tangent is defined in ASP, but we will create an alias for it to be consistent with the common abbreviation.

PHP defines several mathematical constants, one of which we'll be using here. M_PI_2 is equal to π divided by 2. If you're using ASP, you'll need to define this constant yourself.

Constants
ASP
  1. Const M_PI = 3.14159265358979323846
  2. Const M_PI_2 = M_PI / 2
Arcsine
ASP
  1. function asin(x)
  2.     asin = atn(x / sqr(1 - x ^ 2))
  3. end function
Arccosine
ASP
  1. function acos(x)
  2.     acos = M_PI_2 - asin(x)
  3. end function
Arctangent
ASP
  1. function atan(x)
  2.     atan = atn(x)
  3. end function
Arccosecant
ASP
  1. function acsc(x)
  2.     acsc = asin(1 / x)
  3. end function
PHP
  1. function acsc($x)
  2. {
  3.     return asin(1 / $x);
  4. }
Arcsecant
ASP
  1. function asec(x)
  2.     asec = M_PI_2 - acsc(x)
  3. end function
PHP
  1. function asec($x)
  2. {
  3.     return (M_PI_2 - acsc($x));
  4. }
Arccotangent
ASP
  1. function acot(x)
  2.     acot = M_PI_2 - atn(x)
  3. end function
PHP
  1. function acot($x)
  2. {
  3.     return (M_PI_2 - atan($x));
  4. }

Hyperbolic Functions

The hyperbolic functions are like the circular trigonometric functions, but for hyperbola instead of circles. The hyperbolic functions for sine, cosine, and tangent are already defined natively in PHP.

Hyperbolic Sine
ASP
  1. function sinh(x)
  2.     sinh = (exp(x) - exp(-x)) / 2
  3. end function
Hyperbolic Cosine
ASP
  1. function cosh(x)
  2.     cosh = (exp(x) + exp(-x)) / 2
  3. end function
Hyperbolic Tangent
ASP
  1. function tanh(x)
  2.     tanh = (exp(x) - exp(-x)) / (exp(x) + exp(-x))
  3. end function
Hyperbolic Cosecant
ASP
  1. function csch(x)
  2.     csch = 2 / (exp(x) - exp(-x))
  3. end function
PHP
  1. function csch($x)
  2. {
  3.     return (2 / (exp($x) - exp(-$x)));
  4. }
Hyperbolic Secant
ASP
  1. function sech(x)
  2.     sech = (2 / (exp(x) + exp(-x)))
  3. end function
PHP
  1. function sech($x)
  2. {
  3.     return (2 / (exp($x) + exp(-$x)));
  4. }
Hyperbolic Cotangent
ASP
  1. function coth(x)
  2.     coth = (exp(x) + exp(-x)) / (exp(x) - exp(-x))
  3. end function
PHP
  1. function coth($x)
  2. {
  3.     return (exp($x) + exp(-$x)) / (exp($x) - exp(-$x));
  4. }

As with regular trigonometry, each hyperbolic function has an inverse. The inverse hyperbolic functions for sine, cosine, and tangent are already defined natively in PHP.

Inverse Hyperbolic Sine
ASP
  1. function asinh(x)
  2.     asinh = log(x + sqr(x * x + 1))
  3. end function
Inverse Hyperbolic Cosine
ASP
  1. function acosh(x)
  2.     acosh = log(x + sqr(x * x - 1))
  3. end function
Inverse Hyperbolic Tangent
ASP
  1. function atanh(x)
  2.     atanh = log((1 + x) / (1 - x)) / 2
  3. end function
Inverse Hyperbolic Cosecant
ASP
  1. function acsch(x)
  2.     acsch = log((sin(x) * sqr(x * x + 1) + 1) / x)
  3. end function
PHP
  1. function acsch($x)
  2. {
  3.     return (log((sin($x) * sqrt($x * $x + 1) + 1) / $x));
  4. }
Inverse Hyperbolic Secant
ASP
  1. function asech(x)
  2.     asech = log((sqr(-x * x + 1) + 1) / x)
  3. end function
PHP
  1. function asech($x)
  2. {
  3.     return (log((sqrt(-$x * $x + 1) + 1) / $x));
  4. }
Inverse Hyperbolic Cotangent
ASP
  1. function acoth(x)
  2.     acoth = log((x + 1) / (x - 1)) / 2
  3. end function
PHP
  1. function acoth($x)
  2. {
  3.     return (log(($x + 1) / ($x - 1)) / 2);
  4. }

Gudermannian Functions

The Gudermannian function relates the circular and hyperbolic trigonometric functions without resorting to complex numbers. It also has an inverse.

Gudermannian Function
ASP PHP
  1. function gd(x)
  2.     gd = 2 * atn(tanh(x / 2))
  3. end function
  1. function gd($x)
  2. {
  3.     return (2 * atan(tanh($x / 2)));
  4. }
Inverse Gudermannian Function
ASP PHP
  1. function agd(x)
  2.     agd = atanh(sin(x))
  3. end function
  1. function agd($x)
  2. {
  3.     return atanh(sin($x));
  4. }