[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

19. 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.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

19.1 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 _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] [ ? ]

19.2 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)

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)

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)

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)

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.)

Function: complex double csin (complex double z)
Function: complex float csinf (complex float z)
Function: complex long double csinl (complex long double z)

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)

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)

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] [ ? ]

19.3 Inverse Trigonometric Functions

These are the usual arc sine, arc cosine and arc tangent 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)

These functions compute the arc sine 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 arc sine 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)

These functions compute the arc cosine 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 arc cosine 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)

These functions compute the arc tangent 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)

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 x-axis. 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.

Function: complex double casin (complex double z)
Function: complex float casinf (complex float z)
Function: complex long double casinl (complex long double z)

These functions compute the complex arc sine 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)

These functions compute the complex arc cosine 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)

These functions compute the complex arc tangent of z--that is, the value whose tangent is z. The value is in units of radians.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

19.4 Exponentiation and Logarithms

Function: double exp (double x)
Function: float expf (float x)
Function: long double expl (long double x)

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)

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: double pow10 (double x)
Function: float pow10f (float x)
Function: long double pow10l (long double x)

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.

Function: double log (double x)
Function: float logf (float x)
Function: long double logl (long double x)

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)

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)

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)

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 &infin;. If x is zero, logb returns &infin;. It does not signal.

Function: int ilogb (double x)
Function: int ilogbf (float x)
Function: int ilogbl (long double x)

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 floating-point number. `math.h' defines constants so you can check for this.

Macro: int FP_ILOGB0

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: int FP_ILOGBNAN

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.  */
      }
  }
Function: double pow (double base, double power)
Function: float powf (float base, float power)
Function: long double powl (long double base, long double power)

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)

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)

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)

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)

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)

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.

Function: complex double cexp (complex double z)
Function: complex float cexpf (complex float z)
Function: complex long double cexpl (complex long double z)

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)

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)

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.

Function: complex double csqrt (complex double z)
Function: complex float csqrtf (complex float z)
Function: complex long double csqrtl (complex long double z)

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)

These functions return base raised to the power of power. This is equivalent to cexp (y * clog (x))


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

19.5 Hyperbolic Functions

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

Function: double sinh (double x)
Function: float sinhf (float x)
Function: long double sinhl (long double x)

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)

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.

Function: double tanh (double x)
Function: float tanhf (float x)
Function: long double tanhl (long double x)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

19.6 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)

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)

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)

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 &infin; 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)

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.

Function: double gamma (double x)
Function: float gammaf (float x)
Function: long double gammal (long double x)

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.

Function: double tgamma (double x)
Function: float tgammaf (float x)
Function: long double tgammal (long double x)

tgamma applies the gamma function to x. The gamma function is defined as

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

This function was introduced in ISO C99.

Function: double j0 (double x)
Function: float j0f (float x)
Function: long double j0l (long double x)

j0 returns the Bessel function of the first kind of order 0 of x. It may signal underflow if x is too large.

Function: double j1 (double x)
Function: float j1f (float x)
Function: long double j1l (long double x)

j1 returns the Bessel function of the first kind of order 1 of x. It may signal underflow if x is too large.

Function: double jn (int n, double x)
Function: float jnf (int n, float x)
Function: long double jnl (int n, long double x)

jn returns the Bessel function of the first kind of order n of x. It may signal underflow if x is too large.

Function: double y0 (double x)
Function: float y0f (float x)
Function: long double y0l (long double x)

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 -&infin;

Function: double y1 (double x)
Function: float y1f (float x)
Function: long double y1l (long double x)

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 -&infin;

Function: double yn (int n, double x)
Function: float ynf (int n, float x)
Function: long double ynl (int n, long double x)

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 -&infin;


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

19.7 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&middot;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. 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 floating-point 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 32-bit

Sparc 64-bit

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] [ ? ]

19.8 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.

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 floating-point numbers. These functions are required by the XPG standard and therefore will be present in all modern Unix systems.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

19.8.1 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

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.

Function: int rand (void)

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)

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)

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] [ ? ]

19.8.2 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 the GNU C library; we support them for BSD compatibility only.

The prototypes for these functions are in `stdlib.h'.

Function: long int random (void)

This function returns the next pseudo-random 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 32-bit limitation, though.

Function: void srandom (unsigned int seed)

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: void * initstate (unsigned int seed, void *state, size_t size)

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: void * setstate (void *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 pseudo-random 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 thread-local 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:

Data Type: struct random_data

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)

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)

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)

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)

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] [ ? ]

19.8.3 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)

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])

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)

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])

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)

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])

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)

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])

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 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])

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)

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)

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, double *result)

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)

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, double *result)

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)

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)

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)

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)

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.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

19.9 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 the GNU C Library 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 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 -ffast-math 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 un-pipelined, which can more than double calculation time.


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated on February, 7 2011 using texi2html 1.76.