## Posted By

Scooter on 03/12/08

## Who likes this?

3 people have marked this snippet as a favorite

# Trigonometry Library

/ Published in: PHP

Library of trigonometry functions, both circular and hyperbolic.

`<?php    // PHP Mathematics Library - Trigonometry    //    // Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.    //    // This work is licensed under the Creative Commons Attribution License. To view    // a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or    // send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California    // 94305, USA.     // CIRCULAR FUNCTIONS     // There are six basic trigonometric functions, three of which (sine, cosine, and tangent) are already defined in PHP.    // function sin() - Returns the sine of the specified angle.    // function cos() - Returns the cosine of the specified angle.    // function tan() - Returns the tangent of the specified angle.     // The other three basic trigonometric functions are the multiplicative inverses of the previous three functions.     // Returns the cosecant of the specified angle.    function csc(\$x)    {        return (1 / sin(\$x));    }     // Returns the secant of the specified angle.    function sec(\$x)    {        return (1 / cos(\$x));    }     // Returns the cotangent of the specified angle.    function cot(\$x)    {        return (1 / tan(\$x));    }     // The following trigonometric functions are rarely used anymore, but are included in this function library for completeness.     // Returns the versed sine (also called versine) of the specified angle.    function versin(\$x)    {        return (1 - cos(\$x));    }     // Returns the coversed sine (also called coversine) of the specified angle.    function coversin(\$x)    {        return (1 - sin(\$x));    }     // Returns the haversed sine (also called haversine) of the specified angle.    function haversin(\$x)    {        return (versin(\$x) / 2);    }     // Returns the hacoversed sine (also called hacoversine, cohaversine, or havercosine) of the specified angle.    function hacoversin(\$x)    {        return (coversin(\$x) / 2);    }     // Returns the exsecant of the specified angle.    function exsec(\$x)    {        return (sec(\$x) - 1);    }     // Returns the excosecant of the specified angle.    function excsc(\$x)    {        return (csc(\$x) - 1);    }     // Each of the six basic trigonometric functions also has an inverse function, three of which are already defined in PHP.    // function asin() - Returns the angle whose sine is the specified whole number.    // function acos() - Returns the angle whose cosine is the specified number.    // function atan() - Returns the angle whose tangent is the specified number.     // Returns the angle whose cosecant is the specified number.    function acsc(\$x)    {        return (atan(\$x / sqrt(\$x * \$x - 1)) + (sin(\$x) - 1) * (2 * atan(1)));    }     // Returns the angle whose secant is the specified number.    function asec(\$x)    {        return (atan(\$x / sqr(\$x * \$x - 1)) + sin((\$x) - 1) * (2 * atan(1)));    }     // Returns the angle whose cotangent is the specified number.    function acot(\$x)    {        return (atan(\$x) + 2 * atan(1));    }     // HYPERBOLIC FUNCTIONS     // The hyperbolic functions are like the basic trigonometric functions, but for hyperbola instead of circles.    // function sinh() - Returns the hyperbolic sine of the specified angle.    // function cosh() - Returns the hyperbolic cosine of the specified angle.    // function tanh() - Returns the hyperbolic tangent of the specified angle.     // Returns the hyperbolic cosecant of the specified angle.    function csch(\$x)    {        return (2 / (exp(\$x) - exp(-\$x)));    }     // Returns the hyperbolic secant of the specified angle.    function sech(\$x)    {        return (2 / (exp(\$x) + exp(-\$x)));    }     // Returns the hyperbolic cotangent of the specified angle.    function coth(\$x)    {        return (exp(\$x) + exp(-\$x)) / (exp(\$x) - exp(-\$x));    }     // Just like in trigonometry, the six basic hyperbolic functions have inverse functions.    // function asinh() - Returns the angle whose hyperbolic sine is the specified number.    // function acosh() - Returns the angle whose hyperbolic cosine is the specified number.    // function atanh() - Returns the angle whose hyperbolic tangent is the specified number.     // Returns the angle whose hyperbolic cosecant is the specified number.    function acsch(\$x)    {        return (log((sin(\$x) * sqrt(\$x * \$x + 1) + 1) / \$x));    }     // Returns the angle whose hyperbolic secant is the specified number.    function asech(\$x)    {        return (log((sqrt(-\$x * \$x + 1) + 1) / \$x));    }     // Returns the angle whose hyperbolic cotangent is the specified number.    function acoth(\$x)    {        return (log((\$x + 1) / (\$x - 1)) / 2);    }      // GUDERMANNIAN FUNCTIONS     // The Gudermannian function relates the circular and hyperbolic trigonometric functions without resorting to complex numbers.    function gd(\$x)    {        return (2 * atan(tanh(\$x / 2)));    }     // Like other trigonometric and hyperbolic functions, Gudermannian has an inverse.    function agd(\$x)    {        return atanh(sin(\$x));    }?>`