@Go to the ‰ŠP�”›first@�”˜, ‰ŠP�”›previous@�”˜, ‰ŠP�”›next@�”˜, ‰ŠP�”›last@�”˜ section, ‰ŠP�”›table of contents@�”˜.
Œ

ˆ‰SEC253Št�”›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 H`math.h'@. ‰IDX1284Š
@�”˜All of the functions that operate on floating-point numbers accept arguments and return results of type Hdouble@. In the future, there may be additional functions that operate on Hfloat@ and Hlong double@ values. For example, Hcosf@ and Hcosl@ would be versions of the Hcos@ function that operate on Hfloat@ and Hlong double@ arguments, respectively. In the meantime, you should avoid using these names yourself. See section ‰ŠP�”›Reserved Names@�”˜.
ˆ‰SEC254Št�”›Domain and Range Errors

‰IDX1285Š@�”˜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 Hacos@ function is defined over the domain between H-1@ and H1@. If you pass an argument to one of these functions that is outside the domain over which it is defined, the function sets Herrno@ to HEDOM@ to indicate a Bdomain error@. On machines that support IEEE 754 floating point, functions reporting error HEDOM@ 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.
‰IDX1286Š@�”˜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 Herrno@ to HERANGE@ to indicate a Brange error@, and returns a particular very large value (named by the macro HHUGE_VAL@) or its negation (H- HUGE_VAL@).
If the magnitude of the result is too small, a value of zero is returned instead. In this case, Herrno@ might or might not be set to HERANGE@.
The only completely reliable way to check for domain and range errors is to set Herrno@ to H0@ before you call the mathematical function and test Herrno@ afterward. As a consequence of this use of Herrno@, 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 HSIGFPE@ signals generated within these functions. (See section ‰ŠP�”›Signal Handling@�”˜, for more information about signals.)

AMacro:@ double DHUGE_VAL
Â‰IDX1287Š@�”˜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 Hdouble@ returning functions in overflow situations.À

AMacro:@ float DHUGE_VALf
Â‰IDX1288Š@�”˜This macro is similar to the HHUGE_VAL@ macro except that it is used by functions returning Hfloat@ values.
This macro is a GNU extension.À

AMacro:@ long double DHUGE_VALl
Â‰IDX1289Š@�”˜This macro is similar to the HHUGE_VAL@ macro except that it is used by functions returning Hlong double@ values. The value is only different from HHUGE_VAL@ if the architecture really supports Hlong double@ values.
This macro is a GNU extension.À

For more information about floating-point representations and limits, see section ‰ŠP�”›Floating Point Parameters@�”˜. In particular, the macro HDBL_MAX@ might be more appropriate than HHUGE_VAL@ for many uses other than testing for an error in a mathematical function.
ˆ‰SEC255Št�”›Trigonometric Functions

‰IDX1290Š
@�”˜These are the familiar Hsin@, Hcos@, and Htan@ functions. The arguments to all of these functions are in units of radians; recall that pi radians equals 180 degrees.
‰IDX1291Š@�”˜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 Hacos (-1.0)@.

AFunction:@ double Dsin@ B(double Bx@)
Â‰IDX1292Š@�”˜This function returns the sine of Bx@, where Bx@ is given in radians. The return value is in the range H-1@ to H1@.À

AFunction:@ double Dcos@ B(double Bx@)
Â‰IDX1293Š@�”˜This function returns the cosine of Bx@, where Bx@ is given in radians. The return value is in the range H-1@ to H1@.À

AFunction:@ double Dtan@ B(double Bx@)
Â‰IDX1294Š@�”˜This function returns the tangent of Bx@, where Bx@ is given in radians.
The following Herrno@ error conditions are defined for this function:
HERANGE
Ä@Mathematically, the tangent function has singularities at odd multiples of pi/2. If the argument Bx@ is too close to one of these singularities, Htan@ sets Herrno@ to HERANGE@ and returns either positive or negative HHUGE_VAL@.Â
À

ˆ‰SEC256Št�”›Inverse Trigonometric Functions

‰IDX1295Š
@�”˜These are the usual arc sine, arc cosine and arc tangent functions, which are the inverses of the sine, cosine and tangent functions, respectively.

AFunction:@ double Dasin@ B(double Bx@)
Â‰IDX1296Š@�”˜This function computes the arc sine of Bx@---that is, the value whose sine is Bx@. The value is in units of radians. Mathematically, there are infinitely many such values; the one actually returned is the one between H-pi/2@ and Hpi/2@ (inclusive).
Hasin@ fails, and sets Herrno@ to HEDOM@, if Bx@ is out of range. The arc sine function is defined mathematically only over the domain H-1@ to H1@.À

AFunction:@ double Dacos@ B(double Bx@)
Â‰IDX1297Š@�”˜This function computes the arc cosine of Bx@---that is, the value whose cosine is Bx@. The value is in units of radians. Mathematically, there are infinitely many such values; the one actually returned is the one between H0@ and Hpi@ (inclusive).
Hacos@ fails, and sets Herrno@ to HEDOM@, if Bx@ is out of range. The arc cosine function is defined mathematically only over the domain H-1@ to H1@.À

AFunction:@ double Datan@ B(double Bx@)
Â‰IDX1298Š@�”˜This function computes the arc tangent of Bx@---that is, the value whose tangent is Bx@. The value is in units of radians. Mathematically, there are infinitely many such values; the one actually returned is the one between H-pi/2@ and Hpi/2@ (inclusive).À

AFunction:@ double Datan2@ B(double By@, double Bx@)
Â‰IDX1299Š@�”˜This is the two argument arc tangent function. It is similar to computing the arc tangent of By@/Bx@, except that the signs of both arguments are used to determine the quadrant of the result, and Bx@ is permitted to be zero. The return value is given in radians and is in the range H-pi@ to Hpi@, inclusive.
If Bx@ and By@ are coordinates of a point in the plane, Hatan2@ returns the signed angle between the line from the origin to that point and the x-axis. Thus, Hatan2@ is useful for converting Cartesian coordinates to polar coordinates. (To compute the radial coordinate, use Hhypot@; see section ‰ŠP�”›Exponentiation and Logarithms@�”˜.)
The function Hatan2@ sets Herrno@ to HEDOM@ if both Bx@ and By@ are zero; the return value is not defined in this case.À

ˆ‰SEC257Št�”›Exponentiation and Logarithms

‰IDX1300Š‰IDX1301Š‰IDX1302Š

A�”˜Function:@ double Dexp@ B(double Bx@)
Â‰IDX1303Š@�”˜The Hexp@ function returns the value of e (the base of natural logarithms) raised to power Bx@.
The function fails, and sets Herrno@ to HERANGE@, if the magnitude of the result is too large to be representable.À

AFunction:@ double Dlog@ B(double Bx@)
Â‰IDX1304Š@�”˜This function returns the natural logarithm of Bx@. Hexp (log (JxH))@ equals Bx@, exactly in mathematics and approximately in C.
The following Herrno@ error conditions are defined for this function:
HEDOM
Ä@The argument Bx@ is negative. The log function is defined mathematically to return a real result only on positive arguments.
ÂHERANGE
Ä@The argument is zero. The log of zero is not defined.Â
À

AFunction:@ double Dlog10@ B(double Bx@)
Â‰IDX1305Š@�”˜This function returns the base-10 logarithm of Bx@. Except for the different base, it is similar to the Hlog@ function. In fact, Hlog10 (JxH)@ equals Hlog (JxH) / log (10)@.À

AFunction:@ double Dpow@ B(double Bbase@, double Bpower@)
Â‰IDX1306Š@�”˜This is a general exponentiation function, returning Bbase@ raised to Bpower@.
The following Herrno@ error conditions are defined for this function:
HEDOM
Ä@The argument Bbase@ is negative and Bpower@ is not an integral value. Mathematically, the result would be a complex number in this case.
ÂHERANGE
Ä@An underflow or overflow condition was detected in the result.Â
À

‰IDX1307Š
A�”˜Function:@ double Dsqrt@ B(double Bx@)
Â‰IDX1308Š@�”˜This function returns the nonnegative square root of Bx@.
The Hsqrt@ function fails, and sets Herrno@ to HEDOM@, if Bx@ is negative. Mathematically, the square root would be a complex number.À

‰IDX1309Š
A�”˜Function:@ double Dcbrt@ B(double Bx@)
Â‰IDX1310Š@�”˜This function returns the cube root of Bx@. This function cannot fail; every representable real value has a representable real cube root.À

AFunction:@ double Dhypot@ B(double Bx@, double By@)
Â‰IDX1311Š@�”˜The Hhypot@ function returns Hsqrt (JxH*JxH + JyH*JyH)@. (This is the length of the hypotenuse of a right triangle with sides of length Bx@ and By@, or the distance of the point (Bx@, By@) from the origin.) See also the function Hcabs@ in section ‰ŠP�”›Absolute Value@�”˜.À

AFunction:@ double Dexpm1@ B(double Bx@)
Â‰IDX1312Š@�”˜This function returns a value equivalent to Hexp (JxH) - 1@. It is computed in a way that is accurate even if the value of Bx@ is near zero--a case where Hexp (JxH) - 1@ would be inaccurate due to subtraction of two numbers that are nearly equal.À

AFunction:@ double Dlog1p@ B(double Bx@)
Â‰IDX1313Š@�”˜This function returns a value equivalent to Hlog (1 + JxH)@. It is computed in a way that is accurate even if the value of Bx@ is near zero.À

ˆ‰SEC258Št�”›Hyperbolic Functions

‰IDX1314Š
@�”˜The functions in this section are related to the exponential functions; see section ‰ŠP�”›Exponentiation and Logarithms@�”˜.

AFunction:@ double Dsinh@ B(double Bx@)
Â‰IDX1315Š@�”˜The Hsinh@ function returns the hyperbolic sine of Bx@, defined mathematically as Hexp (JxH) - exp (-JxH) / 2@. The function fails, and sets Herrno@ to HERANGE@, if the value of Bx@ is too large; that is, if overflow occurs.À

AFunction:@ double Dcosh@ B(double Bx@)
Â‰IDX1316Š@�”˜The Hcosh@ function returns the hyperbolic cosine of Bx@, defined mathematically as Hexp (JxH) + exp (-JxH) / 2@. The function fails, and sets Herrno@ to HERANGE@, if the value of Bx@ is too large; that is, if overflow occurs.À

AFunction:@ double Dtanh@ B(double Bx@)
Â‰IDX1317Š@�”˜This function returns the hyperbolic tangent of Bx@, whose mathematical definition is Hsinh (JxH) / cosh (JxH)@.À

‰IDX1318Š

A�”˜Function:@ double Dasinh@ B(double Bx@)
Â‰IDX1319Š@�”˜This function returns the inverse hyperbolic sine of Bx@---the value whose hyperbolic sine is Bx@.À

AFunction:@ double Dacosh@ B(double Bx@)
Â‰IDX1320Š@�”˜This function returns the inverse hyperbolic cosine of Bx@---the value whose hyperbolic cosine is Bx@. If Bx@ is less than H1@, Hacosh@ returns HHUGE_VAL@.À

AFunction:@ double Datanh@ B(double Bx@)
Â‰IDX1321Š@�”˜This function returns the inverse hyperbolic tangent of Bx@---the value whose hyperbolic tangent is Bx@. If the absolute value of Bx@ is greater than or equal to H1@, Hatanh@ returns HHUGE_VAL@.À

ˆ‰SEC259Št�”›Pseudo-Random Numbers

‰IDX1322Š‰IDX1323Š‰IDX1324Š
@�”˜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 Bseed@ 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 Bexactly 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, Hrand@ and Hsrand@.
ˆ‰SEC260ŠT�”›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 H`stdlib.h'@ in your program. ‰IDX1325Š

A�”˜Macro:@ int DRAND_MAX
Â‰IDX1326Š@�”˜The value of this macro is an integer constant expression that represents the maximum possible value returned by the Hrand@ function. In the GNU library, it is H037777777@, which is the largest signed integer representable in 32 bits. In other libraries, it may be as low as H32767@.À

AFunction:@ int Drand@ B()
Â‰IDX1327Š@�”˜The Hrand@ function returns the next pseudo-random number in the series. The value is in the range from H0@ to HRAND_MAX@.À

AFunction:@ void Dsrand@ B(unsigned int Bseed@)
Â‰IDX1328Š@�”˜This function establishes Bseed@ as the seed for a new series of pseudo-random numbers. If you call Hrand@ before a seed has been established with Hsrand@, it uses the value H1@ as a default seed.
To produce truly random numbers (not just pseudo-random), do Hsrand (time (0))@.À

ˆ‰SEC261ŠT�”›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 H`stdlib.h'@. ‰IDX1329Š

A�”˜Function:@ long int Drandom@ B()
Â‰IDX1330Š@�”˜This function returns the next pseudo-random number in the sequence. The range of values returned is from H0@ to HRAND_MAX@.À

AFunction:@ void Dsrandom@ B(unsigned int Bseed@)
Â‰IDX1331Š@�”˜The Hsrandom@ function sets the seed for the current random number state based on the integer Bseed@. If you supply a Bseed@ value of H1@, this will cause Hrandom@ to reproduce the default set of random numbers.
To produce truly random numbers (not just pseudo-random), do Hsrandom (time (0))@.À

AFunction:@ void * Dinitstate@ B(unsigned int Bseed@, void *Bstate@, size_t Bsize@)
Â‰IDX1332Š@�”˜The Hinitstate@ function is used to initialize the random number generator state. The argument Bstate@ is an array of Bsize@ 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 Bstate@ 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 Hsetstate@ to restore that state.À

AFunction:@ void * Dsetstate@ B(void *Bstate@)
Â‰IDX1333Š@�”˜The Hsetstate@ function restores the random number state information Bstate@. The argument must have been the result of a previous call to Binitstate@ or Bsetstate@.
The return value is the previous value of the state information array. You can use thise value later as an argument to Hsetstate@ to restore that state.À

Œ
Go to the ‰ŠP�”›first@�”˜, ‰ŠP�”›previous@�”˜, ‰ŠP�”›next@�”˜, ‰ŠP�”›last@�”˜ section, ‰ŠP�”›table of contents@�”˜.