This chapter describes basic mathematical functions.  Some of these functions are present in system libraries, but the alternative versions given here can be used as a substitute when the system functions are not available.

The functions and macros described in this chapter are defined in the header file gsl_math.h.

Mathematical Constants
======================

The library ensures that the standard BSD mathematical constants are defined. For reference, here is a list of the constants:

M_E
     The base of exponentials, e

M_LOG2E
     The base-2 logarithm of e, \log_2 (e)

M_LOG10E
     The base-10 logarithm of e, $\log_10(e)$ \log_10 (e)

M_SQRT2
     The square root of two, \sqrt 2

M_SQRT1_2
     The square root of one-half, $\sqrt1/2$ \sqrt{1/2}

M_SQRT3
     The square root of three, \sqrt 3

M_PI
     The constant pi, \pi

M_PI_2
     Pi divided by two, \pi/2

M_PI_4
     Pi divided by four, \pi/4

M_SQRTPI
     The square root of pi, \sqrt\pi

M_2_SQRTPI
     Two divided by the square root of pi, 2/\sqrt\pi

M_1_PI
     The reciprocal of pi, 1/\pi

M_2_PI
     Twice the reciprocal of pi, 2/\pi

M_LN10
     The natural logarithm of ten, \ln(10)

M_LN2
     The natural logarithm of two, \ln(2)

M_LNPI
     The natural logarithm of pi, \ln(\pi)

M_EULER
     Euler's constant, \gamma

Infinities and Not-a-number
===========================

 -- Macro: GSL_POSINF
     This macro contains the IEEE representation of positive infinity, +\infty. It is computed from the expression +1.0/0.0.

 -- Macro: GSL_NEGINF
     This macro contains the IEEE representation of negative infinity, -\infty. It is computed from the expression -1.0/0.0.

 -- Macro: GSL_NAN
     This macro contains the IEEE representation of the Not-a-Number symbol, NaN. It is computed from the ratio 0.0/0.0.

 -- Function: int gsl_isnan (const double X)
     This function returns 1 if X is not-a-number.

 -- Function: int gsl_isinf (const double X)
     This function returns +1 if X is positive infinity, -1 if X is negative infinity and 0 otherwise. (Note that the C99 standard only requires the system isinf function to return a non-zero value, without the sign of the infinity.  The implementation in some earlier versions of GSL used the system isinf function and may have this behavior on some platforms.  Therefore, it is advisable to test the sign of X separately, if needed, rather than relying the sign of the return value from gsl_isinf().)

 -- Function: int gsl_finite (const double X)
     This function returns 1 if X is a real number, and 0 if it is infinite or not-a-number.

Elementary Functions
====================

The following routines provide portable implementations of functions found in the BSD math library.  When native versions are not available the functions described here can be used instead.  The substitution can be made automatically if you use autoconf to compile your application (see Portability functions).

 -- Function: double gsl_log1p (const double X)
     This function computes the value of \log(1+x) in a way that is accurate for small X. It provides an alternative to the BSD math function log1p(x).

 -- Function: double gsl_expm1 (const double X)
     This function computes the value of \exp(x)-1 in a way that is accurate for small X. It provides an alternative to the BSD math function expm1(x).

 -- Function: double gsl_hypot (const double X, const double Y)
     This function computes the value of $\sqrtx^2 + y^2$ \sqrt{x^2 + y^2} in a way that avoids overflow. It provides an alternative to the BSD math function hypot(x,y).

 -- Function: double gsl_hypot3 (const double X, const double Y, const double Z)
     This function computes the value of $\sqrtx^2 + y^2 + z^2$ \sqrt{x^2 + y^2 + z^2} in a way that avoids overflow.

 -- Function: double gsl_acosh (const double X)
     This function computes the value of \arccosh(x). It provides an alternative to the standard math function acosh(x).

 -- Function: double gsl_asinh (const double X)
     This function computes the value of \arcsinh(x). It provides an alternative to the standard math function asinh(x).

 -- Function: double gsl_atanh (const double X)
     This function computes the value of \arctanh(x). It provides an alternative to the standard math function atanh(x).

 -- Function: double gsl_ldexp (double X, int E)
     This function computes the value of x * 2^e. It provides an alternative to the standard math function ldexp(x,e).

 -- Function: double gsl_frexp (double X, int * E)
     This function splits the number x into its normalized fraction f and exponent e, such that x = f * 2^e and $0.5 \le f < 1$ 0.5 <= f < 1. The function returns f and stores the exponent in e. If x is zero, both f and e are set to zero. This function provides an alternative to the standard math function frexp(x, e).

Small integer powers
====================

A common complaint about the standard C library is its lack of a function for calculating (small) integer powers.  GSL provides some simple functions to fill this gap.  For reasons of efficiency, these functions do not check for overflow or underflow conditions.

 -- Function: double gsl_pow_int (double X, int N)
     This routine computes the power x^n for integer N.  The power is computed efficiently---for example, x^8 is computed as ((x^2)^2)^2, requiring only 3 multiplications.  A version of this function which also computes the numerical error in the result is available as gsl_sf_pow_int_e.

 -- Function: double gsl_pow_2 (const double X)
 -- Function: double gsl_pow_3 (const double X)
 -- Function: double gsl_pow_4 (const double X)
 -- Function: double gsl_pow_5 (const double X)
 -- Function: double gsl_pow_6 (const double X)
 -- Function: double gsl_pow_7 (const double X)
 -- Function: double gsl_pow_8 (const double X)
 -- Function: double gsl_pow_9 (const double X)
     These functions can be used to compute small integer powers x^2, x^3, etc. efficiently. The functions will be inlined when HAVE_INLINE is defined, so that use of these functions should be as efficient as explicitly writing the corresponding product expression.

     #include <gsl/gsl_math.h>
     double y = gsl_pow_4 (3.141)  /* compute 3.141**4 */

Testing the Sign of Numbers
===========================

 -- Macro: GSL_SIGN (x)
     This macro returns the sign of X. It is defined as ((x) >= 0 ? 1 : -1). Note that with this definition the sign of zero is positive (regardless of its IEEE sign bit).

Testing for Odd and Even Numbers
================================

 -- Macro: GSL_IS_ODD (n)
     This macro evaluates to 1 if N is odd and 0 if N is even. The argument N must be of integer type.

 -- Macro: GSL_IS_EVEN (n)
     This macro is the opposite of GSL_IS_ODD(n). It evaluates to 1 if N is even and 0 if N is odd. The argument N must be of integer type.

Maximum and Minimum functions
=============================

Note that the following macros perform multiple evaluations of their arguments, so they should not be used with arguments that have side effects (such as a call to a random number generator).

 -- Macro: GSL_MAX (a, b)
     This macro returns the maximum of A and B. It is defined as ((a) > (b) ? (a):(b)).

 -- Macro: GSL_MIN (a, b)
     This macro returns the minimum of A and B. It is defined as ((a) < (b) ? (a):(b)).

 -- Function: extern inline double GSL_MAX_DBL (double A, double B)
     This function returns the maximum of the double precision numbers A and B using an inline function. The use of a function allows for type checking of the arguments as an extra safety feature. On platforms where inline functions are not available the macro GSL_MAX will be automatically substituted.

 -- Function: extern inline double GSL_MIN_DBL (double A, double B)
     This function returns the minimum of the double precision numbers A and B using an inline function. The use of a function allows for type checking of the arguments as an extra safety feature. On platforms where inline functions are not available the macro GSL_MIN will be automatically substituted.

 -- Function: extern inline int GSL_MAX_INT (int A, int B)
 -- Function: extern inline int GSL_MIN_INT (int A, int B)
     These functions return the maximum or minimum of the integers A and B using an inline function.  On platforms where inline functions are not available the macros GSL_MAX or GSL_MIN will be automatically substituted.

 -- Function: extern inline long double GSL_MAX_LDBL (long double A, long double B)
 -- Function: extern inline long double GSL_MIN_LDBL (long double A, long double B)
     These functions return the maximum or minimum of the long doubles A and B using an inline function.  On platforms where inline functions are not available the macros GSL_MAX or GSL_MIN will be automatically substituted.

Approximate Comparison of Floating Point Numbers
================================================

It is sometimes useful to be able to compare two floating point numbers approximately, to allow for rounding and truncation errors.  The following function implements the approximate floating-point comparison algorithm proposed by D.E. Knuth in Section 4.2.2 of Seminumerical Algorithms (3rd edition).

 -- Function: int gsl_fcmp (double X, double Y, double EPSILON)
     This function determines whether x and y are approximately equal to a relative accuracy EPSILON.

     The relative accuracy is measured using an interval of size 2 \delta, where \delta = 2^k \epsilon and k is the maximum base-2 exponent of x and y as computed by the function frexp.

     If x and y lie within this interval, they are considered approximately equal and the function returns 0. Otherwise if x < y, the function returns -1, or if x > y, the function returns +1.

     Note that x and y are compared to relative accuracy, so this function is not suitable for testing whether a value is approximately zero.

     The implementation is based on the package fcmp by T.C. Belding.