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.

All of the functions that operate on floating-point numbers accept arguments and return results of type double.  In the future, there may be additional functions that operate on float and long double values.  For example, cosf and cosl would be versions of the cos function that operate on float and long double arguments, respectively.  In the meantime, you should avoid using these names yourself.  See Reserved Names.

Domain and Range Errors
=======================

Many of the functions listed in this chapter are defined mathematically over a domain that is only a subset of real numbers.  For example, the acos function is defined over the domain between -1 and 1.  If you pass an argument to one of these functions that is outside the domain over which it is defined, the function sets errno to EDOM to indicate a domain error.  On machines that support IEEE 754 floating point, functions reporting error EDOM also return a NaN.

Some of these functions are defined mathematically to result in a complex value over parts of their domains.  The most familiar example of this is taking the square root of a negative number.  The functions in this chapter take only real arguments and return only real values; therefore, if the value ought to be nonreal, this is treated as a domain error.

A related problem is that the mathematical result of a function may not be representable as a floating point number.  If magnitude of the correct result is too large to be represented, the function sets errno to ERANGE to indicate a range error, and returns a particular very large value (named by the macro HUGE_VAL) or its negation (- HUGE_VAL).

If the magnitude of the result is too small, a value of zero is returned instead.  In this case, errno might or might not be set to ERANGE.

The only completely reliable way to check for domain and range errors is to set errno to 0 before you call the mathematical function and test errno afterward.  As a consequence of this use of errno, use of the mathematical functions is not reentrant if you check for errors.

None of the mathematical functions ever generates signals as a result of domain or range errors.  In particular, this means that you won't see SIGFPE signals generated within these functions.  (see Signal Handling, for more information about signals.)

 -- Macro: double HUGE_VAL
     An expression representing a particular very large number.  On machines that use IEEE 754/IEEE 854 floating point format, the value is ``infinity''.  On other machines, it's typically the largest positive number that can be represented.

     The value of this macro is used as the return value from various mathematical double returning functions in overflow situations.

 -- Macro: float HUGE_VALf
     This macro is similar to the HUGE_VAL macro except that it is used by functions returning float values.

     This macro is a GNU extension.

 -- Macro: long double HUGE_VALl
     This macro is similar to the HUGE_VAL macro except that it is used by functions returning long double values.  The value is only different from HUGE_VAL if the architecture really supports long double values.

     This macro is a GNU extension.

For more information about floating-point representations and limits, see Floating Point Parameters.  In particular, the macro DBL_MAX might be more appropriate than HUGE_VAL for many uses other than testing for an error in a mathematical function.

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 doesn't define a symbolic constant for pi, but you can define your own if you need one:

     #define PI 3.14159265358979323846264338327

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

 -- Function: double sin (double X)
     This function returns 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)
     This function returns 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)
     This function returns the tangent of X, where X is given in radians.

     The following errno error conditions are defined for this function:

     ERANGE
          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 sets errno to ERANGE and returns either positive or negative HUGE_VAL.

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)
     This function computes 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).

     asin fails, and sets errno to EDOM, if X is out of range.  The arc sine function is defined mathematically only over the domain -1 to 1.

 -- Function: double acos (double X)
     This function computes 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).

     acos fails, and sets errno to EDOM, if X is out of range.  The arc cosine function is defined mathematically only over the domain -1 to 1.

 -- Function: double atan (double X)
     This function computes 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)
     This is the two argument arc tangent function.  It is similar to computing the arc tangent of Y/X, except that the signs of both arguments are used to determine the quadrant of the result, and X is permitted to be zero.  The return value is given in radians and is in the range -pi to pi, inclusive.

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

     The function atan2 sets errno to EDOM if both X and Y are zero; the return value is not defined in this case.

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

 -- Function: double exp (double X)
     The exp function returns the value of e (the base of natural logarithms) raised to power X.

     The function fails, and sets errno to ERANGE, if the magnitude of the result is too large to be representable.

 -- Function: double log (double X)
     This function returns the natural logarithm of X.  exp (log (X)) equals X, exactly in mathematics and approximately in C.

     The following errno error conditions are defined for this function:

     EDOM
          The argument X is negative.  The log function is defined mathematically to return a real result only on positive arguments.

     ERANGE
          The argument is zero.  The log of zero is not defined.

 -- Function: double log10 (double X)
     This function returns the base-10 logarithm of X.  Except for the different base, it is similar to the log function.  In fact, log10 (X) equals log (X) / log (10).

 -- Function: double pow (double BASE, double POWER)
     This is a general exponentiation function, returning BASE raised to POWER.

     The following errno error conditions are defined for this function:

     EDOM
          The argument BASE is negative and POWER is not an integral value.  Mathematically, the result would be a complex number in this case.

     ERANGE
          An underflow or overflow condition was detected in the result.

 -- Function: double sqrt (double X)
     This function returns the nonnegative square root of X.

     The sqrt function fails, and sets errno to EDOM, if X is negative.  Mathematically, the square root would be a complex number.

 -- Function: double cbrt (double X)
     This function returns the cube root of X.  This function cannot fail; every representable real value has a representable real cube root.

 -- Function: double hypot (double X, double Y)
     The hypot function returns 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.)  See also the function cabs in Absolute Value.

 -- Function: double expm1 (double X)
     This function returns a value equivalent to exp (X) - 1. It is computed in a way that is accurate even if the value of X is near zero---a case where exp (X) - 1 would be inaccurate due to subtraction of two numbers that are nearly equal.

 -- Function: double log1p (double X)
     This function returns a value equivalent to log (1 + X). It is computed in a way that is accurate even if the value of X is near zero.

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

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

 -- Function: double sinh (double X)
     The sinh function returns the hyperbolic sine of X, defined mathematically as exp (X) - exp (-X) / 2.  The function fails, and sets errno to ERANGE, if the value of X is too large; that is, if overflow occurs.

 -- Function: double cosh (double X)
     The cosh function returns the hyperbolic cosine of X, defined mathematically as exp (X) + exp (-X) / 2. The function fails, and sets errno to ERANGE, if the value of X is too large; that is, if overflow occurs.

 -- Function: double tanh (double X)
     This function returns the hyperbolic tangent of X, whose mathematical definition is sinh (X) / cosh (X).

 -- Function: double asinh (double X)
     This function returns the inverse hyperbolic sine of X---the value whose hyperbolic sine is X.

 -- Function: double acosh (double X)
     This function returns the inverse hyperbolic cosine of X---the value whose hyperbolic cosine is X.  If X is less than 1, acosh returns HUGE_VAL.

 -- Function: double atanh (double X)
     This function returns the inverse hyperbolic tangent of X---the value whose hyperbolic tangent is X.  If the absolute value of X is greater than or equal to 1, atanh returns HUGE_VAL.

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 at all times 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 truly random numbers, not just pseudo-random, specify a seed based on the current time.

You can get 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 another set derived from BSD.  We recommend you use the standard ones, rand and srand.

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 expression that represents the maximum possible value returned by the rand function.  In the GNU library, it is 037777777, which is the largest signed integer representable in 32 bits.  In other libraries, it may be as low as 32767.

 -- Function: int rand ()
     The rand function returns the next pseudo-random number in the series.  The value is in the range 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 truly random numbers (not just pseudo-random), do srand (time (0)).

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 ()
     This function returns the next pseudo-random number in the sequence. The range of values returned is from 0 to RAND_MAX.

 -- Function: void srandom (unsigned int SEED)
     The srandom function sets the seed for the current random number state 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 truly random numbers (not just pseudo-random), 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.  The size must be at least 8 bytes, and optimal sizes are 8, 16, 32, 64, 128, and 256.  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 thise value later as an argument to setstate to restore that state.