Mathematics
***********

This chapter contains information about functions for performing mathematical computations, such as trigonometric functions.  Most of these functions have prototypes declared in the header file math.h.  The complex-valued functions are defined in complex.h.

All mathematical functions which take a floating-point argument have three variants, one each for double, float, and long double arguments.  The double versions are mostly defined in ISO C89.  The float and long double versions are from the numeric extensions to C included in ISO C99.

Which of the three versions of a function should be used depends on the situation.  For most calculations, the float functions are the fastest.  On the other hand, the long double functions have the highest precision.  double is somewhere in between.  It is usually wise to pick the narrowest type that can accommodate your data. Not all machines have a distinct long double type; it may be the same as double.

Theglibc also provides _FloatN and _FloatNx types.  These types are defined in ISO/IEC TS 18661-3, which extends ISO C and defines floating-point types that are not machine-dependent.  When such a type, such as _Float128, is supported by theglibc, extra variants for most of the mathematical functions provided for double, float, and long double are also provided for the supported type.  Throughout this manual, the _FloatN and _FloatNx variants of these functions are described along with the double, float, and long double variants and they come from ISO/IEC TS 18661-3, unless explicitly stated otherwise.

Support for _FloatN or _FloatNx types is provided for _Float32, _Float64 and _Float32x on all platforms. It is also provided for _Float128 and _Float64x on powerpc64le (PowerPC 64-bits little-endian), x86_64, x86, ia64, aarch64, alpha, mips64, riscv, s390 and sparc.

Predefined Mathematical Constants
=================================

The header math.h defines several useful mathematical constants. All values are defined as preprocessor macros starting with M_. The values provided are:

M_E
     The base of natural logarithms.

M_LOG2E
     The logarithm to base 2 of M_E.

M_LOG10E
     The logarithm to base 10 of M_E.

M_LN2
     The natural logarithm of 2.

M_LN10
     The natural logarithm of 10.

M_PI
     Pi, the ratio of a circle's circumference to its diameter.

M_PI_2
     Pi divided by two.

M_PI_4
     Pi divided by four.

M_1_PI
     The reciprocal of pi (1/pi)

M_2_PI
     Two times the reciprocal of pi.

M_2_SQRTPI
     Two times the reciprocal of the square root of pi.

M_SQRT2
     The square root of two.

M_SQRT1_2
     The reciprocal of the square root of two (also the square root of 1/2).

These constants come from the Unix98 standard and were also available in 4.4BSD; therefore they are only defined if _XOPEN_SOURCE=500, or a more general feature select macro, is defined.  The default set of features includes these constants. See Feature Test Macros.

All values are of type double.  As an extension, theglibc also defines these constants with type long double and float.  The long double macros have a lowercase l while the float macros have a lowercase f appended to their names: M_El, M_PIl, and so forth.  These are only available if _GNU_SOURCE is defined.

Likewise, theglibc also defines these constants with the types _FloatN and _FloatNx for the machines that have support for such types enabled (see Mathematics) and if _GNU_SOURCE is defined.  When available, the macros names are appended with fN or fNx, such as f128 for the type _Float128.

Note: Some programs use a constant named PI which has the same value as M_PI.  This constant is not standard; it may have appeared in some old AT&T headers, and is mentioned in Stroustrup's book on C++.  It infringes on the user's name space, so theglibc does not define it.  Fixing programs written to expect it is simple: replace PI with M_PI throughout, or put -DPI=M_PI on the compiler command line.

Trigonometric Functions
=======================

These are the familiar sin, cos, and tan functions. The arguments to all of these functions are in units of radians; recall that pi radians equals 180 degrees.

The math library normally defines M_PI to a double approximation of pi.  If strict ISO and/or POSIX compliance are requested this constant is not defined, but you can easily define it yourself:

     #define M_PI 3.14159265358979323846264338327

You can also compute the value of pi with the expression acos (-1.0).

 -- Function: double sin (double X)
 -- Function: float sinf (float X)
 -- Function: long double sinl (long double X)
 -- Function: _FloatN sinfN (_FloatN X)
 -- Function: _FloatNx sinfNx (_FloatNx X)
     ISO, math.h sinfN, TS 18661-3:2015, math.h sinfNx, TS 18661-3:2015, math.h These functions return the sine of X, where X is given in radians.  The return value is in the range -1 to 1.

 -- Function: double cos (double X)
 -- Function: float cosf (float X)
 -- Function: long double cosl (long double X)
 -- Function: _FloatN cosfN (_FloatN X)
 -- Function: _FloatNx cosfNx (_FloatNx X)
     ISO, math.h cosfN, TS 18661-3:2015, math.h cosfNx, TS 18661-3:2015, math.h These functions return the cosine of X, where X is given in radians.  The return value is in the range -1 to 1.

 -- Function: double tan (double X)
 -- Function: float tanf (float X)
 -- Function: long double tanl (long double X)
 -- Function: _FloatN tanfN (_FloatN X)
 -- Function: _FloatNx tanfNx (_FloatNx X)
     ISO, math.h tanfN, TS 18661-3:2015, math.h tanfNx, TS 18661-3:2015, math.h These functions return the tangent of X, where X is given in radians.

     Mathematically, the tangent function has singularities at odd multiples of pi/2.  If the argument X is too close to one of these singularities, tan will signal overflow.

In many applications where sin and cos are used, the sine and cosine of the same angle are needed at the same time.  It is more efficient to compute them simultaneously, so the library provides a function to do that.

 -- Function: void sincos (double X, double *SINX, double *COSX)
 -- Function: void sincosf (float X, float *SINX, float *COSX)
 -- Function: void sincosl (long double X, long double *SINX, long double *COSX)
 -- Function: _FloatN sincosfN (_FloatN X, _FloatN *SINX, _FloatN *COSX)
 -- Function: _FloatNx sincosfNx (_FloatNx X, _FloatNx *SINX, _FloatNx *COSX)
     GNU, math.h These functions return the sine of X in *SINX and the cosine of X in *COSX, where X is given in radians.  Both values, *SINX and *COSX, are in the range of -1 to 1.

     All these functions, including the _FloatN and _FloatNx variants, are GNU extensions.  Portable programs should be prepared to cope with their absence.

ISO C99 defines variants of the trig functions which work on complex numbers.  Theglibc provides these functions, but they are only useful if your compiler supports the new complex types defined by the standard. (As of this writing GCC supports complex numbers, but there are bugs in the implementation.)

 -- Function: complex double csin (complex double Z)
 -- Function: complex float csinf (complex float Z)
 -- Function: complex long double csinl (complex long double Z)
 -- Function: complex _FloatN csinfN (complex _FloatN Z)
 -- Function: complex _FloatNx csinfNx (complex _FloatNx Z)
     ISO, complex.h csinfN, TS 18661-3:2015, complex.h csinfNx, TS 18661-3:2015, complex.h These functions return the complex sine of Z. The mathematical definition of the complex sine is

     sin (z) = 1/(2*i) * (exp (z*i) - exp (-z*i)).

 -- Function: complex double ccos (complex double Z)
 -- Function: complex float ccosf (complex float Z)
 -- Function: complex long double ccosl (complex long double Z)
 -- Function: complex _FloatN ccosfN (complex _FloatN Z)
 -- Function: complex _FloatNx ccosfNx (complex _FloatNx Z)
     ISO, complex.h ccosfN, TS 18661-3:2015, complex.h ccosfNx, TS 18661-3:2015, complex.h These functions return the complex cosine of Z. The mathematical definition of the complex cosine is

     cos (z) = 1/2 * (exp (z*i) + exp (-z*i))

 -- Function: complex double ctan (complex double Z)
 -- Function: complex float ctanf (complex float Z)
 -- Function: complex long double ctanl (complex long double Z)
 -- Function: complex _FloatN ctanfN (complex _FloatN Z)
 -- Function: complex _FloatNx ctanfNx (complex _FloatNx Z)
     ISO, complex.h ctanfN, TS 18661-3:2015, complex.h ctanfNx, TS 18661-3:2015, complex.h These functions return the complex tangent of Z. The mathematical definition of the complex tangent is

     tan (z) = -i * (exp (z*i) - exp (-z*i)) / (exp (z*i) + exp (-z*i))

     The complex tangent has poles at pi/2 + 2n, where n is an integer.  ctan may signal overflow if Z is too close to a pole.

Inverse Trigonometric Functions
===============================

These are the usual arcsine, arccosine and arctangent functions, which are the inverses of the sine, cosine and tangent functions respectively.

 -- Function: double asin (double X)
 -- Function: float asinf (float X)
 -- Function: long double asinl (long double X)
 -- Function: _FloatN asinfN (_FloatN X)
 -- Function: _FloatNx asinfNx (_FloatNx X)
     ISO, math.h asinfN, TS 18661-3:2015, math.h asinfNx, TS 18661-3:2015, math.h These functions compute the arcsine of X---that is, the value whose sine is X.  The value is in units of radians.  Mathematically, there are infinitely many such values; the one actually returned is the one between -pi/2 and pi/2 (inclusive).

     The arcsine function is defined mathematically only over the domain -1 to 1.  If X is outside the domain, asin signals a domain error.

 -- Function: double acos (double X)
 -- Function: float acosf (float X)
 -- Function: long double acosl (long double X)
 -- Function: _FloatN acosfN (_FloatN X)
 -- Function: _FloatNx acosfNx (_FloatNx X)
     ISO, math.h acosfN, TS 18661-3:2015, math.h acosfNx, TS 18661-3:2015, math.h These functions compute the arccosine of X---that is, the value whose cosine is X.  The value is in units of radians. Mathematically, there are infinitely many such values; the one actually returned is the one between 0 and pi (inclusive).

     The arccosine function is defined mathematically only over the domain -1 to 1.  If X is outside the domain, acos signals a domain error.

 -- Function: double atan (double X)
 -- Function: float atanf (float X)
 -- Function: long double atanl (long double X)
 -- Function: _FloatN atanfN (_FloatN X)
 -- Function: _FloatNx atanfNx (_FloatNx X)
     ISO, math.h atanfN, TS 18661-3:2015, math.h atanfNx, TS 18661-3:2015, math.h These functions compute the arctangent of X---that is, the value whose tangent is X.  The value is in units of radians. Mathematically, there are infinitely many such values; the one actually returned is the one between -pi/2 and pi/2 (inclusive).

 -- Function: double atan2 (double Y, double X)
 -- Function: float atan2f (float Y, float X)
 -- Function: long double atan2l (long double Y, long double X)
 -- Function: _FloatN atan2fN (_FloatN Y, _FloatN X)
 -- Function: _FloatNx atan2fNx (_FloatNx Y, _FloatNx X)
     ISO, math.h atan2fN, TS 18661-3:2015, math.h atan2fNx, TS 18661-3:2015, math.h This function computes the arctangent of Y/X, but the signs of both arguments are used to determine the quadrant of the result, and X is permitted to be zero.  The return value is given in radians and is in the range -pi to pi, inclusive.

     If X and Y are coordinates of a point in the plane, atan2 returns the signed angle between the line from the origin to that point and the x-axis.  Thus, atan2 is useful for converting Cartesian coordinates to polar coordinates.  (To compute the radial coordinate, use hypot; see Exponents and Logarithms.)

     If both X and Y are zero, atan2 returns zero.

ISO C99 defines complex versions of the inverse trig functions.

 -- Function: complex double casin (complex double Z)
 -- Function: complex float casinf (complex float Z)
 -- Function: complex long double casinl (complex long double Z)
 -- Function: complex _FloatN casinfN (complex _FloatN Z)
 -- Function: complex _FloatNx casinfNx (complex _FloatNx Z)
     ISO, complex.h casinfN, TS 18661-3:2015, complex.h casinfNx, TS 18661-3:2015, complex.h These functions compute the complex arcsine of Z---that is, the value whose sine is Z.  The value returned is in radians.

     Unlike the real-valued functions, casin is defined for all values of Z.

 -- Function: complex double cacos (complex double Z)
 -- Function: complex float cacosf (complex float Z)
 -- Function: complex long double cacosl (complex long double Z)
 -- Function: complex _FloatN cacosfN (complex _FloatN Z)
 -- Function: complex _FloatNx cacosfNx (complex _FloatNx Z)
     ISO, complex.h cacosfN, TS 18661-3:2015, complex.h cacosfNx, TS 18661-3:2015, complex.h These functions compute the complex arccosine of Z---that is, the value whose cosine is Z.  The value returned is in radians.

     Unlike the real-valued functions, cacos is defined for all values of Z.

 -- Function: complex double catan (complex double Z)
 -- Function: complex float catanf (complex float Z)
 -- Function: complex long double catanl (complex long double Z)
 -- Function: complex _FloatN catanfN (complex _FloatN Z)
 -- Function: complex _FloatNx catanfNx (complex _FloatNx Z)
     ISO, complex.h catanfN, TS 18661-3:2015, complex.h catanfNx, TS 18661-3:2015, complex.h These functions compute the complex arctangent of Z---that is, the value whose tangent is Z.  The value is in units of radians.

Exponentiation and Logarithms
=============================

 -- Function: double exp (double X)
 -- Function: float expf (float X)
 -- Function: long double expl (long double X)
 -- Function: _FloatN expfN (_FloatN X)
 -- Function: _FloatNx expfNx (_FloatNx X)
     ISO, math.h expfN, TS 18661-3:2015, math.h expfNx, TS 18661-3:2015, math.h These functions compute e (the base of natural logarithms) raised to the power X.

     If the magnitude of the result is too large to be representable, exp signals overflow.

 -- Function: double exp2 (double X)
 -- Function: float exp2f (float X)
 -- Function: long double exp2l (long double X)
 -- Function: _FloatN exp2fN (_FloatN X)
 -- Function: _FloatNx exp2fNx (_FloatNx X)
     ISO, math.h exp2fN, TS 18661-3:2015, math.h exp2fNx, TS 18661-3:2015, math.h These functions compute 2 raised to the power X. Mathematically, exp2 (x) is the same as exp (x * log (2)).

 -- Function: double exp10 (double X)
 -- Function: float exp10f (float X)
 -- Function: long double exp10l (long double X)
 -- Function: _FloatN exp10fN (_FloatN X)
 -- Function: _FloatNx exp10fNx (_FloatNx X)
     ISO, math.h exp10fN, TS 18661-4:2015, math.h exp10fNx, TS 18661-4:2015, math.h These functions compute 10 raised to the power X. Mathematically, exp10 (x) is the same as exp (x * log (10)).

     The exp10 functions are from TS 18661-4:2015.

 -- Function: double log (double X)
 -- Function: float logf (float X)
 -- Function: long double logl (long double X)
 -- Function: _FloatN logfN (_FloatN X)
 -- Function: _FloatNx logfNx (_FloatNx X)
     ISO, math.h logfN, TS 18661-3:2015, math.h logfNx, TS 18661-3:2015, math.h These functions compute the natural logarithm of X.  exp (log (X)) equals X, exactly in mathematics and approximately in C.

     If X is negative, log signals a domain error.  If X is zero, it returns negative infinity; if X is too close to zero, it may signal overflow.

 -- Function: double log10 (double X)
 -- Function: float log10f (float X)
 -- Function: long double log10l (long double X)
 -- Function: _FloatN log10fN (_FloatN X)
 -- Function: _FloatNx log10fNx (_FloatNx X)
     ISO, math.h log10fN, TS 18661-3:2015, math.h log10fNx, TS 18661-3:2015, math.h These functions return the base-10 logarithm of X. log10 (X) equals log (X) / log (10).

 -- Function: double log2 (double X)
 -- Function: float log2f (float X)
 -- Function: long double log2l (long double X)
 -- Function: _FloatN log2fN (_FloatN X)
 -- Function: _FloatNx log2fNx (_FloatNx X)
     ISO, math.h log2fN, TS 18661-3:2015, math.h log2fNx, TS 18661-3:2015, math.h These functions return the base-2 logarithm of X. log2 (X) equals log (X) / log (2).

 -- Function: double logb (double X)
 -- Function: float logbf (float X)
 -- Function: long double logbl (long double X)
 -- Function: _FloatN logbfN (_FloatN X)
 -- Function: _FloatNx logbfNx (_FloatNx X)
     ISO, math.h logbfN, TS 18661-3:2015, math.h logbfNx, TS 18661-3:2015, math.h These functions extract the exponent of X and return it as a floating-point value.  If FLT_RADIX is two, logb is equal to floor (log2 (x)), except it's probably faster.

     If X is de-normalized, logb returns the exponent X would have if it were normalized.  If X is infinity (positive or negative), logb returns oo.  If X is zero, logb returns oo.  It does not signal.

 -- Function: int ilogb (double X)
 -- Function: int ilogbf (float X)
 -- Function: int ilogbl (long double X)
 -- Function: int ilogbfN (_FloatN X)
 -- Function: int ilogbfNx (_FloatNx X)
 -- Function: long int llogb (double X)
 -- Function: long int llogbf (float X)
 -- Function: long int llogbl (long double X)
 -- Function: long int llogbfN (_FloatN X)
 -- Function: long int llogbfNx (_FloatNx X)
     ISO, math.h ilogbfN, TS 18661-3:2015, math.h ilogbfNx, TS 18661-3:2015, math.h llogbfN, TS 18661-3:2015, math.h llogbfNx, TS 18661-3:2015, math.h These functions are equivalent to the corresponding logb functions except that they return signed integer values.  The ilogb, ilogbf, and ilogbl functions are from ISO C99; the llogb, llogbf, llogbl functions are from TS 18661-1:2014; the ilogbfN, ilogbfNx, llogbfN, and llogbfNx functions are from TS 18661-3:2015.

Since integers cannot represent infinity and NaN, ilogb instead returns an integer that can't be the exponent of a normal floating-point number.  math.h defines constants so you can check for this.

 -- Macro: int FP_ILOGB0
     ISO, math.h ilogb returns this value if its argument is 0.  The numeric value is either INT_MIN or -INT_MAX.

     This macro is defined in ISO C99.

 -- Macro: long int FP_LLOGB0
     ISO, math.h llogb returns this value if its argument is 0.  The numeric value is either LONG_MIN or -LONG_MAX.

     This macro is defined in TS 18661-1:2014.

 -- Macro: int FP_ILOGBNAN
     ISO, math.h ilogb returns this value if its argument is NaN.  The numeric value is either INT_MIN or INT_MAX.

     This macro is defined in ISO C99.

 -- Macro: long int FP_LLOGBNAN
     ISO, math.h llogb returns this value if its argument is NaN.  The numeric value is either LONG_MIN or LONG_MAX.

     This macro is defined in TS 18661-1:2014.

These values are system specific.  They might even be the same.  The proper way to test the result of ilogb is as follows:

     i = ilogb (f);
     if (i == FP_ILOGB0 || i == FP_ILOGBNAN)
       {
         if (isnan (f))
           {
             /* Handle NaN.  */
           }
         else if (f  == 0.0)
           {
             /* Handle 0.0.  */
           }
         else
           {
             /* Some other value with large exponent,
                perhaps +Inf.  */
           }
       }

 -- Function: double pow (double BASE, double POWER)
 -- Function: float powf (float BASE, float POWER)
 -- Function: long double powl (long double BASE, long double POWER)
 -- Function: _FloatN powfN (_FloatN BASE, _FloatN POWER)
 -- Function: _FloatNx powfNx (_FloatNx BASE, _FloatNx POWER)
     ISO, math.h powfN, TS 18661-3:2015, math.h powfNx, TS 18661-3:2015, math.h These are general exponentiation functions, returning BASE raised to POWER.

     Mathematically, pow would return a complex number when BASE is negative and POWER is not an integral value.  pow can't do that, so instead it signals a domain error. pow may also underflow or overflow the destination type.

 -- Function: double sqrt (double X)
 -- Function: float sqrtf (float X)
 -- Function: long double sqrtl (long double X)
 -- Function: _FloatN sqrtfN (_FloatN X)
 -- Function: _FloatNx sqrtfNx (_FloatNx X)
     ISO, math.h sqrtfN, TS 18661-3:2015, math.h sqrtfNx, TS 18661-3:2015, math.h These functions return the nonnegative square root of X.

     If X is negative, sqrt signals a domain error. Mathematically, it should return a complex number.

 -- Function: double cbrt (double X)
 -- Function: float cbrtf (float X)
 -- Function: long double cbrtl (long double X)
 -- Function: _FloatN cbrtfN (_FloatN X)
 -- Function: _FloatNx cbrtfNx (_FloatNx X)
     BSD, math.h cbrtfN, TS 18661-3:2015, math.h cbrtfNx, TS 18661-3:2015, math.h These functions return the cube root of X.  They cannot fail; every representable real value has a representable real cube root.

 -- Function: double hypot (double X, double Y)
 -- Function: float hypotf (float X, float Y)
 -- Function: long double hypotl (long double X, long double Y)
 -- Function: _FloatN hypotfN (_FloatN X, _FloatN Y)
 -- Function: _FloatNx hypotfNx (_FloatNx X, _FloatNx Y)
     ISO, math.h hypotfN, TS 18661-3:2015, math.h hypotfNx, TS 18661-3:2015, math.h These functions return sqrt (X*X + Y*Y).  This is the length of the hypotenuse of a right triangle with sides of length X and Y, or the distance of the point (X, Y) from the origin.  Using this function instead of the direct formula is wise, since the error is much smaller.  See also the function cabs in Absolute Value.

 -- Function: double expm1 (double X)
 -- Function: float expm1f (float X)
 -- Function: long double expm1l (long double X)
 -- Function: _FloatN expm1fN (_FloatN X)
 -- Function: _FloatNx expm1fNx (_FloatNx X)
     ISO, math.h expm1fN, TS 18661-3:2015, math.h expm1fNx, TS 18661-3:2015, math.h These functions return a value equivalent to exp (X) - 1. They are computed in a way that is accurate even if X is near zero---a case where exp (X) - 1 would be inaccurate owing to subtraction of two numbers that are nearly equal.

 -- Function: double log1p (double X)
 -- Function: float log1pf (float X)
 -- Function: long double log1pl (long double X)
 -- Function: _FloatN log1pfN (_FloatN X)
 -- Function: _FloatNx log1pfNx (_FloatNx X)
     ISO, math.h log1pfN, TS 18661-3:2015, math.h log1pfNx, TS 18661-3:2015, math.h These functions return a value equivalent to log (1 + X). They are computed in a way that is accurate even if X is near zero.

ISO C99 defines complex variants of some of the exponentiation and logarithm functions.

 -- Function: complex double cexp (complex double Z)
 -- Function: complex float cexpf (complex float Z)
 -- Function: complex long double cexpl (complex long double Z)
 -- Function: complex _FloatN cexpfN (complex _FloatN Z)
 -- Function: complex _FloatNx cexpfNx (complex _FloatNx Z)
     ISO, complex.h cexpfN, TS 18661-3:2015, complex.h cexpfNx, TS 18661-3:2015, complex.h These functions return e (the base of natural logarithms) raised to the power of Z. Mathematically, this corresponds to the value

     exp (z) = exp (creal (z)) * (cos (cimag (z)) + I * sin (cimag (z)))

 -- Function: complex double clog (complex double Z)
 -- Function: complex float clogf (complex float Z)
 -- Function: complex long double clogl (complex long double Z)
 -- Function: complex _FloatN clogfN (complex _FloatN Z)
 -- Function: complex _FloatNx clogfNx (complex _FloatNx Z)
     ISO, complex.h clogfN, TS 18661-3:2015, complex.h clogfNx, TS 18661-3:2015, complex.h These functions return the natural logarithm of Z. Mathematically, this corresponds to the value

     log (z) = log (cabs (z)) + I * carg (z)

     clog has a pole at 0, and will signal overflow if Z equals or is very close to 0.  It is well-defined for all other values of Z.

 -- Function: complex double clog10 (complex double Z)
 -- Function: complex float clog10f (complex float Z)
 -- Function: complex long double clog10l (complex long double Z)
 -- Function: complex _FloatN clog10fN (complex _FloatN Z)
 -- Function: complex _FloatNx clog10fNx (complex _FloatNx Z)
     GNU, complex.h These functions return the base 10 logarithm of the complex value Z.  Mathematically, this corresponds to the value

     log10 (z) = log10 (cabs (z)) + I * carg (z) / log (10)

     All these functions, including the _FloatN and _FloatNx variants, are GNU extensions.

 -- Function: complex double csqrt (complex double Z)
 -- Function: complex float csqrtf (complex float Z)
 -- Function: complex long double csqrtl (complex long double Z)
 -- Function: complex _FloatN csqrtfN (_FloatN Z)
 -- Function: complex _FloatNx csqrtfNx (complex _FloatNx Z)
     ISO, complex.h csqrtfN, TS 18661-3:2015, complex.h csqrtfNx, TS 18661-3:2015, complex.h These functions return the complex square root of the argument Z.  Unlike the real-valued functions, they are defined for all values of Z.

 -- Function: complex double cpow (complex double BASE, complex double POWER)
 -- Function: complex float cpowf (complex float BASE, complex float POWER)
 -- Function: complex long double cpowl (complex long double BASE, complex long double POWER)
 -- Function: complex _FloatN cpowfN (complex _FloatN BASE, complex _FloatN POWER)
 -- Function: complex _FloatNx cpowfNx (complex _FloatNx BASE, complex _FloatNx POWER)
     ISO, complex.h cpowfN, TS 18661-3:2015, complex.h cpowfNx, TS 18661-3:2015, complex.h These functions return BASE raised to the power of POWER.  This is equivalent to cexp (y * clog (x))

Hyperbolic Functions
====================

The functions in this section are related to the exponential functions; see Exponents and Logarithms.

 -- Function: double sinh (double X)
 -- Function: float sinhf (float X)
 -- Function: long double sinhl (long double X)
 -- Function: _FloatN sinhfN (_FloatN X)
 -- Function: _FloatNx sinhfNx (_FloatNx X)
     ISO, math.h sinhfN, TS 18661-3:2015, math.h sinhfNx, TS 18661-3:2015, math.h These functions return the hyperbolic sine of X, defined mathematically as (exp (X) - exp (-X)) / 2.  They may signal overflow if X is too large.

 -- Function: double cosh (double X)
 -- Function: float coshf (float X)
 -- Function: long double coshl (long double X)
 -- Function: _FloatN coshfN (_FloatN X)
 -- Function: _FloatNx coshfNx (_FloatNx X)
     ISO, math.h coshfN, TS 18661-3:2015, math.h coshfNx, TS 18661-3:2015, math.h These functions return the hyperbolic cosine of X, defined mathematically as (exp (X) + exp (-X)) / 2. They may signal overflow if X is too large.

 -- Function: double tanh (double X)
 -- Function: float tanhf (float X)
 -- Function: long double tanhl (long double X)
 -- Function: _FloatN tanhfN (_FloatN X)
 -- Function: _FloatNx tanhfNx (_FloatNx X)
     ISO, math.h tanhfN, TS 18661-3:2015, math.h tanhfNx, TS 18661-3:2015, math.h These functions return the hyperbolic tangent of X, defined mathematically as sinh (X) / cosh (X). They may signal overflow if X is too large.

There are counterparts for the hyperbolic functions which take complex arguments.

 -- Function: complex double csinh (complex double Z)
 -- Function: complex float csinhf (complex float Z)
 -- Function: complex long double csinhl (complex long double Z)
 -- Function: complex _FloatN csinhfN (complex _FloatN Z)
 -- Function: complex _FloatNx csinhfNx (complex _FloatNx Z)
     ISO, complex.h csinhfN, TS 18661-3:2015, complex.h csinhfNx, TS 18661-3:2015, complex.h These functions return the complex hyperbolic sine of Z, defined mathematically as (exp (Z) - exp (-Z)) / 2.

 -- Function: complex double ccosh (complex double Z)
 -- Function: complex float ccoshf (complex float Z)
 -- Function: complex long double ccoshl (complex long double Z)
 -- Function: complex _FloatN ccoshfN (complex _FloatN Z)
 -- Function: complex _FloatNx ccoshfNx (complex _FloatNx Z)
     ISO, complex.h ccoshfN, TS 18661-3:2015, complex.h ccoshfNx, TS 18661-3:2015, complex.h These functions return the complex hyperbolic cosine of Z, defined mathematically as (exp (Z) + exp (-Z)) / 2.

 -- Function: complex double ctanh (complex double Z)
 -- Function: complex float ctanhf (complex float Z)
 -- Function: complex long double ctanhl (complex long double Z)
 -- Function: complex _FloatN ctanhfN (complex _FloatN Z)
 -- Function: complex _FloatNx ctanhfNx (complex _FloatNx Z)
     ISO, complex.h ctanhfN, TS 18661-3:2015, complex.h ctanhfNx, TS 18661-3:2015, complex.h These functions return the complex hyperbolic tangent of Z, defined mathematically as csinh (Z) / ccosh (Z).

 -- Function: double asinh (double X)
 -- Function: float asinhf (float X)
 -- Function: long double asinhl (long double X)
 -- Function: _FloatN asinhfN (_FloatN X)
 -- Function: _FloatNx asinhfNx (_FloatNx X)
     ISO, math.h asinhfN, TS 18661-3:2015, math.h asinhfNx, TS 18661-3:2015, math.h These functions return the inverse hyperbolic sine of X---the value whose hyperbolic sine is X.

 -- Function: double acosh (double X)
 -- Function: float acoshf (float X)
 -- Function: long double acoshl (long double X)
 -- Function: _FloatN acoshfN (_FloatN X)
 -- Function: _FloatNx acoshfNx (_FloatNx X)
     ISO, math.h acoshfN, TS 18661-3:2015, math.h acoshfNx, TS 18661-3:2015, math.h These functions return the inverse hyperbolic cosine of X---the value whose hyperbolic cosine is X.  If X is less than 1, acosh signals a domain error.

 -- Function: double atanh (double X)
 -- Function: float atanhf (float X)
 -- Function: long double atanhl (long double X)
 -- Function: _FloatN atanhfN (_FloatN X)
 -- Function: _FloatNx atanhfNx (_FloatNx X)
     ISO, math.h atanhfN, TS 18661-3:2015, math.h atanhfNx, TS 18661-3:2015, math.h These functions return the inverse hyperbolic tangent of X---the value whose hyperbolic tangent is X.  If the absolute value of X is greater than 1, atanh signals a domain error; if it is equal to 1, atanh returns infinity.

 -- Function: complex double casinh (complex double Z)
 -- Function: complex float casinhf (complex float Z)
 -- Function: complex long double casinhl (complex long double Z)
 -- Function: complex _FloatN casinhfN (complex _FloatN Z)
 -- Function: complex _FloatNx casinhfNx (complex _FloatNx Z)
     ISO, complex.h casinhfN, TS 18661-3:2015, complex.h casinhfNx, TS 18661-3:2015, complex.h These functions return the inverse complex hyperbolic sine of Z---the value whose complex hyperbolic sine is Z.

 -- Function: complex double cacosh (complex double Z)
 -- Function: complex float cacoshf (complex float Z)
 -- Function: complex long double cacoshl (complex long double Z)
 -- Function: complex _FloatN cacoshfN (complex _FloatN Z)
 -- Function: complex _FloatNx cacoshfNx (complex _FloatNx Z)
     ISO, complex.h cacoshfN, TS 18661-3:2015, complex.h cacoshfNx, TS 18661-3:2015, complex.h These functions return the inverse complex hyperbolic cosine of Z---the value whose complex hyperbolic cosine is Z.  Unlike the real-valued functions, there are no restrictions on the value of Z.

 -- Function: complex double catanh (complex double Z)
 -- Function: complex float catanhf (complex float Z)
 -- Function: complex long double catanhl (complex long double Z)
 -- Function: complex _FloatN catanhfN (complex _FloatN Z)
 -- Function: complex _FloatNx catanhfNx (complex _FloatNx Z)
     ISO, complex.h catanhfN, TS 18661-3:2015, complex.h catanhfNx, TS 18661-3:2015, complex.h These functions return the inverse complex hyperbolic tangent of Z---the value whose complex hyperbolic tangent is Z.  Unlike the real-valued functions, there are no restrictions on the value of Z.

Special Functions
=================

These are some more exotic mathematical functions which are sometimes useful.  Currently they only have real-valued versions.

 -- Function: double erf (double X)
 -- Function: float erff (float X)
 -- Function: long double erfl (long double X)
 -- Function: _FloatN erffN (_FloatN X)
 -- Function: _FloatNx erffNx (_FloatNx X)
     SVID, math.h erffN, TS 18661-3:2015, math.h erffNx, TS 18661-3:2015, math.h erf returns the error function of X.  The error function is defined as

          erf (x) = 2/sqrt(pi) * integral from 0 to x of exp(-t^2) dt

 -- Function: double erfc (double X)
 -- Function: float erfcf (float X)
 -- Function: long double erfcl (long double X)
 -- Function: _FloatN erfcfN (_FloatN X)
 -- Function: _FloatNx erfcfNx (_FloatNx X)
     SVID, math.h erfcfN, TS 18661-3:2015, math.h erfcfNx, TS 18661-3:2015, math.h erfc returns 1.0 - erf(X), but computed in a fashion that avoids round-off error when X is large.

 -- Function: double lgamma (double X)
 -- Function: float lgammaf (float X)
 -- Function: long double lgammal (long double X)
 -- Function: _FloatN lgammafN (_FloatN X)
 -- Function: _FloatNx lgammafNx (_FloatNx X)
     SVID, math.h lgammafN, TS 18661-3:2015, math.h lgammafNx, TS 18661-3:2015, math.h prelim:signgam asunsafe acsafe lgamma returns the natural logarithm of the absolute value of the gamma function of X.  The gamma function is defined as

          gamma (x) = integral from 0 to oo of t^(x-1) e^-t dt

     The sign of the gamma function is stored in the global variable SIGNGAM, which is declared in math.h.  It is 1 if the intermediate result was positive or zero, or -1 if it was negative.

     To compute the real gamma function you can use the tgamma function or you can compute the values as follows:

          lgam = lgamma(x);
          gam  = signgam*exp(lgam);

     The gamma function has singularities at the non-positive integers. lgamma will raise the zero divide exception if evaluated at a singularity.

 -- Function: double lgamma_r (double X, int *SIGNP)
 -- Function: float lgammaf_r (float X, int *SIGNP)
 -- Function: long double lgammal_r (long double X, int *SIGNP)
 -- Function: _FloatN lgammafN_r (_FloatN X, int *SIGNP)
 -- Function: _FloatNx lgammafNx_r (_FloatNx X, int *SIGNP)
     XPG, math.h lgammafN_r, GNU, math.h lgammafNx_r, GNU, math.h lgamma_r is just like lgamma, but it stores the sign of the intermediate result in the variable pointed to by SIGNP instead of in the SIGNGAM global.  This means it is reentrant.

     The lgammafN_r and lgammafNx_r functions are GNU extensions.

 -- Function: double gamma (double X)
 -- Function: float gammaf (float X)
 -- Function: long double gammal (long double X)
     SVID, math.h prelim:signgam asunsafe acsafe These functions exist for compatibility reasons.  They are equivalent to lgamma etc.  It is better to use lgamma since for one the name reflects better the actual computation, and moreover lgamma is standardized in ISO C99 while gamma is not.

 -- Function: double tgamma (double X)
 -- Function: float tgammaf (float X)
 -- Function: long double tgammal (long double X)
 -- Function: _FloatN tgammafN (_FloatN X)
 -- Function: _FloatNx tgammafNx (_FloatNx X)
     tgamma, XPG, math.h tgamma, ISO, math.h tgammaf, XPG, math.h tgammaf, ISO, math.h tgammal, XPG, math.h tgammal, ISO, math.h tgammafN, TS 18661-3:2015, math.h tgammafNx, TS 18661-3:2015, math.h tgamma applies the gamma function to X.  The gamma function is defined as

          gamma (x) = integral from 0 to oo of t^(x-1) e^-t dt

     This function was introduced in ISO C99.  The _FloatN and _FloatNx variants were introduced in ISO/IEC TS 18661-3.

 -- Function: double j0 (double X)
 -- Function: float j0f (float X)
 -- Function: long double j0l (long double X)
 -- Function: _FloatN j0fN (_FloatN X)
 -- Function: _FloatNx j0fNx (_FloatNx X)
     SVID, math.h j0fN, GNU, math.h j0fNx, GNU, math.h j0 returns the Bessel function of the first kind of order 0 of X.  It may signal underflow if X is too large.

     The _FloatN and _FloatNx variants are GNU extensions.

 -- Function: double j1 (double X)
 -- Function: float j1f (float X)
 -- Function: long double j1l (long double X)
 -- Function: _FloatN j1fN (_FloatN X)
 -- Function: _FloatNx j1fNx (_FloatNx X)
     SVID, math.h j1fN, GNU, math.h j1fNx, GNU, math.h j1 returns the Bessel function of the first kind of order 1 of X.  It may signal underflow if X is too large.

     The _FloatN and _FloatNx variants are GNU extensions.

 -- Function: double jn (int N, double X)
 -- Function: float jnf (int N, float X)
 -- Function: long double jnl (int N, long double X)
 -- Function: _FloatN jnfN (int N, _FloatN X)
 -- Function: _FloatNx jnfNx (int N, _FloatNx X)
     SVID, math.h jnfN, GNU, math.h jnfNx, GNU, math.h jn returns the Bessel function of the first kind of order N of X.  It may signal underflow if X is too large.

     The _FloatN and _FloatNx variants are GNU extensions.

 -- Function: double y0 (double X)
 -- Function: float y0f (float X)
 -- Function: long double y0l (long double X)
 -- Function: _FloatN y0fN (_FloatN X)
 -- Function: _FloatNx y0fNx (_FloatNx X)
     SVID, math.h y0fN, GNU, math.h y0fNx, GNU, math.h y0 returns the Bessel function of the second kind of order 0 of X.  It may signal underflow if X is too large.  If X is negative, y0 signals a domain error; if it is zero, y0 signals overflow and returns -oo.

     The _FloatN and _FloatNx variants are GNU extensions.

 -- Function: double y1 (double X)
 -- Function: float y1f (float X)
 -- Function: long double y1l (long double X)
 -- Function: _FloatN y1fN (_FloatN X)
 -- Function: _FloatNx y1fNx (_FloatNx X)
     SVID, math.h y1fN, GNU, math.h y1fNx, GNU, math.h y1 returns the Bessel function of the second kind of order 1 of X.  It may signal underflow if X is too large.  If X is negative, y1 signals a domain error; if it is zero, y1 signals overflow and returns -oo.

     The _FloatN and _FloatNx variants are GNU extensions.

 -- Function: double yn (int N, double X)
 -- Function: float ynf (int N, float X)
 -- Function: long double ynl (int N, long double X)
 -- Function: _FloatN ynfN (int N, _FloatN X)
 -- Function: _FloatNx ynfNx (int N, _FloatNx X)
     SVID, math.h ynfN, GNU, math.h ynfNx, GNU, math.h yn returns the Bessel function of the second kind of order N of X.  It may signal underflow if X is too large.  If X is negative, yn signals a domain error; if it is zero, yn signals overflow and returns -oo.

     The _FloatN and _FloatNx variants are GNU extensions.

Known Maximum Errors in Math Functions
======================================

This section lists the known errors of the functions in the math library.  Errors are measured in ``units of the last place''.  This is a measure for the relative error.  For a number z with the representation d.d...d*2^e (we assume IEEE floating-point numbers with base 2) the ULP is represented by

     |d.d...d - (z / 2^e)| / 2^(p - 1)

where p is the number of bits in the mantissa of the floating-point number representation.  Ideally the error for all functions is always less than 0.5ulps in round-to-nearest mode.  Using rounding bits this is also possible and normally implemented for the basic operations.  Except for certain functions such as sqrt, fma and rint whose results are fully specified by reference to corresponding IEEE 754 floating-point operations, and conversions between strings and floating point, theglibc does not aim for correctly rounded results for functions in the math library, and does not aim for correctness in whether ``inexact'' exceptions are raised.  Instead, the goals for accuracy of functions without fully specified results are as follows; some functions have bugs meaning they do not meet these goals in all cases.  In the future, theglibc may provide some other correctly rounding functions under the names such as crsin proposed for an extension to ISO C.

  * Each function with a floating-point result behaves as if it computes an infinite-precision result that is within a few ulp (in both real and complex parts, for functions with complex results) of the mathematically correct value of the function (interpreted together with ISO C or POSIX semantics for the function in question) at the exact value passed as the input.  Exceptions are raised appropriately for this value and in accordance with IEEE 754 / ISO C / POSIX semantics, and it is then rounded according to the current rounding direction to the result that is returned to the user.  errno may also be set (see Math Error Reporting).  (The ``inexact'' exception may be raised, or not raised, even if this is inconsistent with the infinite-precision value.)

  * For the IBM long double format, as used on PowerPC GNU/Linux, the accuracy goal is weaker for input values not exactly representable in 106 bits of precision; it is as if the input value is some value within 0.5ulp of the value actually passed, where ``ulp'' is interpreted in terms of a fixed-precision 106-bit mantissa, but not necessarily the exact value actually passed with discontiguous mantissa bits.

  * For the IBM long double format, functions whose results are fully specified by reference to corresponding IEEE 754 floating-point operations have the same accuracy goals as other functions, but with the error bound being the same as that for division (3ulp). Furthermore, ``inexact'' and ``underflow'' exceptions may be raised for all functions for any inputs, even where such exceptions are inconsistent with the returned value, since the underlying floating-point arithmetic has that property.

  * Functions behave as if the infinite-precision result computed is zero, infinity or NaN if and only if that is the mathematically correct infinite-precision result.  They behave as if the infinite-precision result computed always has the same sign as the mathematically correct result.

  * If the mathematical result is more than a few ulp above the overflow threshold for the current rounding direction, the value returned is the appropriate overflow value for the current rounding direction, with the overflow exception raised.

  * If the mathematical result has magnitude well below half the least subnormal magnitude, the returned value is either zero or the least subnormal (in each case, with the correct sign), according to the current rounding direction and with the underflow exception raised.

  * Where the mathematical result underflows (before rounding) and is not exactly representable as a floating-point value, the function does not behave as if the computed infinite-precision result is an exact value in the subnormal range.  This means that the underflow exception is raised other than possibly for cases where the mathematical result is very close to the underflow threshold and the function behaves as if it computes an infinite-precision result that does not underflow.  (So there may be spurious underflow exceptions in cases where the underflowing result is exact, but not missing underflow exceptions in cases where it is inexact.)

  * Theglibc does not aim for functions to satisfy other properties of the underlying mathematical function, such as monotonicity, where not implied by the above goals.

  * All the above applies to both real and complex parts, for complex functions.

Therefore many of the functions in the math library have errors.  The table lists the maximum error for each function which is exposed by one of the existing tests in the test suite.  The table tries to cover as much as possible and list the actual maximum error (or at least a ballpark figure) but this is often not achieved due to the large search space.

The table lists the ULP values for different architectures.  Different architectures have different results since their hardware support for floating-point operations varies and also the existing hardware support is different.  Only the round-to-nearest rounding mode is covered by this table.  Functions not listed do not have known errors.  Vector versions of functions in the x86_64 libmvec library have a maximum error of 4 ulps.

     [file libm-err.texi not included]

Pseudo-Random Numbers
=====================

This section describes the GNU facilities for generating a series of pseudo-random numbers.  The numbers generated are not truly random; typically, they form a sequence that repeats periodically, with a period so large that you can ignore it for ordinary purposes.  The random number generator works by remembering a seed value which it uses to compute the next random number and also to compute a new seed.

Although the generated numbers look unpredictable within one run of a program, the sequence of numbers is exactly the same from one run to the next.  This is because the initial seed is always the same.  This is convenient when you are debugging a program, but it is unhelpful if you want the program to behave unpredictably.  If you want a different pseudo-random series each time your program runs, you must specify a different seed each time.  For ordinary purposes, basing the seed on the current time works well.  For random numbers in cryptography, see Unpredictable Bytes.

You can obtain repeatable sequences of numbers on a particular machine type by specifying the same initial seed value for the random number generator.  There is no standard meaning for a particular seed value; the same seed, used in different C libraries or on different CPU types, will give you different random numbers.

Theglibc supports the standard ISO C random number functions plus two other sets derived from BSD and SVID.  The BSD and ISO C functions provide identical, somewhat limited functionality.  If only a small number of random bits are required, we recommend you use the ISO C interface, rand and srand.  The SVID functions provide a more flexible interface, which allows better random number generator algorithms, provides more random bits (up to 48) per call, and can provide random floating-point numbers.  These functions are required by the XPG standard and therefore will be present in all modern Unix systems.

ISO C Random Number Functions
-----------------------------

This section describes the random number functions that are part of the ISO C standard.

To use these facilities, you should include the header file stdlib.h in your program.

 -- Macro: int RAND_MAX
     ISO, stdlib.h The value of this macro is an integer constant representing the largest value the rand function can return.  In theglibc, it is 2147483647, which is the largest signed integer representable in 32 bits.  In other libraries, it may be as low as 32767.

 -- Function: int rand (void)
     ISO, stdlib.h The rand function returns the next pseudo-random number in the series.  The value ranges from 0 to RAND_MAX.

 -- Function: void srand (unsigned int SEED)
     ISO, stdlib.h This function establishes SEED as the seed for a new series of pseudo-random numbers.  If you call rand before a seed has been established with srand, it uses the value 1 as a default seed.

     To produce a different pseudo-random series each time your program is run, do srand (time (0)).

POSIX.1 extended the C standard functions to support reproducible random numbers in multi-threaded programs.  However, the extension is badly designed and unsuitable for serious work.

 -- Function: int rand_r (unsigned int *SEED)
     POSIX.1, stdlib.h This function returns a random number in the range 0 to RAND_MAX just as rand does.  However, all its state is stored in the SEED argument.  This means the RNG's state can only have as many bits as the type unsigned int has.  This is far too few to provide a good RNG.

     If your program requires a reentrant RNG, we recommend you use the reentrant GNU extensions to the SVID random number generator.  The POSIX.1 interface should only be used when the GNU extensions are not available.

BSD Random Number Functions
---------------------------

This section describes a set of random number generation functions that are derived from BSD.  There is no advantage to using these functions with theglibc; we support them for BSD compatibility only.

The prototypes for these functions are in stdlib.h.

 -- Function: long int random (void)
     BSD, stdlib.h This function returns the next pseudo-random number in the sequence. The value returned ranges from 0 to 2147483647.

     NB: Temporarily this function was defined to return a int32_t value to indicate that the return value always contains 32 bits even if long int is wider.  The standard demands it differently.  Users must always be aware of the 32-bit limitation, though.

 -- Function: void srandom (unsigned int SEED)
     BSD, stdlib.h The srandom function sets the state of the random number generator based on the integer SEED.  If you supply a SEED value of 1, this will cause random to reproduce the default set of random numbers.

     To produce a different set of pseudo-random numbers each time your program runs, do srandom (time (0)).

 -- Function: char * initstate (unsigned int SEED, char *STATE, size_t SIZE)
     BSD, stdlib.h The initstate function is used to initialize the random number generator state.  The argument STATE is an array of SIZE bytes, used to hold the state information.  It is initialized based on SEED.  The size must be between 8 and 256 bytes, and should be a power of two.  The bigger the STATE array, the better.

     The return value is the previous value of the state information array. You can use this value later as an argument to setstate to restore that state.

 -- Function: char * setstate (char *STATE)
     BSD, stdlib.h The setstate function restores the random number state information STATE.  The argument must have been the result of a previous call to INITSTATE or SETSTATE.

     The return value is the previous value of the state information array. You can use this value later as an argument to setstate to restore that state.

     If the function fails the return value is NULL.

The four functions described so far in this section all work on a state which is shared by all threads.  The state is not directly accessible to the user and can only be modified by these functions.  This makes it hard to deal with situations where each thread should have its own pseudo-random number generator.

Theglibc contains four additional functions which contain the state as an explicit parameter and therefore make it possible to handle thread-local PRNGs.  Besides this there is no difference.  In fact, the four functions already discussed are implemented internally using the following interfaces.

The stdlib.h header contains a definition of the following type:

 -- Data Type: struct random_data
     GNU, stdlib.h

     Objects of type struct random_data contain the information necessary to represent the state of the PRNG.  Although a complete definition of the type is present the type should be treated as opaque.

The functions modifying the state follow exactly the already described functions.

 -- Function: int random_r (struct random_data *restrict BUF, int32_t *restrict RESULT)
     GNU, stdlib.h prelim:buf assafe The random_r function behaves exactly like the random function except that it uses and modifies the state in the object pointed to by the first parameter instead of the global state.

 -- Function: int srandom_r (unsigned int SEED, struct random_data *BUF)
     GNU, stdlib.h prelim:buf assafe The srandom_r function behaves exactly like the srandom function except that it uses and modifies the state in the object pointed to by the second parameter instead of the global state.

 -- Function: int initstate_r (unsigned int SEED, char *restrict STATEBUF, size_t STATELEN, struct random_data *restrict BUF)
     GNU, stdlib.h prelim:buf assafe The initstate_r function behaves exactly like the initstate function except that it uses and modifies the state in the object pointed to by the fourth parameter instead of the global state.

 -- Function: int setstate_r (char *restrict STATEBUF, struct random_data *restrict BUF)
     GNU, stdlib.h prelim:buf assafe The setstate_r function behaves exactly like the setstate function except that it uses and modifies the state in the object pointed to by the first parameter instead of the global state.

SVID Random Number Function
---------------------------

The C library on SVID systems contains yet another kind of random number generator functions.  They use a state of 48 bits of data.  The user can choose among a collection of functions which return the random bits in different forms.

Generally there are two kinds of function.  The first uses a state of the random number generator which is shared among several functions and by all threads of the process.  The second requires the user to handle the state.

All functions have in common that they use the same congruential formula with the same constants.  The formula is

     Y = (a * X + c) mod m

where X is the state of the generator at the beginning and Y the state at the end.  a and c are constants determining the way the generator works.  By default they are

     a = 0x5DEECE66D = 25214903917
     c = 0xb = 11

but they can also be changed by the user.  m is of course 2^48 since the state consists of a 48-bit array.

The prototypes for these functions are in stdlib.h.

 -- Function: double drand48 (void)
     SVID, stdlib.h prelim:drand48 asunsafe This function returns a double value in the range of 0.0 to 1.0 (exclusive).  The random bits are determined by the global state of the random number generator in the C library.

     Since the double type according to IEEE 754 has a 52-bit mantissa this means 4 bits are not initialized by the random number generator.  These are (of course) chosen to be the least significant bits and they are initialized to 0.

 -- Function: double erand48 (unsigned short int XSUBI[3])
     SVID, stdlib.h prelim:drand48 asunsafe This function returns a double value in the range of 0.0 to 1.0 (exclusive), similarly to drand48.  The argument is an array describing the state of the random number generator.

     This function can be called subsequently since it updates the array to guarantee random numbers.  The array should have been initialized before initial use to obtain reproducible results.

 -- Function: long int lrand48 (void)
     SVID, stdlib.h prelim:drand48 asunsafe The lrand48 function returns an integer value in the range of 0 to 2^31 (exclusive).  Even if the size of the long int type can take more than 32 bits, no higher numbers are returned. The random bits are determined by the global state of the random number generator in the C library.

 -- Function: long int nrand48 (unsigned short int XSUBI[3])
     SVID, stdlib.h prelim:drand48 asunsafe This function is similar to the lrand48 function in that it returns a number in the range of 0 to 2^31 (exclusive) but the state of the random number generator used to produce the random bits is determined by the array provided as the parameter to the function.

     The numbers in the array are updated afterwards so that subsequent calls to this function yield different results (as is expected of a random number generator).  The array should have been initialized before the first call to obtain reproducible results.

 -- Function: long int mrand48 (void)
     SVID, stdlib.h prelim:drand48 asunsafe The mrand48 function is similar to lrand48.  The only difference is that the numbers returned are in the range -2^31 to 2^31 (exclusive).

 -- Function: long int jrand48 (unsigned short int XSUBI[3])
     SVID, stdlib.h prelim:drand48 asunsafe The jrand48 function is similar to nrand48.  The only difference is that the numbers returned are in the range -2^31 to 2^31 (exclusive).  For the xsubi parameter the same requirements are necessary.

The internal state of the random number generator can be initialized in several ways.  The methods differ in the completeness of the information provided.

 -- Function: void srand48 (long int SEEDVAL)
     SVID, stdlib.h prelim:drand48 asunsafe The srand48 function sets the most significant 32 bits of the internal state of the random number generator to the least significant 32 bits of the SEEDVAL parameter.  The lower 16 bits are initialized to the value 0x330E.  Even if the long int type contains more than 32 bits only the lower 32 bits are used.

     Owing to this limitation, initialization of the state of this function is not very useful.  But it makes it easy to use a construct like srand48 (time (0)).

     A side-effect of this function is that the values a and c from the internal state, which are used in the congruential formula, are reset to the default values given above.  This is of importance once the user has called the lcong48 function (see below).

 -- Function: unsigned short int * seed48 (unsigned short int SEED16V[3])
     SVID, stdlib.h prelim:drand48 asunsafe The seed48 function initializes all 48 bits of the state of the internal random number generator from the contents of the parameter SEED16V.  Here the lower 16 bits of the first element of SEED16V initialize the least significant 16 bits of the internal state, the lower 16 bits of SEED16V[1] initialize the mid-order 16 bits of the state and the 16 lower bits of SEED16V[2] initialize the most significant 16 bits of the state.

     Unlike srand48 this function lets the user initialize all 48 bits of the state.

     The value returned by seed48 is a pointer to an array containing the values of the internal state before the change.  This might be useful to restart the random number generator at a certain state. Otherwise the value can simply be ignored.

     As for srand48, the values a and c from the congruential formula are reset to the default values.

There is one more function to initialize the random number generator which enables you to specify even more information by allowing you to change the parameters in the congruential formula.

 -- Function: void lcong48 (unsigned short int PARAM[7])
     SVID, stdlib.h prelim:drand48 asunsafe The lcong48 function allows the user to change the complete state of the random number generator.  Unlike srand48 and seed48, this function also changes the constants in the congruential formula.

     From the seven elements in the array PARAM the least significant 16 bits of the entries PARAM[0] to PARAM[2] determine the initial state, the least significant 16 bits of PARAM[3] to PARAM[5] determine the 48 bit constant a and PARAM[6] determines the 16-bit value c.

All the above functions have in common that they use the global parameters for the congruential formula.  In multi-threaded programs it might sometimes be useful to have different parameters in different threads.  For this reason all the above functions have a counterpart which works on a description of the random number generator in the user-supplied buffer instead of the global state.

Please note that it is no problem if several threads use the global state if all threads use the functions which take a pointer to an array containing the state.  The random numbers are computed following the same loop but if the state in the array is different all threads will obtain an individual random number generator.

The user-supplied buffer must be of type struct drand48_data. This type should be regarded as opaque and not manipulated directly.

 -- Function: int drand48_r (struct drand48_data *BUFFER, double *RESULT)
     GNU, stdlib.h prelim:buffer assafe This function is equivalent to the drand48 function with the difference that it does not modify the global random number generator parameters but instead the parameters in the buffer supplied through the pointer BUFFER.  The random number is returned in the variable pointed to by RESULT.

     The return value of the function indicates whether the call succeeded. If the value is less than 0 an error occurred and errno is set to indicate the problem.

     This function is a GNU extension and should not be used in portable programs.

 -- Function: int erand48_r (unsigned short int XSUBI[3], struct drand48_data *BUFFER, double *RESULT)
     GNU, stdlib.h prelim:buffer assafe The erand48_r function works like erand48, but in addition it takes an argument BUFFER which describes the random number generator.  The state of the random number generator is taken from the xsubi array, the parameters for the congruential formula from the global random number generator data.  The random number is returned in the variable pointed to by RESULT.

     The return value is non-negative if the call succeeded.

     This function is a GNU extension and should not be used in portable programs.

 -- Function: int lrand48_r (struct drand48_data *BUFFER, long int *RESULT)
     GNU, stdlib.h prelim:buffer assafe This function is similar to lrand48, but in addition it takes a pointer to a buffer describing the state of the random number generator just like drand48.

     If the return value of the function is non-negative the variable pointed to by RESULT contains the result.  Otherwise an error occurred.

     This function is a GNU extension and should not be used in portable programs.

 -- Function: int nrand48_r (unsigned short int XSUBI[3], struct drand48_data *BUFFER, long int *RESULT)
     GNU, stdlib.h prelim:buffer assafe The nrand48_r function works like nrand48 in that it produces a random number in the range 0 to 2^31.  But instead of using the global parameters for the congruential formula it uses the information from the buffer pointed to by BUFFER.  The state is described by the values in XSUBI.

     If the return value is non-negative the variable pointed to by RESULT contains the result.

     This function is a GNU extension and should not be used in portable programs.

 -- Function: int mrand48_r (struct drand48_data *BUFFER, long int *RESULT)
     GNU, stdlib.h prelim:buffer assafe This function is similar to mrand48 but like the other reentrant functions it uses the random number generator described by the value in the buffer pointed to by BUFFER.

     If the return value is non-negative the variable pointed to by RESULT contains the result.

     This function is a GNU extension and should not be used in portable programs.

 -- Function: int jrand48_r (unsigned short int XSUBI[3], struct drand48_data *BUFFER, long int *RESULT)
     GNU, stdlib.h prelim:buffer assafe The jrand48_r function is similar to jrand48.  Like the other reentrant functions of this function family it uses the congruential formula parameters from the buffer pointed to by BUFFER.

     If the return value is non-negative the variable pointed to by RESULT contains the result.

     This function is a GNU extension and should not be used in portable programs.

Before any of the above functions are used the buffer of type struct drand48_data should be initialized.  The easiest way to do this is to fill the whole buffer with null bytes, e.g. by

     memset (buffer, '\0', sizeof (struct drand48_data));

Using any of the reentrant functions of this family now will automatically initialize the random number generator to the default values for the state and the parameters of the congruential formula.

The other possibility is to use any of the functions which explicitly initialize the buffer.  Though it might be obvious how to initialize the buffer from looking at the parameter to the function, it is highly recommended to use these functions since the result might not always be what you expect.

 -- Function: int srand48_r (long int SEEDVAL, struct drand48_data *BUFFER)
     GNU, stdlib.h prelim:buffer assafe The description of the random number generator represented by the information in BUFFER is initialized similarly to what the function srand48 does.  The state is initialized from the parameter SEEDVAL and the parameters for the congruential formula are initialized to their default values.

     If the return value is non-negative the function call succeeded.

     This function is a GNU extension and should not be used in portable programs.

 -- Function: int seed48_r (unsigned short int SEED16V[3], struct drand48_data *BUFFER)
     GNU, stdlib.h prelim:buffer assafe This function is similar to srand48_r but like seed48 it initializes all 48 bits of the state from the parameter SEED16V.

     If the return value is non-negative the function call succeeded.  It does not return a pointer to the previous state of the random number generator like the seed48 function does.  If the user wants to preserve the state for a later re-run s/he can copy the whole buffer pointed to by BUFFER.

     This function is a GNU extension and should not be used in portable programs.

 -- Function: int lcong48_r (unsigned short int PARAM[7], struct drand48_data *BUFFER)
     GNU, stdlib.h prelim:buffer assafe This function initializes all aspects of the random number generator described in BUFFER with the data in PARAM.  Here it is especially true that the function does more than just copying the contents of PARAM and BUFFER.  More work is required and therefore it is important to use this function rather than initializing the random number generator directly.

     If the return value is non-negative the function call succeeded.

     This function is a GNU extension and should not be used in portable programs.

Is Fast Code or Small Code preferred?
=====================================

If an application uses many floating point functions it is often the case that the cost of the function calls themselves is not negligible. Modern processors can often execute the operations themselves very fast, but the function call disrupts the instruction pipeline.

For this reason theglibc provides optimizations for many of the frequently-used math functions.  When GNU CC is used and the user activates the optimizer, several new inline functions and macros are defined.  These new functions and macros have the same names as the library functions and so are used instead of the latter.  In the case of inline functions the compiler will decide whether it is reasonable to use them, and this decision is usually correct.

This means that no calls to the library functions may be necessary, and can increase the speed of generated code significantly.  The drawback is that code size will increase, and the increase is not always negligible.

There are two kinds of inline functions: those that give the same result as the library functions and others that might not set errno and might have a reduced precision and/or argument range in comparison with the library functions.  The latter inline functions are only available if the flag -ffast-math is given to GNU CC.

Not all hardware implements the entire IEEE 754 standard, and even if it does there may be a substantial performance penalty for using some of its features.  For example, enabling traps on some processors forces the FPU to run un-pipelined, which can more than double calculation time.