[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
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 complexvalued functions are defined in `complex.h'.
All mathematical functions which take a floatingpoint 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
.
19.1 Predefined Mathematical Constants  Precise numeric values for oftenused constants.  
19.2 Trigonometric Functions  Sine, cosine, tangent, and friends.  
19.3 Inverse Trigonometric Functions  Arcsine, arccosine, etc.  
19.4 Exponentiation and Logarithms  Also pow and sqrt.  
19.5 Hyperbolic Functions  sinh, cosh, tanh, etc.  
19.6 Special Functions  Bessel, gamma, erf.  
19.7 Known Maximum Errors in Math Functions  
19.8 PseudoRandom Numbers  Functions for generating pseudorandom numbers.  
19.9 Is Fast Code or Small Code preferred?  Fast code or small code. 
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
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 _BSD_SOURCE
or
_XOPEN_SOURCE=500
, or a more general feature select macro, is
defined. The default set of features includes these constants.
See section Feature Test Macros.
All values are of type double
. As an extension, the GNU C
library also defines these constants with type long double
. The
long double
macros have a lowercase `l' appended to their
names: M_El
, M_PIl
, and so forth. These are only
available if _GNU_SOURCE
is defined.
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 the GNU C library
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.
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
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)
.
These functions return the sine of x, where x is given in
radians. The return value is in the range 1
to 1
.
These functions return the cosine of x, where x is given in
radians. The return value is in the range 1
to 1
.
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.
These functions return the sine of x in *sinx
and the
cosine of x in *cos
, where x is given in
radians. Both values, *sinx
and *cosx
, are in
the range of 1
to 1
.
This function is a GNU extension. Portable programs should be prepared to cope with its absence.
ISO C99 defines variants of the trig functions which work on complex numbers. The GNU C library 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.)
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)).
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))
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.
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
These are the usual arc sine, arc cosine and arc tangent functions, which are the inverses of the sine, cosine and tangent functions respectively.
These functions compute the arc sine of xthat 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 arc sine function is defined mathematically only
over the domain 1
to 1
. If x is outside the
domain, asin
signals a domain error.
These functions compute the arc cosine of xthat 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 arc cosine function is defined mathematically only
over the domain 1
to 1
. If x is outside the
domain, acos
signals a domain error.
These functions compute the arc tangent of xthat 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).
This function computes the arc tangent 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 xaxis. Thus, atan2
is useful for
converting Cartesian coordinates to polar coordinates. (To compute the
radial coordinate, use hypot
; see Exponentiation and Logarithms.)
If both x and y are zero, atan2
returns zero.
ISO C99 defines complex versions of the inverse trig functions.
These functions compute the complex arc sine of zthat is, the value whose sine is z. The value returned is in radians.
Unlike the realvalued functions, casin
is defined for all
values of z.
These functions compute the complex arc cosine of zthat is, the value whose cosine is z. The value returned is in radians.
Unlike the realvalued functions, cacos
is defined for all
values of z.
These functions compute the complex arc tangent of zthat is, the value whose tangent is z. The value is in units of radians.
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
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.
These functions compute 2
raised to the power x.
Mathematically, exp2 (x)
is the same as exp (x * log (2))
.
These functions compute 10
raised to the power x.
Mathematically, exp10 (x)
is the same as exp (x * log (10))
.
These functions are GNU extensions. The name exp10
is
preferred, since it is analogous to exp
and exp2
.
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.
These functions return the base10 logarithm of x.
log10 (x)
equals log (x) / log (10)
.
These functions return the base2 logarithm of x.
log2 (x)
equals log (x) / log (2)
.
These functions extract the exponent of x and return it as a
floatingpoint value. If FLT_RADIX
is two, logb
is equal
to floor (log2 (x))
, except it's probably faster.
If x is denormalized, logb
returns the exponent x
would have if it were normalized. If x is infinity (positive or
negative), logb
returns ∞. If x is zero,
logb
returns ∞. It does not signal.
These functions are equivalent to the corresponding logb
functions except that they return signed integer values.
Since integers cannot represent infinity and NaN, ilogb
instead
returns an integer that can't be the exponent of a normal floatingpoint
number. `math.h' defines constants so you can check for this.
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.
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.
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. */ } } 
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.
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.
These functions return the cube root of x. They cannot fail; every representable real value has a representable real cube root.
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.
These functions return a value equivalent to exp (x)  1
.
They are computed in a way that is accurate even if x is
near zeroa case where exp (x)  1
would be inaccurate owing
to subtraction of two numbers that are nearly equal.
These functions returns 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.
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)))
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 welldefined for all other values of
z.
These functions return the base 10 logarithm of the complex value z. Mathematically, this corresponds to the value
log (z) = log10 (cabs (z)) + I * carg (z)
These functions are GNU extensions.
These functions return the complex square root of the argument z. Unlike the realvalued functions, they are defined for all values of z.
These functions return base raised to the power of
power. This is equivalent to cexp (y * clog (x))
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
The functions in this section are related to the exponential functions; see Exponentiation and Logarithms.
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.
These function return the hyperbolic cosine of x,
defined mathematically as (exp (x) + exp (x)) / 2
.
They may signal overflow if x is too large.
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.
These functions return the complex hyperbolic sine of z, defined
mathematically as (exp (z)  exp (z)) / 2
.
These functions return the complex hyperbolic cosine of z, defined
mathematically as (exp (z) + exp (z)) / 2
.
These functions return the complex hyperbolic tangent of z,
defined mathematically as csinh (z) / ccosh (z)
.
These functions return the inverse hyperbolic sine of xthe value whose hyperbolic sine is x.
These functions return the inverse hyperbolic cosine of xthe
value whose hyperbolic cosine is x. If x is less than
1
, acosh
signals a domain error.
These functions return the inverse hyperbolic tangent of xthe
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.
These functions return the inverse complex hyperbolic sine of zthe value whose complex hyperbolic sine is z.
These functions return the inverse complex hyperbolic cosine of zthe value whose complex hyperbolic cosine is z. Unlike the realvalued functions, there are no restrictions on the value of z.
These functions return the inverse complex hyperbolic tangent of zthe value whose complex hyperbolic tangent is z. Unlike the realvalued functions, there are no restrictions on the value of z.
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
These are some more exotic mathematical functions which are sometimes useful. Currently they only have realvalued versions.
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 
erfc
returns 1.0  erf(x)
, but computed in a
fashion that avoids roundoff error when x is large.
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 ∞ of t^(x1) 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 nonpositive integers.
lgamma
will raise the zero divide exception if evaluated at a
singularity.
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.
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, moreover lgamma
is
standardized in ISO C99 while gamma
is not.
tgamma
applies the gamma function to x. The gamma
function is defined as
gamma (x) = integral from 0 to ∞ of t^(x1) e^t dt 
This function was introduced in ISO C99.
j0
returns the Bessel function of the first kind of order 0 of
x. It may signal underflow if x is too large.
j1
returns the Bessel function of the first kind of order 1 of
x. It may signal underflow if x is too large.
jn
returns the Bessel function of the first kind of order
n of x. It may signal underflow if x is too large.
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 ∞
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 ∞
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 ∞
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
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 floatingpoint 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 floatingpoint number representation. Ideally the error for all functions is always less than 0.5ulps. Using rounding bits this is also possible and normally implemented for the basic operations. To achieve the same for the complex math functions requires a lot more work and this has not yet been done.
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 floatingpoint operations varies and also the existing hardware support is different.
Function  Generic  ix86  IA64  PowerPC  S/390 
acosf           
acos           
acosl    622    1   
acoshf           
acosh           
acoshl        1   
asinf           
asin           
asinl    1    2   
asinhf           
asinh           
asinhl        1   
atanf           
atan           
atanl           
atanhf        1  1 
atanh           
atanhl    1       
atan2f        1  1 
atan2           
atan2l        1  1 
cabsf           
cabs           
cabsl        1   
cacosf    0 + i 1  0 + i 1     
cacos           
cacosl    0 + i 2  0 + i 2  1 + i 1  0 + i 1 
cacoshf    9 + i 4  7 + i 0  7 + i 3  7 + i 3 
cacosh    1 + i 1  1 + i 1  1 + i 1  1 + i 1 
cacoshl    6 + i 1  7 + i 1  1 + i 0  0 + i 1 
cargf           
carg           
cargl           
casinf    1 + i 1  1 + i 1  1 + i 0  1 + i 0 
casin    1 + i 0  1 + i 0  1 + i 0  1 + i 0 
casinl    2 + i 2  2 + i 2  1 + i 1  0 + i 1 
casinhf    1 + i 6  1 + i 6  1 + i 6  1 + i 6 
casinh    5 + i 3  5 + i 3  5 + i 3  5 + i 3 
casinhl    5 + i 5  5 + i 5  4 + i 1  4 + i 2 
catanf    0 + i 1  0 + i 1  4 + i 1  4 + i 1 
catan    0 + i 1  0 + i 1  0 + i 1  0 + i 1 
catanl        1 + i 1  0 + i 1 
catanhf    1 + i 0    0 + i 6  0 + i 6 
catanh    2 + i 0  4 + i 0  4 + i 0  4 + i 0 
catanhl    1 + i 0  1 + i 0    1 + i 1 
cbrtf           
cbrt        1  1 
cbrtl    1    1  1 
ccosf    0 + i 1  0 + i 1  1 + i 1  1 + i 1 
ccos    1 + i 0  1 + i 0  1 + i 0  1 + i 0 
ccosl    1 + i 1  1 + i 1  1 + i 1  1 + i 1 
ccoshf    1 + i 1  1 + i 1  1 + i 1  1 + i 1 
ccosh    1 + i 1  1 + i 1  1 + i 0  1 + i 0 
ccoshl    0 + i 1  0 + i 1  1 + i 2  1 + i 1 
ceilf           
ceil           
ceill           
cexpf      1 + i 1  1 + i 1  1 + i 1 
cexp           
cexpl    1 + i 1  0 + i 1  2 + i 1  1 + i 1 
cimagf           
cimag           
cimagl           
clogf    1 + i 0  1 + i 0  1 + i 3  1 + i 3 
clog           
clogl    1 + i 0  1 + i 0  2 + i 1  1 + i 0 
clog10f    1 + i 1  1 + i 1  1 + i 5  1 + i 5 
clog10    1 + i 1  1 + i 1  0 + i 1  0 + i 1 
clog10l    1 + i 1  1 + i 1  3 + i 1  1 + i 1 
conjf           
conj           
conjl           
copysignf           
copysign           
copysignl           
cosf    1  1  1  1 
cos    2  2  2  2 
cosl    1  1  1  1 
coshf           
cosh           
coshl        1   
cpowf    4 + i 3  5 + i 3  5 + i 2  4 + i 2 
cpow    1 + i 2  2 + i 2  2 + i 2  2 + i 2 
cpowl    763 + i 2  6 + i 4  2 + i 2  10 + i 1 
cprojf           
cproj           
cprojl        0 + i 1   
crealf           
creal           
creall           
csinf    1 + i 1  1 + i 1     
csin           
csinl    1 + i 0  1 + i 0  1 + i 0  1 + i 1 
csinhf    1 + i 1  1 + i 1  1 + i 1  1 + i 1 
csinh    1 + i 1  1 + i 1  0 + i 1  0 + i 1 
csinhl    1 + i 2  1 + i 2  1 + i 1  1 + i 0 
csqrtf      1 + i 0  1 + i 0  1 + i 0 
csqrt           
csqrtl        1 + i 1  1 + i 1 
ctanf    0 + i 1  0 + i 1     
ctan    1 + i 1  1 + i 1  1 + i 1  1 + i 1 
ctanl    439 + i 3  2 + i 1  1 + i 1  1 + i 2 
ctanhf    1 + i 1  0 + i 1  2 + i 1  2 + i 1 
ctanh    1 + i 1  1 + i 1  1 + i 0  1 + i 0 
ctanhl    5 + i 25  1 + i 24  1 + i 1  1 + i 1 
erff           
erf    1  1  1  1 
erfl        1   
erfcf    1  1  1  1 
erfc    1  1  1  1 
erfcl    1  1  1  1 
expf           
exp           
expl        1   
exp10f      2  2  2 
exp10      6  6  6 
exp10l    8  3  8  1 
exp2f           
exp2           
exp2l        2  2 
expm1f        1  1 
expm1        1  1 
expm1l      1    1 
fabsf           
fabs           
fabsl           
fdimf           
fdim           
fdiml           
floorf           
floor           
floorl           
fmaf           
fma           
fmal           
fmaxf           
fmax           
fmaxl           
fminf           
fmin           
fminl           
fmodf           
fmod           
fmodl           
frexpf           
frexp           
frexpl           
gammaf           
gamma    1       
gammal    1  1  1  1 
hypotf    1  1  1  1 
hypot           
hypotl        1   
ilogbf           
ilogb           
ilogbl           
j0f    2  2  2  2 
j0    3  3  3  3 
j0l    1  2  1  2 
j1f    1  2  2  2 
j1    1  1  1  1 
j1l    1  1  1  4 
jnf    2  4  4  4 
jn    5  3  3  4 
jnl    2  2  4  4 
lgammaf    2  2  2  2 
lgamma    1  1  1  1 
lgammal    1  1  3  1 
lrintf           
lrint           
lrintl           
llrintf           
llrint           
llrintl           
logf    1  1     
log           
logl        1   
log10f    1  1  2  2 
log10        1  1 
log10l    1  1  1  1 
log1pf        1  1 
log1p           
log1pl        1  1 
log2f           
log2           
log2l        1  1 
logbf           
logb           
logbl           
lroundf           
lround           
lroundl           
llroundf           
llround           
llroundl           
modff           
modf           
modfl           
nearbyintf           
nearbyint           
nearbyintl           
nextafterf           
nextafter           
nextafterl           
nexttowardf           
nexttoward           
nexttowardl           
powf           
pow           
powl        1   
remainderf           
remainder           
remainderl           
remquof           
remquo           
remquol           
rintf           
rint           
rintl           
roundf           
round           
roundl           
scalbf           
scalb           
scalbl           
scalbnf           
scalbn           
scalbnl           
scalblnf           
scalbln           
scalblnl           
sinf           
sin           
sinl        1   
sincosf    1  1  1  1 
sincos    1  1  1  1 
sincosl    1  1  1  1 
sinhf           
sinh    1       
sinhl        1   
sqrtf           
sqrt           
sqrtl          1 
tanf           
tan    1  1  1  1 
tanl        1   
tanhf           
tanh           
tanhl        1  1 
tgammaf    1  1  1  1 
tgamma    2  1  1  1 
tgammal    1  1  1  1 
truncf           
trunc           
truncl           
y0f    1  1  1  1 
y0    2  2  2  2 
y0l    1  1  1  3 
y1f    2  2  2  2 
y1    2  3  3  3 
y1l    1  1  2  1 
ynf    3  2  2  2 
yn    2  3  3  3 
ynl    4  2  2  5 
Function  SH4  Sparc 32bit  Sparc 64bit  x86_64/fpu 
acosf         
acos         
acosl        1 
acoshf         
acosh         
acoshl         
asinf  2       
asin  1       
asinl        1 
asinhf         
asinh         
asinhl         
atanf         
atan         
atanl         
atanhf    1  1  1 
atanh  1       
atanhl        1 
atan2f  4  6  6  1 
atan2         
atan2l    1  1   
cabsf  1       
cabs  1       
cabsl         
cacosf  1 + i 1      0 + i 1 
cacos  1 + i 0       
cacosl    0 + i 1  0 + i 1  0 + i 2 
cacoshf  7 + i 3  7 + i 3  7 + i 3  7 + i 3 
cacosh  1 + i 1  1 + i 1  1 + i 1  1 + i 1 
cacoshl    5 + i 1  5 + i 1  6 + i 1 
cargf         
carg         
cargl         
casinf  2 + i 1  1 + i 0  1 + i 0  1 + i 1 
casin  3 + i 0  1 + i 0  1 + i 0  1 + i 0 
casinl    0 + i 1  0 + i 1  2 + i 2 
casinhf  1 + i 6  1 + i 6  1 + i 6  1 + i 6 
casinh  5 + i 3  5 + i 3  5 + i 3  5 + i 3 
casinhl    4 + i 2  4 + i 2  5 + i 5 
catanf  4 + i 1  4 + i 1  4 + i 1  4 + i 1 
catan  0 + i 1  0 + i 1  0 + i 1  0 + i 1 
catanl    0 + i 1  0 + i 1   
catanhf  1 + i 6  0 + i 6  0 + i 6  0 + i 6 
catanh  4 + i 1  4 + i 0  4 + i 0  4 + i 0 
catanhl    1 + i 1  1 + i 1  1 + i 0 
cbrtf         
cbrt  1  1  1  1 
cbrtl    1  1  1 
ccosf  0 + i 1  1 + i 1  1 + i 1  1 + i 1 
ccos  1 + i 1  1 + i 0  1 + i 0  1 + i 0 
ccosl    1 + i 1  1 + i 1  1 + i 1 
ccoshf  1 + i 1  1 + i 1  1 + i 1  1 + i 1 
ccosh  1 + i 1  1 + i 0  1 + i 0  1 + i 1 
ccoshl    1 + i 1  1 + i 1  0 + i 1 
ceilf         
ceil         
ceill         
cexpf  1 + i 1  1 + i 1  1 + i 1  1 + i 1 
cexp  1 + i 0       
cexpl    1 + i 1  1 + i 1  0 + i 1 
cimagf         
cimag         
cimagl         
clogf  0 + i 3  1 + i 3  1 + i 3  1 + i 3 
clog  0 + i 1       
clogl    1 + i 0  1 + i 0  1 + i 0 
clog10f  1 + i 5  1 + i 5  1 + i 5  1 + i 5 
clog10  1 + i 1  0 + i 1  0 + i 1  1 + i 1 
clog10l    1 + i 1  1 + i 1  1 + i 1 
conjf         
conj         
conjl         
copysignf         
copysign         
copysignl         
cosf  1  1  1  1 
cos  2  2  2  2 
cosl    1  1  1 
coshf         
cosh         
coshl         
cpowf  4 + i 2  4 + i 2  4 + i 2  5 + i 2 
cpow  1 + i 1.1031  2 + i 2  2 + i 2  2 + i 2 
cpowl    10 + i 1  10 + i 1  5 + i 2 
cprojf         
cproj         
cprojl         
crealf         
creal         
creall         
csinf  0 + i 1      0 + i 1 
csin        0 + i 1 
csinl    1 + i 1  1 + i 1  1 + i 0 
csinhf  1 + i 1  1 + i 1  1 + i 1  1 + i 1 
csinh  0 + i 1  0 + i 1  0 + i 1  1 + i 1 
csinhl    1 + i 0  1 + i 0  1 + i 2 
csqrtf  1 + i 1  1 + i 0  1 + i 0  1 + i 0 
csqrt  1 + i 0       
csqrtl    1 + i 1  1 + i 1   
ctanf  1 + i 1      0 + i 1 
ctan  1 + i 1  1 + i 1  1 + i 1  1 + i 1 
ctanl    1 + i 2  1 + i 2  439 + i 3 
ctanhf  2 + i 1  2 + i 1  2 + i 1  2 + i 1 
ctanh  2 + i 2  1 + i 0  1 + i 0  1 + i 1 
ctanhl    1 + i 1  1 + i 1  5 + i 25 
erff         
erf    1  1  1 
erfl         
erfcf  12       
erfc  24  1  1  1 
erfcl    1  1  1 
expf         
exp         
expl         
exp10f  2  2  2  2 
exp10  6  6  6  6 
exp10l    1  1  8 
exp2f         
exp2         
exp2l    2  2   
expm1f  1  1  1  1 
expm1    1  1  1 
expm1l    1  1   
fabsf         
fabs         
fabsl         
fdimf         
fdim         
fdiml         
floorf         
floor         
floorl         
fmaf         
fma         
fmal         
fmaxf         
fmax         
fmaxl         
fminf         
fmin         
fminl         
fmodf  1       
fmod  2       
fmodl         
frexpf         
frexp         
frexpl         
gammaf         
gamma         
gammal    1  1  1 
hypotf  1  1  1  1 
hypot  1       
hypotl         
ilogbf         
ilogb         
ilogbl         
j0f  2  2  2  2 
j0  2  2  2  2 
j0l    2  2  1 
j1f  2  2  2  2 
j1  1  1  1  1 
j1l    4  4  1 
jnf  4  4  4  4 
jn  6  4  4  4 
jnl    4  4  2 
lgammaf  2  2  2  2 
lgamma  1  1  1  1 
lgammal    1  1  1 
lrintf         
lrint         
lrintl         
llrintf         
llrint         
llrintl         
logf  1       
log  1       
logl         
log10f  1  2  2  2 
log10  1  1  1  1 
log10l    1  1  1 
log1pf  1  1  1  1 
log1p  1       
log1pl    1  1   
log2f  1       
log2  1       
log2l    1  1   
logbf         
logb         
logbl         
lroundf         
lround         
lroundl         
llroundf         
llround         
llroundl         
modff         
modf         
modfl         
nearbyintf         
nearbyint         
nearbyintl         
nextafterf         
nextafter         
nextafterl         
nexttowardf         
nexttoward         
nexttowardl         
powf         
pow         
powl         
remainderf         
remainder         
remainderl         
remquof         
remquo         
remquol         
rintf         
rint         
rintl         
roundf         
round         
roundl         
scalbf         
scalb         
scalbl         
scalbnf         
scalbn         
scalbnl         
scalblnf         
scalbln         
scalblnl         
sinf         
sin         
sinl         
sincosf  1  1  1  1 
sincos  1  1  1  1 
sincosl    1  1  1 
sinhf  1       
sinh  1       
sinhl         
sqrtf         
sqrt         
sqrtl    1  1   
tanf         
tan  0.5  1  1  1 
tanl         
tanhf  1       
tanh  1       
tanhl    1  1   
tgammaf  1  1  1  1 
tgamma  1  1  1  1 
tgammal    1  1  1 
truncf         
trunc         
truncl         
y0f  1  1  1  1 
y0  2  2  2  2 
y0l    3  3  1 
y1f  2  2  2  2 
y1  3  3  3  3 
y1l    1  1  1 
ynf  2  2  2  2 
yn  3  3  3  3 
ynl    5  5  4 
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
This section describes the GNU facilities for generating a series of pseudorandom 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 pseudorandom 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.
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.
The GNU library 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 floatingpoint numbers. These functions are required
by the XPG standard and therefore will be present in all modern Unix
systems.
19.8.1 ISO C Random Number Functions  rand and friends.
 
19.8.2 BSD Random Number Functions  random and friends.
 
19.8.3 SVID Random Number Function  drand48 and friends.

[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
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.
The value of this macro is an integer constant representing the largest
value the rand
function can return. In the GNU library, it is
2147483647
, which is the largest signed integer representable in
32 bits. In other libraries, it may be as low as 32767
.
The rand
function returns the next pseudorandom number in the
series. The value ranges from 0
to RAND_MAX
.
This function establishes seed as the seed for a new series of
pseudorandom 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 pseudorandom series each time your program is
run, do srand (time (0))
.
POSIX.1 extended the C standard functions to support reproducible random numbers in multithreaded programs. However, the extension is badly designed and unsuitable for serious work.
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.
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
This section describes a set of random number generation functions that are derived from BSD. There is no advantage to using these functions with the GNU C library; we support them for BSD compatibility only.
The prototypes for these functions are in `stdlib.h'.
This function returns the next pseudorandom number in the sequence.
The value returned ranges from 0
to RAND_MAX
.
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 32bit limitation,
though.
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 pseudorandom numbers each time your
program runs, do srandom (time (0))
.
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.
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 pseudorandom number generator.
The GNU C library contains four additional functions which contain the state as an explicit parameter and therefore make it possible to handle threadlocal PRNGs. Beside 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:
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.
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.
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.
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.
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.
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
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 48bit array.
The prototypes for these functions are in `stdlib.h'.
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 52bit
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
.
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.
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.
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.
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).
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.
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 sideeffect 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).
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
see16v initialize the least significant 16 bits of the internal
state, the lower 16 bits of seed16v[1]
initialize the midorder
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.
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 16bit value
c
.
All the above functions have in common that they use the global parameters for the congruential formula. In multithreaded 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 usersupplied 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 usersupplied buffer must be of type struct drand48_data
.
This type should be regarded as opaque and not manipulated directly.
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.
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 nonnegative if the call succeeded.
This function is a GNU extension and should not be used in portable programs.
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 nonnegative 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.
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 nonnegative the variable pointed to by result contains the result.
This function is a GNU extension and should not be used in portable programs.
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 nonnegative the variable pointed to by result contains the result.
This function is a GNU extension and should not be used in portable programs.
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 nonnegative 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.
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 nonnegative the function call succeeded.
This function is a GNU extension and should not be used in portable programs.
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 nonnegative 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 rerun 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.
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 nonnegative the function call succeeded.
This function is a GNU extension and should not be used in portable programs.
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
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 the GNU C Library provides optimizations for many of the frequentlyused 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 kind 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 ffastmath
is given to GNU CC.
In cases where the inline functions and macros are not wanted the symbol
__NO_MATH_INLINES
should be defined before any system header is
included. This will ensure that only library functions are used. Of
course, it can be determined for each file in the project whether
giving this option is preferable or not.
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 unpipelined, which can more than double calculation time.
[ << ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
This document was generated on February, 7 2011 using texi2html 1.76.