Blame manual/math.texi

Packit 6c4009
@c We need some definitions here.
Packit 6c4009
@ifclear mult
Packit 6c4009
@ifhtml
Packit 6c4009
@set mult ·
Packit 6c4009
@set infty ∞
Packit 6c4009
@set pie π
Packit 6c4009
@end ifhtml
Packit 6c4009
@iftex
Packit 6c4009
@set mult @cdot
Packit 6c4009
@set infty @infty
Packit 6c4009
@end iftex
Packit 6c4009
@ifclear mult
Packit 6c4009
@set mult *
Packit 6c4009
@set infty oo
Packit 6c4009
@set pie pi
Packit 6c4009
@end ifclear
Packit 6c4009
@macro mul
Packit 6c4009
@value{mult}
Packit 6c4009
@end macro
Packit 6c4009
@macro infinity
Packit 6c4009
@value{infty}
Packit 6c4009
@end macro
Packit 6c4009
@ifnottex
Packit 6c4009
@macro pi
Packit 6c4009
@value{pie}
Packit 6c4009
@end macro
Packit 6c4009
@end ifnottex
Packit 6c4009
@end ifclear
Packit 6c4009
Packit 6c4009
@node Mathematics, Arithmetic, Syslog, Top
Packit 6c4009
@c %MENU% Math functions, useful constants, random numbers
Packit 6c4009
@chapter Mathematics
Packit 6c4009
Packit 6c4009
This chapter contains information about functions for performing
Packit 6c4009
mathematical computations, such as trigonometric functions.  Most of
Packit 6c4009
these functions have prototypes declared in the header file
Packit 6c4009
@file{math.h}.  The complex-valued functions are defined in
Packit 6c4009
@file{complex.h}.
Packit 6c4009
@pindex math.h
Packit 6c4009
@pindex complex.h
Packit 6c4009
Packit 6c4009
All mathematical functions which take a floating-point argument
Packit 6c4009
have three variants, one each for @code{double}, @code{float}, and
Packit 6c4009
@code{long double} arguments.  The @code{double} versions are mostly
Packit 6c4009
defined in @w{ISO C89}.  The @code{float} and @code{long double}
Packit 6c4009
versions are from the numeric extensions to C included in @w{ISO C99}.
Packit 6c4009
Packit 6c4009
Which of the three versions of a function should be used depends on the
Packit 6c4009
situation.  For most calculations, the @code{float} functions are the
Packit 6c4009
fastest.  On the other hand, the @code{long double} functions have the
Packit 6c4009
highest precision.  @code{double} is somewhere in between.  It is
Packit 6c4009
usually wise to pick the narrowest type that can accommodate your data.
Packit 6c4009
Not all machines have a distinct @code{long double} type; it may be the
Packit 6c4009
same as @code{double}.
Packit 6c4009
Packit 6c4009
@Theglibc{} also provides @code{_Float@var{N}} and
Packit 6c4009
@code{_Float@var{N}x} types.  These types are defined in @w{ISO/IEC TS
Packit 6c4009
18661-3}, which extends @w{ISO C} and defines floating-point types that
Packit 6c4009
are not machine-dependent.  When such a type, such as @code{_Float128},
Packit 6c4009
is supported by @theglibc{}, extra variants for most of the mathematical
Packit 6c4009
functions provided for @code{double}, @code{float}, and @code{long
Packit 6c4009
double} are also provided for the supported type.  Throughout this
Packit 6c4009
manual, the @code{_Float@var{N}} and @code{_Float@var{N}x} variants of
Packit 6c4009
these functions are described along with the @code{double},
Packit 6c4009
@code{float}, and @code{long double} variants and they come from
Packit 6c4009
@w{ISO/IEC TS 18661-3}, unless explicitly stated otherwise.
Packit 6c4009
Packit 6c4009
Support for @code{_Float@var{N}} or @code{_Float@var{N}x} types is
Packit 6c4009
provided for @code{_Float32}, @code{_Float64} and @code{_Float32x} on
Packit 6c4009
all platforms.
Packit 6c4009
It is also provided for @code{_Float128} and @code{_Float64x} on
Packit 6c4009
powerpc64le (PowerPC 64-bits little-endian), x86_64, x86, ia64,
Packit 6c4009
aarch64, alpha, mips64, riscv, s390 and sparc.
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* Mathematical Constants::      Precise numeric values for often-used
Packit 6c4009
                                 constants.
Packit 6c4009
* Trig Functions::              Sine, cosine, tangent, and friends.
Packit 6c4009
* Inverse Trig Functions::      Arcsine, arccosine, etc.
Packit 6c4009
* Exponents and Logarithms::    Also pow and sqrt.
Packit 6c4009
* Hyperbolic Functions::        sinh, cosh, tanh, etc.
Packit 6c4009
* Special Functions::           Bessel, gamma, erf.
Packit 6c4009
* Errors in Math Functions::    Known Maximum Errors in Math Functions.
Packit 6c4009
* Pseudo-Random Numbers::       Functions for generating pseudo-random
Packit 6c4009
				 numbers.
Packit 6c4009
* FP Function Optimizations::   Fast code or small code.
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
@node Mathematical Constants
Packit 6c4009
@section Predefined Mathematical Constants
Packit 6c4009
@cindex constants
Packit 6c4009
@cindex mathematical constants
Packit 6c4009
Packit 6c4009
The header @file{math.h} defines several useful mathematical constants.
Packit 6c4009
All values are defined as preprocessor macros starting with @code{M_}.
Packit 6c4009
The values provided are:
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item M_E
Packit 6c4009
The base of natural logarithms.
Packit 6c4009
@item M_LOG2E
Packit 6c4009
The logarithm to base @code{2} of @code{M_E}.
Packit 6c4009
@item M_LOG10E
Packit 6c4009
The logarithm to base @code{10} of @code{M_E}.
Packit 6c4009
@item M_LN2
Packit 6c4009
The natural logarithm of @code{2}.
Packit 6c4009
@item M_LN10
Packit 6c4009
The natural logarithm of @code{10}.
Packit 6c4009
@item M_PI
Packit 6c4009
Pi, the ratio of a circle's circumference to its diameter.
Packit 6c4009
@item M_PI_2
Packit 6c4009
Pi divided by two.
Packit 6c4009
@item M_PI_4
Packit 6c4009
Pi divided by four.
Packit 6c4009
@item M_1_PI
Packit 6c4009
The reciprocal of pi (1/pi)
Packit 6c4009
@item M_2_PI
Packit 6c4009
Two times the reciprocal of pi.
Packit 6c4009
@item M_2_SQRTPI
Packit 6c4009
Two times the reciprocal of the square root of pi.
Packit 6c4009
@item M_SQRT2
Packit 6c4009
The square root of two.
Packit 6c4009
@item M_SQRT1_2
Packit 6c4009
The reciprocal of the square root of two (also the square root of 1/2).
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
These constants come from the Unix98 standard and were also available in
Packit 6c4009
4.4BSD; therefore they are only defined if
Packit 6c4009
@code{_XOPEN_SOURCE=500}, or a more general feature select macro, is
Packit 6c4009
defined.  The default set of features includes these constants.
Packit 6c4009
@xref{Feature Test Macros}.
Packit 6c4009
Packit 6c4009
All values are of type @code{double}.  As an extension, @theglibc{}
Packit 6c4009
also defines these constants with type @code{long double}.  The
Packit 6c4009
@code{long double} macros have a lowercase @samp{l} appended to their
Packit 6c4009
names: @code{M_El}, @code{M_PIl}, and so forth.  These are only
Packit 6c4009
available if @code{_GNU_SOURCE} is defined.
Packit 6c4009
Packit 6c4009
Likewise, @theglibc{} also defines these constants with the types
Packit 6c4009
@code{_Float@var{N}} and @code{_Float@var{N}x} for the machines that
Packit 6c4009
have support for such types enabled (@pxref{Mathematics}) and if
Packit 6c4009
@code{_GNU_SOURCE} is defined.  When available, the macros names are
Packit 6c4009
appended with @samp{f@var{N}} or @samp{f@var{N}x}, such as @samp{f128}
Packit 6c4009
for the type @code{_Float128}.
Packit 6c4009
Packit 6c4009
@vindex PI
Packit 6c4009
@emph{Note:} Some programs use a constant named @code{PI} which has the
Packit 6c4009
same value as @code{M_PI}.  This constant is not standard; it may have
Packit 6c4009
appeared in some old AT&T headers, and is mentioned in Stroustrup's book
Packit 6c4009
on C++.  It infringes on the user's name space, so @theglibc{}
Packit 6c4009
does not define it.  Fixing programs written to expect it is simple:
Packit 6c4009
replace @code{PI} with @code{M_PI} throughout, or put @samp{-DPI=M_PI}
Packit 6c4009
on the compiler command line.
Packit 6c4009
Packit 6c4009
@node Trig Functions
Packit 6c4009
@section Trigonometric Functions
Packit 6c4009
@cindex trigonometric functions
Packit 6c4009
Packit 6c4009
These are the familiar @code{sin}, @code{cos}, and @code{tan} functions.
Packit 6c4009
The arguments to all of these functions are in units of radians; recall
Packit 6c4009
that pi radians equals 180 degrees.
Packit 6c4009
Packit 6c4009
@cindex pi (trigonometric constant)
Packit 6c4009
The math library normally defines @code{M_PI} to a @code{double}
Packit 6c4009
approximation of pi.  If strict ISO and/or POSIX compliance
Packit 6c4009
are requested this constant is not defined, but you can easily define it
Packit 6c4009
yourself:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
#define M_PI 3.14159265358979323846264338327
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
You can also compute the value of pi with the expression @code{acos
Packit 6c4009
(-1.0)}.
Packit 6c4009
Packit 6c4009
@deftypefun double sin (double @var{x})
Packit 6c4009
@deftypefunx float sinf (float @var{x})
Packit 6c4009
@deftypefunx {long double} sinl (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN sinfN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx sinfNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@standardsx{sinfN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{sinfNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return the sine of @var{x}, where @var{x} is given in
Packit 6c4009
radians.  The return value is in the range @code{-1} to @code{1}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double cos (double @var{x})
Packit 6c4009
@deftypefunx float cosf (float @var{x})
Packit 6c4009
@deftypefunx {long double} cosl (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN cosfN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx cosfNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@standardsx{cosfN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{cosfNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return the cosine of @var{x}, where @var{x} is given in
Packit 6c4009
radians.  The return value is in the range @code{-1} to @code{1}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double tan (double @var{x})
Packit 6c4009
@deftypefunx float tanf (float @var{x})
Packit 6c4009
@deftypefunx {long double} tanl (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN tanfN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx tanfNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@standardsx{tanfN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{tanfNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return the tangent of @var{x}, where @var{x} is given in
Packit 6c4009
radians.
Packit 6c4009
Packit 6c4009
Mathematically, the tangent function has singularities at odd multiples
Packit 6c4009
of pi/2.  If the argument @var{x} is too close to one of these
Packit 6c4009
singularities, @code{tan} will signal overflow.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
In many applications where @code{sin} and @code{cos} are used, the sine
Packit 6c4009
and cosine of the same angle are needed at the same time.  It is more
Packit 6c4009
efficient to compute them simultaneously, so the library provides a
Packit 6c4009
function to do that.
Packit 6c4009
Packit 6c4009
@deftypefun void sincos (double @var{x}, double *@var{sinx}, double *@var{cosx})
Packit 6c4009
@deftypefunx void sincosf (float @var{x}, float *@var{sinx}, float *@var{cosx})
Packit 6c4009
@deftypefunx void sincosl (long double @var{x}, long double *@var{sinx}, long double *@var{cosx})
Packit 6c4009
@deftypefunx _FloatN sincosfN (_Float@var{N} @var{x}, _Float@var{N} *@var{sinx}, _Float@var{N} *@var{cosx})
Packit 6c4009
@deftypefunx _FloatNx sincosfNx (_Float@var{N}x @var{x}, _Float@var{N}x *@var{sinx}, _Float@var{N}x *@var{cosx})
Packit 6c4009
@standards{GNU, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return the sine of @var{x} in @code{*@var{sinx}} and the
Packit 6c4009
cosine of @var{x} in @code{*@var{cosx}}, where @var{x} is given in
Packit 6c4009
radians.  Both values, @code{*@var{sinx}} and @code{*@var{cosx}}, are in
Packit 6c4009
the range of @code{-1} to @code{1}.
Packit 6c4009
Packit 6c4009
All these functions, including the @code{_Float@var{N}} and
Packit 6c4009
@code{_Float@var{N}x} variants, are GNU extensions.  Portable programs
Packit 6c4009
should be prepared to cope with their absence.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@cindex complex trigonometric functions
Packit 6c4009
Packit 6c4009
@w{ISO C99} defines variants of the trig functions which work on
Packit 6c4009
complex numbers.  @Theglibc{} provides these functions, but they
Packit 6c4009
are only useful if your compiler supports the new complex types defined
Packit 6c4009
by the standard.
Packit 6c4009
@c XXX Change this when gcc is fixed. -zw
Packit 6c4009
(As of this writing GCC supports complex numbers, but there are bugs in
Packit 6c4009
the implementation.)
Packit 6c4009
Packit 6c4009
@deftypefun {complex double} csin (complex double @var{z})
Packit 6c4009
@deftypefunx {complex float} csinf (complex float @var{z})
Packit 6c4009
@deftypefunx {complex long double} csinl (complex long double @var{z})
Packit 6c4009
@deftypefunx {complex _FloatN} csinfN (complex _Float@var{N} @var{z})
Packit 6c4009
@deftypefunx {complex _FloatNx} csinfNx (complex _Float@var{N}x @var{z})
Packit 6c4009
@standards{ISO, complex.h}
Packit 6c4009
@standardsx{csinfN, TS 18661-3:2015, complex.h}
Packit 6c4009
@standardsx{csinfNx, TS 18661-3:2015, complex.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c There are calls to nan* that could trigger @mtslocale if they didn't get
Packit 6c4009
@c empty strings.
Packit 6c4009
These functions return the complex sine of @var{z}.
Packit 6c4009
The mathematical definition of the complex sine is
Packit 6c4009
Packit 6c4009
@ifnottex
Packit 6c4009
@math{sin (z) = 1/(2*i) * (exp (z*i) - exp (-z*i))}.
Packit 6c4009
@end ifnottex
Packit 6c4009
@tex
Packit 6c4009
$$\sin(z) = {1\over 2i} (e^{zi} - e^{-zi})$$
Packit 6c4009
@end tex
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {complex double} ccos (complex double @var{z})
Packit 6c4009
@deftypefunx {complex float} ccosf (complex float @var{z})
Packit 6c4009
@deftypefunx {complex long double} ccosl (complex long double @var{z})
Packit 6c4009
@deftypefunx {complex _FloatN} ccosfN (complex _Float@var{N} @var{z})
Packit 6c4009
@deftypefunx {complex _FloatNx} ccosfNx (complex _Float@var{N}x @var{z})
Packit 6c4009
@standards{ISO, complex.h}
Packit 6c4009
@standardsx{ccosfN, TS 18661-3:2015, complex.h}
Packit 6c4009
@standardsx{ccosfNx, TS 18661-3:2015, complex.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return the complex cosine of @var{z}.
Packit 6c4009
The mathematical definition of the complex cosine is
Packit 6c4009
Packit 6c4009
@ifnottex
Packit 6c4009
@math{cos (z) = 1/2 * (exp (z*i) + exp (-z*i))}
Packit 6c4009
@end ifnottex
Packit 6c4009
@tex
Packit 6c4009
$$\cos(z) = {1\over 2} (e^{zi} + e^{-zi})$$
Packit 6c4009
@end tex
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {complex double} ctan (complex double @var{z})
Packit 6c4009
@deftypefunx {complex float} ctanf (complex float @var{z})
Packit 6c4009
@deftypefunx {complex long double} ctanl (complex long double @var{z})
Packit 6c4009
@deftypefunx {complex _FloatN} ctanfN (complex _Float@var{N} @var{z})
Packit 6c4009
@deftypefunx {complex _FloatNx} ctanfNx (complex _Float@var{N}x @var{z})
Packit 6c4009
@standards{ISO, complex.h}
Packit 6c4009
@standardsx{ctanfN, TS 18661-3:2015, complex.h}
Packit 6c4009
@standardsx{ctanfNx, TS 18661-3:2015, complex.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return the complex tangent of @var{z}.
Packit 6c4009
The mathematical definition of the complex tangent is
Packit 6c4009
Packit 6c4009
@ifnottex
Packit 6c4009
@math{tan (z) = -i * (exp (z*i) - exp (-z*i)) / (exp (z*i) + exp (-z*i))}
Packit 6c4009
@end ifnottex
Packit 6c4009
@tex
Packit 6c4009
$$\tan(z) = -i \cdot {e^{zi} - e^{-zi}\over e^{zi} + e^{-zi}}$$
Packit 6c4009
@end tex
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
The complex tangent has poles at @math{pi/2 + 2n}, where @math{n} is an
Packit 6c4009
integer.  @code{ctan} may signal overflow if @var{z} is too close to a
Packit 6c4009
pole.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node Inverse Trig Functions
Packit 6c4009
@section Inverse Trigonometric Functions
Packit 6c4009
@cindex inverse trigonometric functions
Packit 6c4009
Packit 6c4009
These are the usual arcsine, arccosine and arctangent functions,
Packit 6c4009
which are the inverses of the sine, cosine and tangent functions
Packit 6c4009
respectively.
Packit 6c4009
Packit 6c4009
@deftypefun double asin (double @var{x})
Packit 6c4009
@deftypefunx float asinf (float @var{x})
Packit 6c4009
@deftypefunx {long double} asinl (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN asinfN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx asinfNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@standardsx{asinfN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{asinfNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions compute the arcsine of @var{x}---that is, the value whose
Packit 6c4009
sine is @var{x}.  The value is in units of radians.  Mathematically,
Packit 6c4009
there are infinitely many such values; the one actually returned is the
Packit 6c4009
one between @code{-pi/2} and @code{pi/2} (inclusive).
Packit 6c4009
Packit 6c4009
The arcsine function is defined mathematically only
Packit 6c4009
over the domain @code{-1} to @code{1}.  If @var{x} is outside the
Packit 6c4009
domain, @code{asin} signals a domain error.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double acos (double @var{x})
Packit 6c4009
@deftypefunx float acosf (float @var{x})
Packit 6c4009
@deftypefunx {long double} acosl (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN acosfN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx acosfNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@standardsx{acosfN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{acosfNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions compute the arccosine of @var{x}---that is, the value
Packit 6c4009
whose cosine is @var{x}.  The value is in units of radians.
Packit 6c4009
Mathematically, there are infinitely many such values; the one actually
Packit 6c4009
returned is the one between @code{0} and @code{pi} (inclusive).
Packit 6c4009
Packit 6c4009
The arccosine function is defined mathematically only
Packit 6c4009
over the domain @code{-1} to @code{1}.  If @var{x} is outside the
Packit 6c4009
domain, @code{acos} signals a domain error.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double atan (double @var{x})
Packit 6c4009
@deftypefunx float atanf (float @var{x})
Packit 6c4009
@deftypefunx {long double} atanl (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN atanfN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx atanfNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@standardsx{atanfN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{atanfNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions compute the arctangent of @var{x}---that is, the value
Packit 6c4009
whose tangent is @var{x}.  The value is in units of radians.
Packit 6c4009
Mathematically, there are infinitely many such values; the one actually
Packit 6c4009
returned is the one between @code{-pi/2} and @code{pi/2} (inclusive).
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double atan2 (double @var{y}, double @var{x})
Packit 6c4009
@deftypefunx float atan2f (float @var{y}, float @var{x})
Packit 6c4009
@deftypefunx {long double} atan2l (long double @var{y}, long double @var{x})
Packit 6c4009
@deftypefunx _FloatN atan2fN (_Float@var{N} @var{y}, _Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx atan2fNx (_Float@var{N}x @var{y}, _Float@var{N}x @var{x})
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@standardsx{atan2fN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{atan2fNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This function computes the arctangent of @var{y}/@var{x}, but the signs
Packit 6c4009
of both arguments are used to determine the quadrant of the result, and
Packit 6c4009
@var{x} is permitted to be zero.  The return value is given in radians
Packit 6c4009
and is in the range @code{-pi} to @code{pi}, inclusive.
Packit 6c4009
Packit 6c4009
If @var{x} and @var{y} are coordinates of a point in the plane,
Packit 6c4009
@code{atan2} returns the signed angle between the line from the origin
Packit 6c4009
to that point and the x-axis.  Thus, @code{atan2} is useful for
Packit 6c4009
converting Cartesian coordinates to polar coordinates.  (To compute the
Packit 6c4009
radial coordinate, use @code{hypot}; see @ref{Exponents and
Packit 6c4009
Logarithms}.)
Packit 6c4009
Packit 6c4009
@c This is experimentally true.  Should it be so? -zw
Packit 6c4009
If both @var{x} and @var{y} are zero, @code{atan2} returns zero.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@cindex inverse complex trigonometric functions
Packit 6c4009
@w{ISO C99} defines complex versions of the inverse trig functions.
Packit 6c4009
Packit 6c4009
@deftypefun {complex double} casin (complex double @var{z})
Packit 6c4009
@deftypefunx {complex float} casinf (complex float @var{z})
Packit 6c4009
@deftypefunx {complex long double} casinl (complex long double @var{z})
Packit 6c4009
@deftypefunx {complex _FloatN} casinfN (complex _Float@var{N} @var{z})
Packit 6c4009
@deftypefunx {complex _FloatNx} casinfNx (complex _Float@var{N}x @var{z})
Packit 6c4009
@standards{ISO, complex.h}
Packit 6c4009
@standardsx{casinfN, TS 18661-3:2015, complex.h}
Packit 6c4009
@standardsx{casinfNx, TS 18661-3:2015, complex.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions compute the complex arcsine of @var{z}---that is, the
Packit 6c4009
value whose sine is @var{z}.  The value returned is in radians.
Packit 6c4009
Packit 6c4009
Unlike the real-valued functions, @code{casin} is defined for all
Packit 6c4009
values of @var{z}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {complex double} cacos (complex double @var{z})
Packit 6c4009
@deftypefunx {complex float} cacosf (complex float @var{z})
Packit 6c4009
@deftypefunx {complex long double} cacosl (complex long double @var{z})
Packit 6c4009
@deftypefunx {complex _FloatN} cacosfN (complex _Float@var{N} @var{z})
Packit 6c4009
@deftypefunx {complex _FloatNx} cacosfNx (complex _Float@var{N}x @var{z})
Packit 6c4009
@standards{ISO, complex.h}
Packit 6c4009
@standardsx{cacosfN, TS 18661-3:2015, complex.h}
Packit 6c4009
@standardsx{cacosfNx, TS 18661-3:2015, complex.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions compute the complex arccosine of @var{z}---that is, the
Packit 6c4009
value whose cosine is @var{z}.  The value returned is in radians.
Packit 6c4009
Packit 6c4009
Unlike the real-valued functions, @code{cacos} is defined for all
Packit 6c4009
values of @var{z}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@deftypefun {complex double} catan (complex double @var{z})
Packit 6c4009
@deftypefunx {complex float} catanf (complex float @var{z})
Packit 6c4009
@deftypefunx {complex long double} catanl (complex long double @var{z})
Packit 6c4009
@deftypefunx {complex _FloatN} catanfN (complex _Float@var{N} @var{z})
Packit 6c4009
@deftypefunx {complex _FloatNx} catanfNx (complex _Float@var{N}x @var{z})
Packit 6c4009
@standards{ISO, complex.h}
Packit 6c4009
@standardsx{catanfN, TS 18661-3:2015, complex.h}
Packit 6c4009
@standardsx{catanfNx, TS 18661-3:2015, complex.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions compute the complex arctangent of @var{z}---that is,
Packit 6c4009
the value whose tangent is @var{z}.  The value is in units of radians.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node Exponents and Logarithms
Packit 6c4009
@section Exponentiation and Logarithms
Packit 6c4009
@cindex exponentiation functions
Packit 6c4009
@cindex power functions
Packit 6c4009
@cindex logarithm functions
Packit 6c4009
Packit 6c4009
@deftypefun double exp (double @var{x})
Packit 6c4009
@deftypefunx float expf (float @var{x})
Packit 6c4009
@deftypefunx {long double} expl (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN expfN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx expfNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@standardsx{expfN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{expfNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions compute @code{e} (the base of natural logarithms) raised
Packit 6c4009
to the power @var{x}.
Packit 6c4009
Packit 6c4009
If the magnitude of the result is too large to be representable,
Packit 6c4009
@code{exp} signals overflow.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double exp2 (double @var{x})
Packit 6c4009
@deftypefunx float exp2f (float @var{x})
Packit 6c4009
@deftypefunx {long double} exp2l (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN exp2fN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx exp2fNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@standardsx{exp2fN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{exp2fNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions compute @code{2} raised to the power @var{x}.
Packit 6c4009
Mathematically, @code{exp2 (x)} is the same as @code{exp (x * log (2))}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double exp10 (double @var{x})
Packit 6c4009
@deftypefunx float exp10f (float @var{x})
Packit 6c4009
@deftypefunx {long double} exp10l (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN exp10fN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx exp10fNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@standardsx{exp10fN, TS 18661-4:2015, math.h}
Packit 6c4009
@standardsx{exp10fNx, TS 18661-4:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions compute @code{10} raised to the power @var{x}.
Packit 6c4009
Mathematically, @code{exp10 (x)} is the same as @code{exp (x * log (10))}.
Packit 6c4009
Packit 6c4009
The @code{exp10} functions are from TS 18661-4:2015.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@deftypefun double log (double @var{x})
Packit 6c4009
@deftypefunx float logf (float @var{x})
Packit 6c4009
@deftypefunx {long double} logl (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN logfN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx logfNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@standardsx{logfN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{logfNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions compute the natural logarithm of @var{x}.  @code{exp (log
Packit 6c4009
(@var{x}))} equals @var{x}, exactly in mathematics and approximately in
Packit 6c4009
C.
Packit 6c4009
Packit 6c4009
If @var{x} is negative, @code{log} signals a domain error.  If @var{x}
Packit 6c4009
is zero, it returns negative infinity; if @var{x} is too close to zero,
Packit 6c4009
it may signal overflow.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double log10 (double @var{x})
Packit 6c4009
@deftypefunx float log10f (float @var{x})
Packit 6c4009
@deftypefunx {long double} log10l (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN log10fN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx log10fNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@standardsx{log10fN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{log10fNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return the base-10 logarithm of @var{x}.
Packit 6c4009
@code{log10 (@var{x})} equals @code{log (@var{x}) / log (10)}.
Packit 6c4009
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double log2 (double @var{x})
Packit 6c4009
@deftypefunx float log2f (float @var{x})
Packit 6c4009
@deftypefunx {long double} log2l (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN log2fN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx log2fNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@standardsx{log2fN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{log2fNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return the base-2 logarithm of @var{x}.
Packit 6c4009
@code{log2 (@var{x})} equals @code{log (@var{x}) / log (2)}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double logb (double @var{x})
Packit 6c4009
@deftypefunx float logbf (float @var{x})
Packit 6c4009
@deftypefunx {long double} logbl (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN logbfN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx logbfNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@standardsx{logbfN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{logbfNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions extract the exponent of @var{x} and return it as a
Packit 6c4009
floating-point value.  If @code{FLT_RADIX} is two, @code{logb} is equal
Packit 6c4009
to @code{floor (log2 (x))}, except it's probably faster.
Packit 6c4009
Packit 6c4009
If @var{x} is de-normalized, @code{logb} returns the exponent @var{x}
Packit 6c4009
would have if it were normalized.  If @var{x} is infinity (positive or
Packit 6c4009
negative), @code{logb} returns @math{@infinity{}}.  If @var{x} is zero,
Packit 6c4009
@code{logb} returns @math{@infinity{}}.  It does not signal.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int ilogb (double @var{x})
Packit 6c4009
@deftypefunx int ilogbf (float @var{x})
Packit 6c4009
@deftypefunx int ilogbl (long double @var{x})
Packit 6c4009
@deftypefunx int ilogbfN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx int ilogbfNx (_Float@var{N}x @var{x})
Packit 6c4009
@deftypefunx {long int} llogb (double @var{x})
Packit 6c4009
@deftypefunx {long int} llogbf (float @var{x})
Packit 6c4009
@deftypefunx {long int} llogbl (long double @var{x})
Packit 6c4009
@deftypefunx {long int} llogbfN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx {long int} llogbfNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@standardsx{ilogbfN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{ilogbfNx, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{llogbfN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{llogbfNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions are equivalent to the corresponding @code{logb}
Packit 6c4009
functions except that they return signed integer values.  The
Packit 6c4009
@code{ilogb}, @code{ilogbf}, and @code{ilogbl} functions are from ISO
Packit 6c4009
C99; the @code{llogb}, @code{llogbf}, @code{llogbl} functions are from
Packit 6c4009
TS 18661-1:2014; the @code{ilogbfN}, @code{ilogbfNx}, @code{llogbfN},
Packit 6c4009
and @code{llogbfNx} functions are from TS 18661-3:2015.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
Since integers cannot represent infinity and NaN, @code{ilogb} instead
Packit 6c4009
returns an integer that can't be the exponent of a normal floating-point
Packit 6c4009
number.  @file{math.h} defines constants so you can check for this.
Packit 6c4009
Packit 6c4009
@deftypevr Macro int FP_ILOGB0
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@code{ilogb} returns this value if its argument is @code{0}.  The
Packit 6c4009
numeric value is either @code{INT_MIN} or @code{-INT_MAX}.
Packit 6c4009
Packit 6c4009
This macro is defined in @w{ISO C99}.
Packit 6c4009
@end deftypevr
Packit 6c4009
Packit 6c4009
@deftypevr Macro {long int} FP_LLOGB0
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@code{llogb} returns this value if its argument is @code{0}.  The
Packit 6c4009
numeric value is either @code{LONG_MIN} or @code{-LONG_MAX}.
Packit 6c4009
Packit 6c4009
This macro is defined in TS 18661-1:2014.
Packit 6c4009
@end deftypevr
Packit 6c4009
Packit 6c4009
@deftypevr Macro int FP_ILOGBNAN
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@code{ilogb} returns this value if its argument is @code{NaN}.  The
Packit 6c4009
numeric value is either @code{INT_MIN} or @code{INT_MAX}.
Packit 6c4009
Packit 6c4009
This macro is defined in @w{ISO C99}.
Packit 6c4009
@end deftypevr
Packit 6c4009
Packit 6c4009
@deftypevr Macro {long int} FP_LLOGBNAN
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@code{llogb} returns this value if its argument is @code{NaN}.  The
Packit 6c4009
numeric value is either @code{LONG_MIN} or @code{LONG_MAX}.
Packit 6c4009
Packit 6c4009
This macro is defined in TS 18661-1:2014.
Packit 6c4009
@end deftypevr
Packit 6c4009
Packit 6c4009
These values are system specific.  They might even be the same.  The
Packit 6c4009
proper way to test the result of @code{ilogb} is as follows:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
i = ilogb (f);
Packit 6c4009
if (i == FP_ILOGB0 || i == FP_ILOGBNAN)
Packit 6c4009
  @{
Packit 6c4009
    if (isnan (f))
Packit 6c4009
      @{
Packit 6c4009
        /* @r{Handle NaN.}  */
Packit 6c4009
      @}
Packit 6c4009
    else if (f  == 0.0)
Packit 6c4009
      @{
Packit 6c4009
        /* @r{Handle 0.0.}  */
Packit 6c4009
      @}
Packit 6c4009
    else
Packit 6c4009
      @{
Packit 6c4009
        /* @r{Some other value with large exponent,}
Packit 6c4009
           @r{perhaps +Inf.}  */
Packit 6c4009
      @}
Packit 6c4009
  @}
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@deftypefun double pow (double @var{base}, double @var{power})
Packit 6c4009
@deftypefunx float powf (float @var{base}, float @var{power})
Packit 6c4009
@deftypefunx {long double} powl (long double @var{base}, long double @var{power})
Packit 6c4009
@deftypefunx _FloatN powfN (_Float@var{N} @var{base}, _Float@var{N} @var{power})
Packit 6c4009
@deftypefunx _FloatNx powfNx (_Float@var{N}x @var{base}, _Float@var{N}x @var{power})
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@standardsx{powfN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{powfNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These are general exponentiation functions, returning @var{base} raised
Packit 6c4009
to @var{power}.
Packit 6c4009
Packit 6c4009
Mathematically, @code{pow} would return a complex number when @var{base}
Packit 6c4009
is negative and @var{power} is not an integral value.  @code{pow} can't
Packit 6c4009
do that, so instead it signals a domain error. @code{pow} may also
Packit 6c4009
underflow or overflow the destination type.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@cindex square root function
Packit 6c4009
@deftypefun double sqrt (double @var{x})
Packit 6c4009
@deftypefunx float sqrtf (float @var{x})
Packit 6c4009
@deftypefunx {long double} sqrtl (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN sqrtfN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx sqrtfNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@standardsx{sqrtfN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{sqrtfNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return the nonnegative square root of @var{x}.
Packit 6c4009
Packit 6c4009
If @var{x} is negative, @code{sqrt} signals a domain error.
Packit 6c4009
Mathematically, it should return a complex number.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@cindex cube root function
Packit 6c4009
@deftypefun double cbrt (double @var{x})
Packit 6c4009
@deftypefunx float cbrtf (float @var{x})
Packit 6c4009
@deftypefunx {long double} cbrtl (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN cbrtfN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx cbrtfNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{BSD, math.h}
Packit 6c4009
@standardsx{cbrtfN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{cbrtfNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return the cube root of @var{x}.  They cannot
Packit 6c4009
fail; every representable real value has a representable real cube root.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double hypot (double @var{x}, double @var{y})
Packit 6c4009
@deftypefunx float hypotf (float @var{x}, float @var{y})
Packit 6c4009
@deftypefunx {long double} hypotl (long double @var{x}, long double @var{y})
Packit 6c4009
@deftypefunx _FloatN hypotfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
Packit 6c4009
@deftypefunx _FloatNx hypotfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@standardsx{hypotfN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{hypotfNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return @code{sqrt (@var{x}*@var{x} +
Packit 6c4009
@var{y}*@var{y})}.  This is the length of the hypotenuse of a right
Packit 6c4009
triangle with sides of length @var{x} and @var{y}, or the distance
Packit 6c4009
of the point (@var{x}, @var{y}) from the origin.  Using this function
Packit 6c4009
instead of the direct formula is wise, since the error is
Packit 6c4009
much smaller.  See also the function @code{cabs} in @ref{Absolute Value}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double expm1 (double @var{x})
Packit 6c4009
@deftypefunx float expm1f (float @var{x})
Packit 6c4009
@deftypefunx {long double} expm1l (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN expm1fN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx expm1fNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@standardsx{expm1fN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{expm1fNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return a value equivalent to @code{exp (@var{x}) - 1}.
Packit 6c4009
They are computed in a way that is accurate even if @var{x} is
Packit 6c4009
near zero---a case where @code{exp (@var{x}) - 1} would be inaccurate owing
Packit 6c4009
to subtraction of two numbers that are nearly equal.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double log1p (double @var{x})
Packit 6c4009
@deftypefunx float log1pf (float @var{x})
Packit 6c4009
@deftypefunx {long double} log1pl (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN log1pfN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx log1pfNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@standardsx{log1pfN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{log1pfNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return a value equivalent to @w{@code{log (1 + @var{x})}}.
Packit 6c4009
They are computed in a way that is accurate even if @var{x} is
Packit 6c4009
near zero.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@cindex complex exponentiation functions
Packit 6c4009
@cindex complex logarithm functions
Packit 6c4009
Packit 6c4009
@w{ISO C99} defines complex variants of some of the exponentiation and
Packit 6c4009
logarithm functions.
Packit 6c4009
Packit 6c4009
@deftypefun {complex double} cexp (complex double @var{z})
Packit 6c4009
@deftypefunx {complex float} cexpf (complex float @var{z})
Packit 6c4009
@deftypefunx {complex long double} cexpl (complex long double @var{z})
Packit 6c4009
@deftypefunx {complex _FloatN} cexpfN (complex _Float@var{N} @var{z})
Packit 6c4009
@deftypefunx {complex _FloatNx} cexpfNx (complex _Float@var{N}x @var{z})
Packit 6c4009
@standards{ISO, complex.h}
Packit 6c4009
@standardsx{cexpfN, TS 18661-3:2015, complex.h}
Packit 6c4009
@standardsx{cexpfNx, TS 18661-3:2015, complex.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return @code{e} (the base of natural
Packit 6c4009
logarithms) raised to the power of @var{z}.
Packit 6c4009
Mathematically, this corresponds to the value
Packit 6c4009
Packit 6c4009
@ifnottex
Packit 6c4009
@math{exp (z) = exp (creal (z)) * (cos (cimag (z)) + I * sin (cimag (z)))}
Packit 6c4009
@end ifnottex
Packit 6c4009
@tex
Packit 6c4009
$$\exp(z) = e^z = e^{{\rm Re}\,z} (\cos ({\rm Im}\,z) + i \sin ({\rm Im}\,z))$$
Packit 6c4009
@end tex
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {complex double} clog (complex double @var{z})
Packit 6c4009
@deftypefunx {complex float} clogf (complex float @var{z})
Packit 6c4009
@deftypefunx {complex long double} clogl (complex long double @var{z})
Packit 6c4009
@deftypefunx {complex _FloatN} clogfN (complex _Float@var{N} @var{z})
Packit 6c4009
@deftypefunx {complex _FloatNx} clogfNx (complex _Float@var{N}x @var{z})
Packit 6c4009
@standards{ISO, complex.h}
Packit 6c4009
@standardsx{clogfN, TS 18661-3:2015, complex.h}
Packit 6c4009
@standardsx{clogfNx, TS 18661-3:2015, complex.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return the natural logarithm of @var{z}.
Packit 6c4009
Mathematically, this corresponds to the value
Packit 6c4009
Packit 6c4009
@ifnottex
Packit 6c4009
@math{log (z) = log (cabs (z)) + I * carg (z)}
Packit 6c4009
@end ifnottex
Packit 6c4009
@tex
Packit 6c4009
$$\log(z) = \log |z| + i \arg z$$
Packit 6c4009
@end tex
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
@code{clog} has a pole at 0, and will signal overflow if @var{z} equals
Packit 6c4009
or is very close to 0.  It is well-defined for all other values of
Packit 6c4009
@var{z}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@deftypefun {complex double} clog10 (complex double @var{z})
Packit 6c4009
@deftypefunx {complex float} clog10f (complex float @var{z})
Packit 6c4009
@deftypefunx {complex long double} clog10l (complex long double @var{z})
Packit 6c4009
@deftypefunx {complex _FloatN} clog10fN (complex _Float@var{N} @var{z})
Packit 6c4009
@deftypefunx {complex _FloatNx} clog10fNx (complex _Float@var{N}x @var{z})
Packit 6c4009
@standards{GNU, complex.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return the base 10 logarithm of the complex value
Packit 6c4009
@var{z}.  Mathematically, this corresponds to the value
Packit 6c4009
Packit 6c4009
@ifnottex
Packit 6c4009
@math{log10 (z) = log10 (cabs (z)) + I * carg (z) / log (10)}
Packit 6c4009
@end ifnottex
Packit 6c4009
@tex
Packit 6c4009
$$\log_{10}(z) = \log_{10}|z| + i \arg z / \log (10)$$
Packit 6c4009
@end tex
Packit 6c4009
Packit 6c4009
All these functions, including the @code{_Float@var{N}} and
Packit 6c4009
@code{_Float@var{N}x} variants, are GNU extensions.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {complex double} csqrt (complex double @var{z})
Packit 6c4009
@deftypefunx {complex float} csqrtf (complex float @var{z})
Packit 6c4009
@deftypefunx {complex long double} csqrtl (complex long double @var{z})
Packit 6c4009
@deftypefunx {complex _FloatN} csqrtfN (_Float@var{N} @var{z})
Packit 6c4009
@deftypefunx {complex _FloatNx} csqrtfNx (complex _Float@var{N}x @var{z})
Packit 6c4009
@standards{ISO, complex.h}
Packit 6c4009
@standardsx{csqrtfN, TS 18661-3:2015, complex.h}
Packit 6c4009
@standardsx{csqrtfNx, TS 18661-3:2015, complex.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return the complex square root of the argument @var{z}.  Unlike
Packit 6c4009
the real-valued functions, they are defined for all values of @var{z}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {complex double} cpow (complex double @var{base}, complex double @var{power})
Packit 6c4009
@deftypefunx {complex float} cpowf (complex float @var{base}, complex float @var{power})
Packit 6c4009
@deftypefunx {complex long double} cpowl (complex long double @var{base}, complex long double @var{power})
Packit 6c4009
@deftypefunx {complex _FloatN} cpowfN (complex _Float@var{N} @var{base}, complex _Float@var{N} @var{power})
Packit 6c4009
@deftypefunx {complex _FloatNx} cpowfNx (complex _Float@var{N}x @var{base}, complex _Float@var{N}x @var{power})
Packit 6c4009
@standards{ISO, complex.h}
Packit 6c4009
@standardsx{cpowfN, TS 18661-3:2015, complex.h}
Packit 6c4009
@standardsx{cpowfNx, TS 18661-3:2015, complex.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return @var{base} raised to the power of
Packit 6c4009
@var{power}.  This is equivalent to @w{@code{cexp (y * clog (x))}}
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@node Hyperbolic Functions
Packit 6c4009
@section Hyperbolic Functions
Packit 6c4009
@cindex hyperbolic functions
Packit 6c4009
Packit 6c4009
The functions in this section are related to the exponential functions;
Packit 6c4009
see @ref{Exponents and Logarithms}.
Packit 6c4009
Packit 6c4009
@deftypefun double sinh (double @var{x})
Packit 6c4009
@deftypefunx float sinhf (float @var{x})
Packit 6c4009
@deftypefunx {long double} sinhl (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN sinhfN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx sinhfNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@standardsx{sinhfN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{sinhfNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return the hyperbolic sine of @var{x}, defined
Packit 6c4009
mathematically as @w{@code{(exp (@var{x}) - exp (-@var{x})) / 2}}.  They
Packit 6c4009
may signal overflow if @var{x} is too large.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double cosh (double @var{x})
Packit 6c4009
@deftypefunx float coshf (float @var{x})
Packit 6c4009
@deftypefunx {long double} coshl (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN coshfN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx coshfNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@standardsx{coshfN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{coshfNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return the hyperbolic cosine of @var{x},
Packit 6c4009
defined mathematically as @w{@code{(exp (@var{x}) + exp (-@var{x})) / 2}}.
Packit 6c4009
They may signal overflow if @var{x} is too large.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double tanh (double @var{x})
Packit 6c4009
@deftypefunx float tanhf (float @var{x})
Packit 6c4009
@deftypefunx {long double} tanhl (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN tanhfN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx tanhfNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@standardsx{tanhfN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{tanhfNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return the hyperbolic tangent of @var{x},
Packit 6c4009
defined mathematically as @w{@code{sinh (@var{x}) / cosh (@var{x})}}.
Packit 6c4009
They may signal overflow if @var{x} is too large.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@cindex hyperbolic functions
Packit 6c4009
Packit 6c4009
There are counterparts for the hyperbolic functions which take
Packit 6c4009
complex arguments.
Packit 6c4009
Packit 6c4009
@deftypefun {complex double} csinh (complex double @var{z})
Packit 6c4009
@deftypefunx {complex float} csinhf (complex float @var{z})
Packit 6c4009
@deftypefunx {complex long double} csinhl (complex long double @var{z})
Packit 6c4009
@deftypefunx {complex _FloatN} csinhfN (complex _Float@var{N} @var{z})
Packit 6c4009
@deftypefunx {complex _FloatNx} csinhfNx (complex _Float@var{N}x @var{z})
Packit 6c4009
@standards{ISO, complex.h}
Packit 6c4009
@standardsx{csinhfN, TS 18661-3:2015, complex.h}
Packit 6c4009
@standardsx{csinhfNx, TS 18661-3:2015, complex.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return the complex hyperbolic sine of @var{z}, defined
Packit 6c4009
mathematically as @w{@code{(exp (@var{z}) - exp (-@var{z})) / 2}}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {complex double} ccosh (complex double @var{z})
Packit 6c4009
@deftypefunx {complex float} ccoshf (complex float @var{z})
Packit 6c4009
@deftypefunx {complex long double} ccoshl (complex long double @var{z})
Packit 6c4009
@deftypefunx {complex _FloatN} ccoshfN (complex _Float@var{N} @var{z})
Packit 6c4009
@deftypefunx {complex _FloatNx} ccoshfNx (complex _Float@var{N}x @var{z})
Packit 6c4009
@standards{ISO, complex.h}
Packit 6c4009
@standardsx{ccoshfN, TS 18661-3:2015, complex.h}
Packit 6c4009
@standardsx{ccoshfNx, TS 18661-3:2015, complex.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return the complex hyperbolic cosine of @var{z}, defined
Packit 6c4009
mathematically as @w{@code{(exp (@var{z}) + exp (-@var{z})) / 2}}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {complex double} ctanh (complex double @var{z})
Packit 6c4009
@deftypefunx {complex float} ctanhf (complex float @var{z})
Packit 6c4009
@deftypefunx {complex long double} ctanhl (complex long double @var{z})
Packit 6c4009
@deftypefunx {complex _FloatN} ctanhfN (complex _Float@var{N} @var{z})
Packit 6c4009
@deftypefunx {complex _FloatNx} ctanhfNx (complex _Float@var{N}x @var{z})
Packit 6c4009
@standards{ISO, complex.h}
Packit 6c4009
@standardsx{ctanhfN, TS 18661-3:2015, complex.h}
Packit 6c4009
@standardsx{ctanhfNx, TS 18661-3:2015, complex.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return the complex hyperbolic tangent of @var{z},
Packit 6c4009
defined mathematically as @w{@code{csinh (@var{z}) / ccosh (@var{z})}}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@cindex inverse hyperbolic functions
Packit 6c4009
Packit 6c4009
@deftypefun double asinh (double @var{x})
Packit 6c4009
@deftypefunx float asinhf (float @var{x})
Packit 6c4009
@deftypefunx {long double} asinhl (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN asinhfN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx asinhfNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@standardsx{asinhfN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{asinhfNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return the inverse hyperbolic sine of @var{x}---the
Packit 6c4009
value whose hyperbolic sine is @var{x}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double acosh (double @var{x})
Packit 6c4009
@deftypefunx float acoshf (float @var{x})
Packit 6c4009
@deftypefunx {long double} acoshl (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN acoshfN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx acoshfNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@standardsx{acoshfN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{acoshfNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return the inverse hyperbolic cosine of @var{x}---the
Packit 6c4009
value whose hyperbolic cosine is @var{x}.  If @var{x} is less than
Packit 6c4009
@code{1}, @code{acosh} signals a domain error.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double atanh (double @var{x})
Packit 6c4009
@deftypefunx float atanhf (float @var{x})
Packit 6c4009
@deftypefunx {long double} atanhl (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN atanhfN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx atanhfNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{ISO, math.h}
Packit 6c4009
@standardsx{atanhfN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{atanhfNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return the inverse hyperbolic tangent of @var{x}---the
Packit 6c4009
value whose hyperbolic tangent is @var{x}.  If the absolute value of
Packit 6c4009
@var{x} is greater than @code{1}, @code{atanh} signals a domain error;
Packit 6c4009
if it is equal to 1, @code{atanh} returns infinity.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@cindex inverse complex hyperbolic functions
Packit 6c4009
Packit 6c4009
@deftypefun {complex double} casinh (complex double @var{z})
Packit 6c4009
@deftypefunx {complex float} casinhf (complex float @var{z})
Packit 6c4009
@deftypefunx {complex long double} casinhl (complex long double @var{z})
Packit 6c4009
@deftypefunx {complex _FloatN} casinhfN (complex _Float@var{N} @var{z})
Packit 6c4009
@deftypefunx {complex _FloatNx} casinhfNx (complex _Float@var{N}x @var{z})
Packit 6c4009
@standards{ISO, complex.h}
Packit 6c4009
@standardsx{casinhfN, TS 18661-3:2015, complex.h}
Packit 6c4009
@standardsx{casinhfNx, TS 18661-3:2015, complex.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return the inverse complex hyperbolic sine of
Packit 6c4009
@var{z}---the value whose complex hyperbolic sine is @var{z}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {complex double} cacosh (complex double @var{z})
Packit 6c4009
@deftypefunx {complex float} cacoshf (complex float @var{z})
Packit 6c4009
@deftypefunx {complex long double} cacoshl (complex long double @var{z})
Packit 6c4009
@deftypefunx {complex _FloatN} cacoshfN (complex _Float@var{N} @var{z})
Packit 6c4009
@deftypefunx {complex _FloatNx} cacoshfNx (complex _Float@var{N}x @var{z})
Packit 6c4009
@standards{ISO, complex.h}
Packit 6c4009
@standardsx{cacoshfN, TS 18661-3:2015, complex.h}
Packit 6c4009
@standardsx{cacoshfNx, TS 18661-3:2015, complex.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return the inverse complex hyperbolic cosine of
Packit 6c4009
@var{z}---the value whose complex hyperbolic cosine is @var{z}.  Unlike
Packit 6c4009
the real-valued functions, there are no restrictions on the value of @var{z}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {complex double} catanh (complex double @var{z})
Packit 6c4009
@deftypefunx {complex float} catanhf (complex float @var{z})
Packit 6c4009
@deftypefunx {complex long double} catanhl (complex long double @var{z})
Packit 6c4009
@deftypefunx {complex _FloatN} catanhfN (complex _Float@var{N} @var{z})
Packit 6c4009
@deftypefunx {complex _FloatNx} catanhfNx (complex _Float@var{N}x @var{z})
Packit 6c4009
@standards{ISO, complex.h}
Packit 6c4009
@standardsx{catanhfN, TS 18661-3:2015, complex.h}
Packit 6c4009
@standardsx{catanhfNx, TS 18661-3:2015, complex.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
These functions return the inverse complex hyperbolic tangent of
Packit 6c4009
@var{z}---the value whose complex hyperbolic tangent is @var{z}.  Unlike
Packit 6c4009
the real-valued functions, there are no restrictions on the value of
Packit 6c4009
@var{z}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@node Special Functions
Packit 6c4009
@section Special Functions
Packit 6c4009
@cindex special functions
Packit 6c4009
@cindex Bessel functions
Packit 6c4009
@cindex gamma function
Packit 6c4009
Packit 6c4009
These are some more exotic mathematical functions which are sometimes
Packit 6c4009
useful.  Currently they only have real-valued versions.
Packit 6c4009
Packit 6c4009
@deftypefun double erf (double @var{x})
Packit 6c4009
@deftypefunx float erff (float @var{x})
Packit 6c4009
@deftypefunx {long double} erfl (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN erffN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx erffNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{SVID, math.h}
Packit 6c4009
@standardsx{erffN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{erffNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{erf} returns the error function of @var{x}.  The error
Packit 6c4009
function is defined as
Packit 6c4009
@tex
Packit 6c4009
$$\hbox{erf}(x) = {2\over\sqrt{\pi}}\cdot\int_0^x e^{-t^2} \hbox{d}t$$
Packit 6c4009
@end tex
Packit 6c4009
@ifnottex
Packit 6c4009
@smallexample
Packit 6c4009
erf (x) = 2/sqrt(pi) * integral from 0 to x of exp(-t^2) dt
Packit 6c4009
@end smallexample
Packit 6c4009
@end ifnottex
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double erfc (double @var{x})
Packit 6c4009
@deftypefunx float erfcf (float @var{x})
Packit 6c4009
@deftypefunx {long double} erfcl (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN erfcfN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx erfcfNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{SVID, math.h}
Packit 6c4009
@standardsx{erfcfN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{erfcfNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{erfc} returns @code{1.0 - erf(@var{x})}, but computed in a
Packit 6c4009
fashion that avoids round-off error when @var{x} is large.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double lgamma (double @var{x})
Packit 6c4009
@deftypefunx float lgammaf (float @var{x})
Packit 6c4009
@deftypefunx {long double} lgammal (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN lgammafN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx lgammafNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{SVID, math.h}
Packit 6c4009
@standardsx{lgammafN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{lgammafNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtunsafe{@mtasurace{:signgam}}@asunsafe{}@acsafe{}}
Packit 6c4009
@code{lgamma} returns the natural logarithm of the absolute value of
Packit 6c4009
the gamma function of @var{x}.  The gamma function is defined as
Packit 6c4009
@tex
Packit 6c4009
$$\Gamma(x) = \int_0^\infty t^{x-1} e^{-t} \hbox{d}t$$
Packit 6c4009
@end tex
Packit 6c4009
@ifnottex
Packit 6c4009
@smallexample
Packit 6c4009
gamma (x) = integral from 0 to @infinity{} of t^(x-1) e^-t dt
Packit 6c4009
@end smallexample
Packit 6c4009
@end ifnottex
Packit 6c4009
Packit 6c4009
@vindex signgam
Packit 6c4009
The sign of the gamma function is stored in the global variable
Packit 6c4009
@var{signgam}, which is declared in @file{math.h}.  It is @code{1} if
Packit 6c4009
the intermediate result was positive or zero, or @code{-1} if it was
Packit 6c4009
negative.
Packit 6c4009
Packit 6c4009
To compute the real gamma function you can use the @code{tgamma}
Packit 6c4009
function or you can compute the values as follows:
Packit 6c4009
@smallexample
Packit 6c4009
lgam = lgamma(x);
Packit 6c4009
gam  = signgam*exp(lgam);
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
The gamma function has singularities at the non-positive integers.
Packit 6c4009
@code{lgamma} will raise the zero divide exception if evaluated at a
Packit 6c4009
singularity.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double lgamma_r (double @var{x}, int *@var{signp})
Packit 6c4009
@deftypefunx float lgammaf_r (float @var{x}, int *@var{signp})
Packit 6c4009
@deftypefunx {long double} lgammal_r (long double @var{x}, int *@var{signp})
Packit 6c4009
@deftypefunx _FloatN lgammafN_r (_Float@var{N} @var{x}, int *@var{signp})
Packit 6c4009
@deftypefunx _FloatNx lgammafNx_r (_Float@var{N}x @var{x}, int *@var{signp})
Packit 6c4009
@standards{XPG, math.h}
Packit 6c4009
@standardsx{lgammafN_r, GNU, math.h}
Packit 6c4009
@standardsx{lgammafNx_r, GNU, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{lgamma_r} is just like @code{lgamma}, but it stores the sign of
Packit 6c4009
the intermediate result in the variable pointed to by @var{signp}
Packit 6c4009
instead of in the @var{signgam} global.  This means it is reentrant.
Packit 6c4009
Packit 6c4009
The @code{lgammaf@var{N}_r} and @code{lgammaf@var{N}x_r} functions are
Packit 6c4009
GNU extensions.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double gamma (double @var{x})
Packit 6c4009
@deftypefunx float gammaf (float @var{x})
Packit 6c4009
@deftypefunx {long double} gammal (long double @var{x})
Packit 6c4009
@standards{SVID, math.h}
Packit 6c4009
@safety{@prelim{}@mtunsafe{@mtasurace{:signgam}}@asunsafe{}@acsafe{}}
Packit 6c4009
These functions exist for compatibility reasons.  They are equivalent to
Packit 6c4009
@code{lgamma} etc.  It is better to use @code{lgamma} since for one the
Packit 6c4009
name reflects better the actual computation, and moreover @code{lgamma} is
Packit 6c4009
standardized in @w{ISO C99} while @code{gamma} is not.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double tgamma (double @var{x})
Packit 6c4009
@deftypefunx float tgammaf (float @var{x})
Packit 6c4009
@deftypefunx {long double} tgammal (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN tgammafN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx tgammafNx (_Float@var{N}x @var{x})
Packit 6c4009
@standardsx{tgamma, XPG, math.h}
Packit 6c4009
@standardsx{tgamma, ISO, math.h}
Packit 6c4009
@standardsx{tgammaf, XPG, math.h}
Packit 6c4009
@standardsx{tgammaf, ISO, math.h}
Packit 6c4009
@standardsx{tgammal, XPG, math.h}
Packit 6c4009
@standardsx{tgammal, ISO, math.h}
Packit 6c4009
@standardsx{tgammafN, TS 18661-3:2015, math.h}
Packit 6c4009
@standardsx{tgammafNx, TS 18661-3:2015, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{tgamma} applies the gamma function to @var{x}.  The gamma
Packit 6c4009
function is defined as
Packit 6c4009
@tex
Packit 6c4009
$$\Gamma(x) = \int_0^\infty t^{x-1} e^{-t} \hbox{d}t$$
Packit 6c4009
@end tex
Packit 6c4009
@ifnottex
Packit 6c4009
@smallexample
Packit 6c4009
gamma (x) = integral from 0 to @infinity{} of t^(x-1) e^-t dt
Packit 6c4009
@end smallexample
Packit 6c4009
@end ifnottex
Packit 6c4009
Packit 6c4009
This function was introduced in @w{ISO C99}.  The @code{_Float@var{N}}
Packit 6c4009
and @code{_Float@var{N}x} variants were introduced in @w{ISO/IEC TS
Packit 6c4009
18661-3}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double j0 (double @var{x})
Packit 6c4009
@deftypefunx float j0f (float @var{x})
Packit 6c4009
@deftypefunx {long double} j0l (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN j0fN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx j0fNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{SVID, math.h}
Packit 6c4009
@standardsx{j0fN, GNU, math.h}
Packit 6c4009
@standardsx{j0fNx, GNU, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{j0} returns the Bessel function of the first kind of order 0 of
Packit 6c4009
@var{x}.  It may signal underflow if @var{x} is too large.
Packit 6c4009
Packit 6c4009
The @code{_Float@var{N}} and @code{_Float@var{N}x} variants are GNU
Packit 6c4009
extensions.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double j1 (double @var{x})
Packit 6c4009
@deftypefunx float j1f (float @var{x})
Packit 6c4009
@deftypefunx {long double} j1l (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN j1fN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx j1fNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{SVID, math.h}
Packit 6c4009
@standardsx{j1fN, GNU, math.h}
Packit 6c4009
@standardsx{j1fNx, GNU, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{j1} returns the Bessel function of the first kind of order 1 of
Packit 6c4009
@var{x}.  It may signal underflow if @var{x} is too large.
Packit 6c4009
Packit 6c4009
The @code{_Float@var{N}} and @code{_Float@var{N}x} variants are GNU
Packit 6c4009
extensions.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double jn (int @var{n}, double @var{x})
Packit 6c4009
@deftypefunx float jnf (int @var{n}, float @var{x})
Packit 6c4009
@deftypefunx {long double} jnl (int @var{n}, long double @var{x})
Packit 6c4009
@deftypefunx _FloatN jnfN (int @var{n}, _Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx jnfNx (int @var{n}, _Float@var{N}x @var{x})
Packit 6c4009
@standards{SVID, math.h}
Packit 6c4009
@standardsx{jnfN, GNU, math.h}
Packit 6c4009
@standardsx{jnfNx, GNU, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{jn} returns the Bessel function of the first kind of order
Packit 6c4009
@var{n} of @var{x}.  It may signal underflow if @var{x} is too large.
Packit 6c4009
Packit 6c4009
The @code{_Float@var{N}} and @code{_Float@var{N}x} variants are GNU
Packit 6c4009
extensions.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double y0 (double @var{x})
Packit 6c4009
@deftypefunx float y0f (float @var{x})
Packit 6c4009
@deftypefunx {long double} y0l (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN y0fN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx y0fNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{SVID, math.h}
Packit 6c4009
@standardsx{y0fN, GNU, math.h}
Packit 6c4009
@standardsx{y0fNx, GNU, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{y0} returns the Bessel function of the second kind of order 0 of
Packit 6c4009
@var{x}.  It may signal underflow if @var{x} is too large.  If @var{x}
Packit 6c4009
is negative, @code{y0} signals a domain error; if it is zero,
Packit 6c4009
@code{y0} signals overflow and returns @math{-@infinity}.
Packit 6c4009
Packit 6c4009
The @code{_Float@var{N}} and @code{_Float@var{N}x} variants are GNU
Packit 6c4009
extensions.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double y1 (double @var{x})
Packit 6c4009
@deftypefunx float y1f (float @var{x})
Packit 6c4009
@deftypefunx {long double} y1l (long double @var{x})
Packit 6c4009
@deftypefunx _FloatN y1fN (_Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx y1fNx (_Float@var{N}x @var{x})
Packit 6c4009
@standards{SVID, math.h}
Packit 6c4009
@standardsx{y1fN, GNU, math.h}
Packit 6c4009
@standardsx{y1fNx, GNU, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{y1} returns the Bessel function of the second kind of order 1 of
Packit 6c4009
@var{x}.  It may signal underflow if @var{x} is too large.  If @var{x}
Packit 6c4009
is negative, @code{y1} signals a domain error; if it is zero,
Packit 6c4009
@code{y1} signals overflow and returns @math{-@infinity}.
Packit 6c4009
Packit 6c4009
The @code{_Float@var{N}} and @code{_Float@var{N}x} variants are GNU
Packit 6c4009
extensions.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double yn (int @var{n}, double @var{x})
Packit 6c4009
@deftypefunx float ynf (int @var{n}, float @var{x})
Packit 6c4009
@deftypefunx {long double} ynl (int @var{n}, long double @var{x})
Packit 6c4009
@deftypefunx _FloatN ynfN (int @var{n}, _Float@var{N} @var{x})
Packit 6c4009
@deftypefunx _FloatNx ynfNx (int @var{n}, _Float@var{N}x @var{x})
Packit 6c4009
@standards{SVID, math.h}
Packit 6c4009
@standardsx{ynfN, GNU, math.h}
Packit 6c4009
@standardsx{ynfNx, GNU, math.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{yn} returns the Bessel function of the second kind of order @var{n} of
Packit 6c4009
@var{x}.  It may signal underflow if @var{x} is too large.  If @var{x}
Packit 6c4009
is negative, @code{yn} signals a domain error; if it is zero,
Packit 6c4009
@code{yn} signals overflow and returns @math{-@infinity}.
Packit 6c4009
Packit 6c4009
The @code{_Float@var{N}} and @code{_Float@var{N}x} variants are GNU
Packit 6c4009
extensions.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@node Errors in Math Functions
Packit 6c4009
@section Known Maximum Errors in Math Functions
Packit 6c4009
@cindex math errors
Packit 6c4009
@cindex ulps
Packit 6c4009
Packit 6c4009
This section lists the known errors of the functions in the math
Packit 6c4009
library.  Errors are measured in ``units of the last place''.  This is a
Packit 6c4009
measure for the relative error.  For a number @math{z} with the
Packit 6c4009
representation @math{d.d@dots{}d@mul{}2^e} (we assume IEEE
Packit 6c4009
floating-point numbers with base 2) the ULP is represented by
Packit 6c4009
Packit 6c4009
@tex
Packit 6c4009
$${|d.d\dots d - (z/2^e)|}\over {2^{p-1}}$$
Packit 6c4009
@end tex
Packit 6c4009
@ifnottex
Packit 6c4009
@smallexample
Packit 6c4009
|d.d...d - (z / 2^e)| / 2^(p - 1)
Packit 6c4009
@end smallexample
Packit 6c4009
@end ifnottex
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
where @math{p} is the number of bits in the mantissa of the
Packit 6c4009
floating-point number representation.  Ideally the error for all
Packit 6c4009
functions is always less than 0.5ulps in round-to-nearest mode.  Using
Packit 6c4009
rounding bits this is also
Packit 6c4009
possible and normally implemented for the basic operations.  Except
Packit 6c4009
for certain functions such as @code{sqrt}, @code{fma} and @code{rint}
Packit 6c4009
whose results are fully specified by reference to corresponding IEEE
Packit 6c4009
754 floating-point operations, and conversions between strings and
Packit 6c4009
floating point, @theglibc{} does not aim for correctly rounded results
Packit 6c4009
for functions in the math library, and does not aim for correctness in
Packit 6c4009
whether ``inexact'' exceptions are raised.  Instead, the goals for
Packit 6c4009
accuracy of functions without fully specified results are as follows;
Packit 6c4009
some functions have bugs meaning they do not meet these goals in all
Packit 6c4009
cases.  In the future, @theglibc{} may provide some other correctly
Packit 6c4009
rounding functions under the names such as @code{crsin} proposed for
Packit 6c4009
an extension to ISO C.
Packit 6c4009
Packit 6c4009
@itemize @bullet
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
Each function with a floating-point result behaves as if it computes
Packit 6c4009
an infinite-precision result that is within a few ulp (in both real
Packit 6c4009
and complex parts, for functions with complex results) of the
Packit 6c4009
mathematically correct value of the function (interpreted together
Packit 6c4009
with ISO C or POSIX semantics for the function in question) at the
Packit 6c4009
exact value passed as the input.  Exceptions are raised appropriately
Packit 6c4009
for this value and in accordance with IEEE 754 / ISO C / POSIX
Packit 6c4009
semantics, and it is then rounded according to the current rounding
Packit 6c4009
direction to the result that is returned to the user.  @code{errno}
Packit 6c4009
may also be set (@pxref{Math Error Reporting}).  (The ``inexact''
Packit 6c4009
exception may be raised, or not raised, even if this is inconsistent
Packit 6c4009
with the infinite-precision value.)
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
For the IBM @code{long double} format, as used on PowerPC GNU/Linux,
Packit 6c4009
the accuracy goal is weaker for input values not exactly representable
Packit 6c4009
in 106 bits of precision; it is as if the input value is some value
Packit 6c4009
within 0.5ulp of the value actually passed, where ``ulp'' is
Packit 6c4009
interpreted in terms of a fixed-precision 106-bit mantissa, but not
Packit 6c4009
necessarily the exact value actually passed with discontiguous
Packit 6c4009
mantissa bits.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
For the IBM @code{long double} format, functions whose results are
Packit 6c4009
fully specified by reference to corresponding IEEE 754 floating-point
Packit 6c4009
operations have the same accuracy goals as other functions, but with
Packit 6c4009
the error bound being the same as that for division (3ulp).
Packit 6c4009
Furthermore, ``inexact'' and ``underflow'' exceptions may be raised
Packit 6c4009
for all functions for any inputs, even where such exceptions are
Packit 6c4009
inconsistent with the returned value, since the underlying
Packit 6c4009
floating-point arithmetic has that property.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
Functions behave as if the infinite-precision result computed is zero,
Packit 6c4009
infinity or NaN if and only if that is the mathematically correct
Packit 6c4009
infinite-precision result.  They behave as if the infinite-precision
Packit 6c4009
result computed always has the same sign as the mathematically correct
Packit 6c4009
result.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
If the mathematical result is more than a few ulp above the overflow
Packit 6c4009
threshold for the current rounding direction, the value returned is
Packit 6c4009
the appropriate overflow value for the current rounding direction,
Packit 6c4009
with the overflow exception raised.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
If the mathematical result has magnitude well below half the least
Packit 6c4009
subnormal magnitude, the returned value is either zero or the least
Packit 6c4009
subnormal (in each case, with the correct sign), according to the
Packit 6c4009
current rounding direction and with the underflow exception raised.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
Where the mathematical result underflows (before rounding) and is not
Packit 6c4009
exactly representable as a floating-point value, the function does not
Packit 6c4009
behave as if the computed infinite-precision result is an exact value
Packit 6c4009
in the subnormal range.  This means that the underflow exception is
Packit 6c4009
raised other than possibly for cases where the mathematical result is
Packit 6c4009
very close to the underflow threshold and the function behaves as if
Packit 6c4009
it computes an infinite-precision result that does not underflow.  (So
Packit 6c4009
there may be spurious underflow exceptions in cases where the
Packit 6c4009
underflowing result is exact, but not missing underflow exceptions in
Packit 6c4009
cases where it is inexact.)
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
@Theglibc{} does not aim for functions to satisfy other properties of
Packit 6c4009
the underlying mathematical function, such as monotonicity, where not
Packit 6c4009
implied by the above goals.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
All the above applies to both real and complex parts, for complex
Packit 6c4009
functions.
Packit 6c4009
Packit 6c4009
@end itemize
Packit 6c4009
Packit 6c4009
Therefore many of the functions in the math library have errors.  The
Packit 6c4009
table lists the maximum error for each function which is exposed by one
Packit 6c4009
of the existing tests in the test suite.  The table tries to cover as much
Packit 6c4009
as possible and list the actual maximum error (or at least a ballpark
Packit 6c4009
figure) but this is often not achieved due to the large search space.
Packit 6c4009
Packit 6c4009
The table lists the ULP values for different architectures.  Different
Packit 6c4009
architectures have different results since their hardware support for
Packit 6c4009
floating-point operations varies and also the existing hardware support
Packit 6c4009
is different.  Only the round-to-nearest rounding mode is covered by
Packit 6c4009
this table, and vector versions of functions are not covered.
Packit 6c4009
Functions not listed do not have known errors.
Packit 6c4009
Packit 6c4009
@page
Packit 6c4009
@c This multitable does not fit on a single page
Packit 6c4009
@include libm-err.texi
Packit 6c4009
Packit 6c4009
@node Pseudo-Random Numbers
Packit 6c4009
@section Pseudo-Random Numbers
Packit 6c4009
@cindex random numbers
Packit 6c4009
@cindex pseudo-random numbers
Packit 6c4009
@cindex seed (for random numbers)
Packit 6c4009
Packit 6c4009
This section describes the GNU facilities for generating a series of
Packit 6c4009
pseudo-random numbers.  The numbers generated are not truly random;
Packit 6c4009
typically, they form a sequence that repeats periodically, with a period
Packit 6c4009
so large that you can ignore it for ordinary purposes.  The random
Packit 6c4009
number generator works by remembering a @dfn{seed} value which it uses
Packit 6c4009
to compute the next random number and also to compute a new seed.
Packit 6c4009
Packit 6c4009
Although the generated numbers look unpredictable within one run of a
Packit 6c4009
program, the sequence of numbers is @emph{exactly the same} from one run
Packit 6c4009
to the next.  This is because the initial seed is always the same.  This
Packit 6c4009
is convenient when you are debugging a program, but it is unhelpful if
Packit 6c4009
you want the program to behave unpredictably.  If you want a different
Packit 6c4009
pseudo-random series each time your program runs, you must specify a
Packit 6c4009
different seed each time.  For ordinary purposes, basing the seed on the
Packit 6c4009
current time works well.  For random numbers in cryptography,
Packit 6c4009
@pxref{Unpredictable Bytes}.
Packit 6c4009
Packit 6c4009
You can obtain repeatable sequences of numbers on a particular machine type
Packit 6c4009
by specifying the same initial seed value for the random number
Packit 6c4009
generator.  There is no standard meaning for a particular seed value;
Packit 6c4009
the same seed, used in different C libraries or on different CPU types,
Packit 6c4009
will give you different random numbers.
Packit 6c4009
Packit 6c4009
@Theglibc{} supports the standard @w{ISO C} random number functions
Packit 6c4009
plus two other sets derived from BSD and SVID.  The BSD and @w{ISO C}
Packit 6c4009
functions provide identical, somewhat limited functionality.  If only a
Packit 6c4009
small number of random bits are required, we recommend you use the
Packit 6c4009
@w{ISO C} interface, @code{rand} and @code{srand}.  The SVID functions
Packit 6c4009
provide a more flexible interface, which allows better random number
Packit 6c4009
generator algorithms, provides more random bits (up to 48) per call, and
Packit 6c4009
can provide random floating-point numbers.  These functions are required
Packit 6c4009
by the XPG standard and therefore will be present in all modern Unix
Packit 6c4009
systems.
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* ISO Random::                  @code{rand} and friends.
Packit 6c4009
* BSD Random::                  @code{random} and friends.
Packit 6c4009
* SVID Random::                 @code{drand48} and friends.
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
@node ISO Random
Packit 6c4009
@subsection ISO C Random Number Functions
Packit 6c4009
Packit 6c4009
This section describes the random number functions that are part of
Packit 6c4009
the @w{ISO C} standard.
Packit 6c4009
Packit 6c4009
To use these facilities, you should include the header file
Packit 6c4009
@file{stdlib.h} in your program.
Packit 6c4009
@pindex stdlib.h
Packit 6c4009
Packit 6c4009
@deftypevr Macro int RAND_MAX
Packit 6c4009
@standards{ISO, stdlib.h}
Packit 6c4009
The value of this macro is an integer constant representing the largest
Packit 6c4009
value the @code{rand} function can return.  In @theglibc{}, it is
Packit 6c4009
@code{2147483647}, which is the largest signed integer representable in
Packit 6c4009
32 bits.  In other libraries, it may be as low as @code{32767}.
Packit 6c4009
@end deftypevr
Packit 6c4009
Packit 6c4009
@deftypefun int rand (void)
Packit 6c4009
@standards{ISO, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
Packit 6c4009
@c Just calls random.
Packit 6c4009
The @code{rand} function returns the next pseudo-random number in the
Packit 6c4009
series.  The value ranges from @code{0} to @code{RAND_MAX}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun void srand (unsigned int @var{seed})
Packit 6c4009
@standards{ISO, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
Packit 6c4009
@c Alias to srandom.
Packit 6c4009
This function establishes @var{seed} as the seed for a new series of
Packit 6c4009
pseudo-random numbers.  If you call @code{rand} before a seed has been
Packit 6c4009
established with @code{srand}, it uses the value @code{1} as a default
Packit 6c4009
seed.
Packit 6c4009
Packit 6c4009
To produce a different pseudo-random series each time your program is
Packit 6c4009
run, do @code{srand (time (0))}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
POSIX.1 extended the C standard functions to support reproducible random
Packit 6c4009
numbers in multi-threaded programs.  However, the extension is badly
Packit 6c4009
designed and unsuitable for serious work.
Packit 6c4009
Packit 6c4009
@deftypefun int rand_r (unsigned int *@var{seed})
Packit 6c4009
@standards{POSIX.1, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This function returns a random number in the range 0 to @code{RAND_MAX}
Packit 6c4009
just as @code{rand} does.  However, all its state is stored in the
Packit 6c4009
@var{seed} argument.  This means the RNG's state can only have as many
Packit 6c4009
bits as the type @code{unsigned int} has.  This is far too few to
Packit 6c4009
provide a good RNG.
Packit 6c4009
Packit 6c4009
If your program requires a reentrant RNG, we recommend you use the
Packit 6c4009
reentrant GNU extensions to the SVID random number generator.  The
Packit 6c4009
POSIX.1 interface should only be used when the GNU extensions are not
Packit 6c4009
available.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node BSD Random
Packit 6c4009
@subsection BSD Random Number Functions
Packit 6c4009
Packit 6c4009
This section describes a set of random number generation functions that
Packit 6c4009
are derived from BSD.  There is no advantage to using these functions
Packit 6c4009
with @theglibc{}; we support them for BSD compatibility only.
Packit 6c4009
Packit 6c4009
The prototypes for these functions are in @file{stdlib.h}.
Packit 6c4009
@pindex stdlib.h
Packit 6c4009
Packit 6c4009
@deftypefun {long int} random (void)
Packit 6c4009
@standards{BSD, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
Packit 6c4009
@c Takes a lock and calls random_r with an automatic variable and the
Packit 6c4009
@c global state, while holding a lock.
Packit 6c4009
This function returns the next pseudo-random number in the sequence.
Packit 6c4009
The value returned ranges from @code{0} to @code{2147483647}.
Packit 6c4009
Packit 6c4009
@strong{NB:} Temporarily this function was defined to return a
Packit 6c4009
@code{int32_t} value to indicate that the return value always contains
Packit 6c4009
32 bits even if @code{long int} is wider.  The standard demands it
Packit 6c4009
differently.  Users must always be aware of the 32-bit limitation,
Packit 6c4009
though.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun void srandom (unsigned int @var{seed})
Packit 6c4009
@standards{BSD, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
Packit 6c4009
@c Takes a lock and calls srandom_r with an automatic variable and a
Packit 6c4009
@c static buffer.  There's no MT-safety issue because the static buffer
Packit 6c4009
@c is internally protected by a lock, although other threads may modify
Packit 6c4009
@c the set state before it is used.
Packit 6c4009
The @code{srandom} function sets the state of the random number
Packit 6c4009
generator based on the integer @var{seed}.  If you supply a @var{seed} value
Packit 6c4009
of @code{1}, this will cause @code{random} to reproduce the default set
Packit 6c4009
of random numbers.
Packit 6c4009
Packit 6c4009
To produce a different set of pseudo-random numbers each time your
Packit 6c4009
program runs, do @code{srandom (time (0))}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {char *} initstate (unsigned int @var{seed}, char *@var{state}, size_t @var{size})
Packit 6c4009
@standards{BSD, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
Packit 6c4009
The @code{initstate} function is used to initialize the random number
Packit 6c4009
generator state.  The argument @var{state} is an array of @var{size}
Packit 6c4009
bytes, used to hold the state information.  It is initialized based on
Packit 6c4009
@var{seed}.  The size must be between 8 and 256 bytes, and should be a
Packit 6c4009
power of two.  The bigger the @var{state} array, the better.
Packit 6c4009
Packit 6c4009
The return value is the previous value of the state information array.
Packit 6c4009
You can use this value later as an argument to @code{setstate} to
Packit 6c4009
restore that state.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {char *} setstate (char *@var{state})
Packit 6c4009
@standards{BSD, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
Packit 6c4009
The @code{setstate} function restores the random number state
Packit 6c4009
information @var{state}.  The argument must have been the result of
Packit 6c4009
a previous call to @var{initstate} or @var{setstate}.
Packit 6c4009
Packit 6c4009
The return value is the previous value of the state information array.
Packit 6c4009
You can use this value later as an argument to @code{setstate} to
Packit 6c4009
restore that state.
Packit 6c4009
Packit 6c4009
If the function fails the return value is @code{NULL}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
The four functions described so far in this section all work on a state
Packit 6c4009
which is shared by all threads.  The state is not directly accessible to
Packit 6c4009
the user and can only be modified by these functions.  This makes it
Packit 6c4009
hard to deal with situations where each thread should have its own
Packit 6c4009
pseudo-random number generator.
Packit 6c4009
Packit 6c4009
@Theglibc{} contains four additional functions which contain the
Packit 6c4009
state as an explicit parameter and therefore make it possible to handle
Packit 6c4009
thread-local PRNGs.  Besides this there is no difference.  In fact, the
Packit 6c4009
four functions already discussed are implemented internally using the
Packit 6c4009
following interfaces.
Packit 6c4009
Packit 6c4009
The @file{stdlib.h} header contains a definition of the following type:
Packit 6c4009
Packit 6c4009
@deftp {Data Type} {struct random_data}
Packit 6c4009
@standards{GNU, stdlib.h}
Packit 6c4009
Packit 6c4009
Objects of type @code{struct random_data} contain the information
Packit 6c4009
necessary to represent the state of the PRNG.  Although a complete
Packit 6c4009
definition of the type is present the type should be treated as opaque.
Packit 6c4009
@end deftp
Packit 6c4009
Packit 6c4009
The functions modifying the state follow exactly the already described
Packit 6c4009
functions.
Packit 6c4009
Packit 6c4009
@deftypefun int random_r (struct random_data *restrict @var{buf}, int32_t *restrict @var{result})
Packit 6c4009
@standards{GNU, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtsrace{:buf}}@assafe{}@acunsafe{@acucorrupt{}}}
Packit 6c4009
The @code{random_r} function behaves exactly like the @code{random}
Packit 6c4009
function except that it uses and modifies the state in the object
Packit 6c4009
pointed to by the first parameter instead of the global state.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int srandom_r (unsigned int @var{seed}, struct random_data *@var{buf})
Packit 6c4009
@standards{GNU, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtsrace{:buf}}@assafe{}@acunsafe{@acucorrupt{}}}
Packit 6c4009
The @code{srandom_r} function behaves exactly like the @code{srandom}
Packit 6c4009
function except that it uses and modifies the state in the object
Packit 6c4009
pointed to by the second parameter instead of the global state.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int initstate_r (unsigned int @var{seed}, char *restrict @var{statebuf}, size_t @var{statelen}, struct random_data *restrict @var{buf})
Packit 6c4009
@standards{GNU, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtsrace{:buf}}@assafe{}@acunsafe{@acucorrupt{}}}
Packit 6c4009
The @code{initstate_r} function behaves exactly like the @code{initstate}
Packit 6c4009
function except that it uses and modifies the state in the object
Packit 6c4009
pointed to by the fourth parameter instead of the global state.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int setstate_r (char *restrict @var{statebuf}, struct random_data *restrict @var{buf})
Packit 6c4009
@standards{GNU, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtsrace{:buf}}@assafe{}@acunsafe{@acucorrupt{}}}
Packit 6c4009
The @code{setstate_r} function behaves exactly like the @code{setstate}
Packit 6c4009
function except that it uses and modifies the state in the object
Packit 6c4009
pointed to by the first parameter instead of the global state.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@node SVID Random
Packit 6c4009
@subsection SVID Random Number Function
Packit 6c4009
Packit 6c4009
The C library on SVID systems contains yet another kind of random number
Packit 6c4009
generator functions.  They use a state of 48 bits of data.  The user can
Packit 6c4009
choose among a collection of functions which return the random bits
Packit 6c4009
in different forms.
Packit 6c4009
Packit 6c4009
Generally there are two kinds of function.  The first uses a state of
Packit 6c4009
the random number generator which is shared among several functions and
Packit 6c4009
by all threads of the process.  The second requires the user to handle
Packit 6c4009
the state.
Packit 6c4009
Packit 6c4009
All functions have in common that they use the same congruential
Packit 6c4009
formula with the same constants.  The formula is
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
Y = (a * X + c) mod m
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
where @var{X} is the state of the generator at the beginning and
Packit 6c4009
@var{Y} the state at the end.  @code{a} and @code{c} are constants
Packit 6c4009
determining the way the generator works.  By default they are
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
a = 0x5DEECE66D = 25214903917
Packit 6c4009
c = 0xb = 11
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
but they can also be changed by the user.  @code{m} is of course 2^48
Packit 6c4009
since the state consists of a 48-bit array.
Packit 6c4009
Packit 6c4009
The prototypes for these functions are in @file{stdlib.h}.
Packit 6c4009
@pindex stdlib.h
Packit 6c4009
Packit 6c4009
Packit 6c4009
@deftypefun double drand48 (void)
Packit 6c4009
@standards{SVID, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
Packit 6c4009
@c Uses of the static state buffer are not guarded by a lock (thus
Packit 6c4009
@c @mtasurace:drand48), so they may be found or left at a
Packit 6c4009
@c partially-updated state in case of calls from within signal handlers
Packit 6c4009
@c or cancellation.  None of this will break safety rules or invoke
Packit 6c4009
@c undefined behavior, but it may affect randomness.
Packit 6c4009
This function returns a @code{double} value in the range of @code{0.0}
Packit 6c4009
to @code{1.0} (exclusive).  The random bits are determined by the global
Packit 6c4009
state of the random number generator in the C library.
Packit 6c4009
Packit 6c4009
Since the @code{double} type according to @w{IEEE 754} has a 52-bit
Packit 6c4009
mantissa this means 4 bits are not initialized by the random number
Packit 6c4009
generator.  These are (of course) chosen to be the least significant
Packit 6c4009
bits and they are initialized to @code{0}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun double erand48 (unsigned short int @var{xsubi}[3])
Packit 6c4009
@standards{SVID, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
Packit 6c4009
@c The static buffer is just initialized with default parameters, which
Packit 6c4009
@c are later read to advance the state held in xsubi.
Packit 6c4009
This function returns a @code{double} value in the range of @code{0.0}
Packit 6c4009
to @code{1.0} (exclusive), similarly to @code{drand48}.  The argument is
Packit 6c4009
an array describing the state of the random number generator.
Packit 6c4009
Packit 6c4009
This function can be called subsequently since it updates the array to
Packit 6c4009
guarantee random numbers.  The array should have been initialized before
Packit 6c4009
initial use to obtain reproducible results.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {long int} lrand48 (void)
Packit 6c4009
@standards{SVID, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
Packit 6c4009
The @code{lrand48} function returns an integer value in the range of
Packit 6c4009
@code{0} to @code{2^31} (exclusive).  Even if the size of the @code{long
Packit 6c4009
int} type can take more than 32 bits, no higher numbers are returned.
Packit 6c4009
The random bits are determined by the global state of the random number
Packit 6c4009
generator in the C library.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {long int} nrand48 (unsigned short int @var{xsubi}[3])
Packit 6c4009
@standards{SVID, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
Packit 6c4009
This function is similar to the @code{lrand48} function in that it
Packit 6c4009
returns a number in the range of @code{0} to @code{2^31} (exclusive) but
Packit 6c4009
the state of the random number generator used to produce the random bits
Packit 6c4009
is determined by the array provided as the parameter to the function.
Packit 6c4009
Packit 6c4009
The numbers in the array are updated afterwards so that subsequent calls
Packit 6c4009
to this function yield different results (as is expected of a random
Packit 6c4009
number generator).  The array should have been initialized before the
Packit 6c4009
first call to obtain reproducible results.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {long int} mrand48 (void)
Packit 6c4009
@standards{SVID, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
Packit 6c4009
The @code{mrand48} function is similar to @code{lrand48}.  The only
Packit 6c4009
difference is that the numbers returned are in the range @code{-2^31} to
Packit 6c4009
@code{2^31} (exclusive).
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {long int} jrand48 (unsigned short int @var{xsubi}[3])
Packit 6c4009
@standards{SVID, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
Packit 6c4009
The @code{jrand48} function is similar to @code{nrand48}.  The only
Packit 6c4009
difference is that the numbers returned are in the range @code{-2^31} to
Packit 6c4009
@code{2^31} (exclusive).  For the @code{xsubi} parameter the same
Packit 6c4009
requirements are necessary.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
The internal state of the random number generator can be initialized in
Packit 6c4009
several ways.  The methods differ in the completeness of the
Packit 6c4009
information provided.
Packit 6c4009
Packit 6c4009
@deftypefun void srand48 (long int @var{seedval})
Packit 6c4009
@standards{SVID, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
Packit 6c4009
The @code{srand48} function sets the most significant 32 bits of the
Packit 6c4009
internal state of the random number generator to the least
Packit 6c4009
significant 32 bits of the @var{seedval} parameter.  The lower 16 bits
Packit 6c4009
are initialized to the value @code{0x330E}.  Even if the @code{long
Packit 6c4009
int} type contains more than 32 bits only the lower 32 bits are used.
Packit 6c4009
Packit 6c4009
Owing to this limitation, initialization of the state of this
Packit 6c4009
function is not very useful.  But it makes it easy to use a construct
Packit 6c4009
like @code{srand48 (time (0))}.
Packit 6c4009
Packit 6c4009
A side-effect of this function is that the values @code{a} and @code{c}
Packit 6c4009
from the internal state, which are used in the congruential formula,
Packit 6c4009
are reset to the default values given above.  This is of importance once
Packit 6c4009
the user has called the @code{lcong48} function (see below).
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {unsigned short int *} seed48 (unsigned short int @var{seed16v}[3])
Packit 6c4009
@standards{SVID, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
Packit 6c4009
The @code{seed48} function initializes all 48 bits of the state of the
Packit 6c4009
internal random number generator from the contents of the parameter
Packit 6c4009
@var{seed16v}.  Here the lower 16 bits of the first element of
Packit 6c4009
@var{seed16v} initialize the least significant 16 bits of the internal
Packit 6c4009
state, the lower 16 bits of @code{@var{seed16v}[1]} initialize the mid-order
Packit 6c4009
16 bits of the state and the 16 lower bits of @code{@var{seed16v}[2]}
Packit 6c4009
initialize the most significant 16 bits of the state.
Packit 6c4009
Packit 6c4009
Unlike @code{srand48} this function lets the user initialize all 48 bits
Packit 6c4009
of the state.
Packit 6c4009
Packit 6c4009
The value returned by @code{seed48} is a pointer to an array containing
Packit 6c4009
the values of the internal state before the change.  This might be
Packit 6c4009
useful to restart the random number generator at a certain state.
Packit 6c4009
Otherwise the value can simply be ignored.
Packit 6c4009
Packit 6c4009
As for @code{srand48}, the values @code{a} and @code{c} from the
Packit 6c4009
congruential formula are reset to the default values.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
There is one more function to initialize the random number generator
Packit 6c4009
which enables you to specify even more information by allowing you to
Packit 6c4009
change the parameters in the congruential formula.
Packit 6c4009
Packit 6c4009
@deftypefun void lcong48 (unsigned short int @var{param}[7])
Packit 6c4009
@standards{SVID, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
Packit 6c4009
The @code{lcong48} function allows the user to change the complete state
Packit 6c4009
of the random number generator.  Unlike @code{srand48} and
Packit 6c4009
@code{seed48}, this function also changes the constants in the
Packit 6c4009
congruential formula.
Packit 6c4009
Packit 6c4009
From the seven elements in the array @var{param} the least significant
Packit 6c4009
16 bits of the entries @code{@var{param}[0]} to @code{@var{param}[2]}
Packit 6c4009
determine the initial state, the least significant 16 bits of
Packit 6c4009
@code{@var{param}[3]} to @code{@var{param}[5]} determine the 48 bit
Packit 6c4009
constant @code{a} and @code{@var{param}[6]} determines the 16-bit value
Packit 6c4009
@code{c}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
All the above functions have in common that they use the global
Packit 6c4009
parameters for the congruential formula.  In multi-threaded programs it
Packit 6c4009
might sometimes be useful to have different parameters in different
Packit 6c4009
threads.  For this reason all the above functions have a counterpart
Packit 6c4009
which works on a description of the random number generator in the
Packit 6c4009
user-supplied buffer instead of the global state.
Packit 6c4009
Packit 6c4009
Please note that it is no problem if several threads use the global
Packit 6c4009
state if all threads use the functions which take a pointer to an array
Packit 6c4009
containing the state.  The random numbers are computed following the
Packit 6c4009
same loop but if the state in the array is different all threads will
Packit 6c4009
obtain an individual random number generator.
Packit 6c4009
Packit 6c4009
The user-supplied buffer must be of type @code{struct drand48_data}.
Packit 6c4009
This type should be regarded as opaque and not manipulated directly.
Packit 6c4009
Packit 6c4009
@deftypefun int drand48_r (struct drand48_data *@var{buffer}, double *@var{result})
Packit 6c4009
@standards{GNU, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
Packit 6c4009
This function is equivalent to the @code{drand48} function with the
Packit 6c4009
difference that it does not modify the global random number generator
Packit 6c4009
parameters but instead the parameters in the buffer supplied through the
Packit 6c4009
pointer @var{buffer}.  The random number is returned in the variable
Packit 6c4009
pointed to by @var{result}.
Packit 6c4009
Packit 6c4009
The return value of the function indicates whether the call succeeded.
Packit 6c4009
If the value is less than @code{0} an error occurred and @var{errno} is
Packit 6c4009
set to indicate the problem.
Packit 6c4009
Packit 6c4009
This function is a GNU extension and should not be used in portable
Packit 6c4009
programs.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int erand48_r (unsigned short int @var{xsubi}[3], struct drand48_data *@var{buffer}, double *@var{result})
Packit 6c4009
@standards{GNU, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
Packit 6c4009
The @code{erand48_r} function works like @code{erand48}, but in addition
Packit 6c4009
it takes an argument @var{buffer} which describes the random number
Packit 6c4009
generator.  The state of the random number generator is taken from the
Packit 6c4009
@code{xsubi} array, the parameters for the congruential formula from the
Packit 6c4009
global random number generator data.  The random number is returned in
Packit 6c4009
the variable pointed to by @var{result}.
Packit 6c4009
Packit 6c4009
The return value is non-negative if the call succeeded.
Packit 6c4009
Packit 6c4009
This function is a GNU extension and should not be used in portable
Packit 6c4009
programs.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int lrand48_r (struct drand48_data *@var{buffer}, long int *@var{result})
Packit 6c4009
@standards{GNU, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
Packit 6c4009
This function is similar to @code{lrand48}, but in addition it takes a
Packit 6c4009
pointer to a buffer describing the state of the random number generator
Packit 6c4009
just like @code{drand48}.
Packit 6c4009
Packit 6c4009
If the return value of the function is non-negative the variable pointed
Packit 6c4009
to by @var{result} contains the result.  Otherwise an error occurred.
Packit 6c4009
Packit 6c4009
This function is a GNU extension and should not be used in portable
Packit 6c4009
programs.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int nrand48_r (unsigned short int @var{xsubi}[3], struct drand48_data *@var{buffer}, long int *@var{result})
Packit 6c4009
@standards{GNU, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
Packit 6c4009
The @code{nrand48_r} function works like @code{nrand48} in that it
Packit 6c4009
produces a random number in the range @code{0} to @code{2^31}.  But instead
Packit 6c4009
of using the global parameters for the congruential formula it uses the
Packit 6c4009
information from the buffer pointed to by @var{buffer}.  The state is
Packit 6c4009
described by the values in @var{xsubi}.
Packit 6c4009
Packit 6c4009
If the return value is non-negative the variable pointed to by
Packit 6c4009
@var{result} contains the result.
Packit 6c4009
Packit 6c4009
This function is a GNU extension and should not be used in portable
Packit 6c4009
programs.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int mrand48_r (struct drand48_data *@var{buffer}, long int *@var{result})
Packit 6c4009
@standards{GNU, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
Packit 6c4009
This function is similar to @code{mrand48} but like the other reentrant
Packit 6c4009
functions it uses the random number generator described by the value in
Packit 6c4009
the buffer pointed to by @var{buffer}.
Packit 6c4009
Packit 6c4009
If the return value is non-negative the variable pointed to by
Packit 6c4009
@var{result} contains the result.
Packit 6c4009
Packit 6c4009
This function is a GNU extension and should not be used in portable
Packit 6c4009
programs.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int jrand48_r (unsigned short int @var{xsubi}[3], struct drand48_data *@var{buffer}, long int *@var{result})
Packit 6c4009
@standards{GNU, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
Packit 6c4009
The @code{jrand48_r} function is similar to @code{jrand48}.  Like the
Packit 6c4009
other reentrant functions of this function family it uses the
Packit 6c4009
congruential formula parameters from the buffer pointed to by
Packit 6c4009
@var{buffer}.
Packit 6c4009
Packit 6c4009
If the return value is non-negative the variable pointed to by
Packit 6c4009
@var{result} contains the result.
Packit 6c4009
Packit 6c4009
This function is a GNU extension and should not be used in portable
Packit 6c4009
programs.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Before any of the above functions are used the buffer of type
Packit 6c4009
@code{struct drand48_data} should be initialized.  The easiest way to do
Packit 6c4009
this is to fill the whole buffer with null bytes, e.g. by
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
memset (buffer, '\0', sizeof (struct drand48_data));
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
Using any of the reentrant functions of this family now will
Packit 6c4009
automatically initialize the random number generator to the default
Packit 6c4009
values for the state and the parameters of the congruential formula.
Packit 6c4009
Packit 6c4009
The other possibility is to use any of the functions which explicitly
Packit 6c4009
initialize the buffer.  Though it might be obvious how to initialize the
Packit 6c4009
buffer from looking at the parameter to the function, it is highly
Packit 6c4009
recommended to use these functions since the result might not always be
Packit 6c4009
what you expect.
Packit 6c4009
Packit 6c4009
@deftypefun int srand48_r (long int @var{seedval}, struct drand48_data *@var{buffer})
Packit 6c4009
@standards{GNU, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
Packit 6c4009
The description of the random number generator represented by the
Packit 6c4009
information in @var{buffer} is initialized similarly to what the function
Packit 6c4009
@code{srand48} does.  The state is initialized from the parameter
Packit 6c4009
@var{seedval} and the parameters for the congruential formula are
Packit 6c4009
initialized to their default values.
Packit 6c4009
Packit 6c4009
If the return value is non-negative the function call succeeded.
Packit 6c4009
Packit 6c4009
This function is a GNU extension and should not be used in portable
Packit 6c4009
programs.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int seed48_r (unsigned short int @var{seed16v}[3], struct drand48_data *@var{buffer})
Packit 6c4009
@standards{GNU, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
Packit 6c4009
This function is similar to @code{srand48_r} but like @code{seed48} it
Packit 6c4009
initializes all 48 bits of the state from the parameter @var{seed16v}.
Packit 6c4009
Packit 6c4009
If the return value is non-negative the function call succeeded.  It
Packit 6c4009
does not return a pointer to the previous state of the random number
Packit 6c4009
generator like the @code{seed48} function does.  If the user wants to
Packit 6c4009
preserve the state for a later re-run s/he can copy the whole buffer
Packit 6c4009
pointed to by @var{buffer}.
Packit 6c4009
Packit 6c4009
This function is a GNU extension and should not be used in portable
Packit 6c4009
programs.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int lcong48_r (unsigned short int @var{param}[7], struct drand48_data *@var{buffer})
Packit 6c4009
@standards{GNU, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
Packit 6c4009
This function initializes all aspects of the random number generator
Packit 6c4009
described in @var{buffer} with the data in @var{param}.  Here it is
Packit 6c4009
especially true that the function does more than just copying the
Packit 6c4009
contents of @var{param} and @var{buffer}.  More work is required and
Packit 6c4009
therefore it is important to use this function rather than initializing
Packit 6c4009
the random number generator directly.
Packit 6c4009
Packit 6c4009
If the return value is non-negative the function call succeeded.
Packit 6c4009
Packit 6c4009
This function is a GNU extension and should not be used in portable
Packit 6c4009
programs.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@node FP Function Optimizations
Packit 6c4009
@section Is Fast Code or Small Code preferred?
Packit 6c4009
@cindex Optimization
Packit 6c4009
Packit 6c4009
If an application uses many floating point functions it is often the case
Packit 6c4009
that the cost of the function calls themselves is not negligible.
Packit 6c4009
Modern processors can often execute the operations themselves
Packit 6c4009
very fast, but the function call disrupts the instruction pipeline.
Packit 6c4009
Packit 6c4009
For this reason @theglibc{} provides optimizations for many of the
Packit 6c4009
frequently-used math functions.  When GNU CC is used and the user
Packit 6c4009
activates the optimizer, several new inline functions and macros are
Packit 6c4009
defined.  These new functions and macros have the same names as the
Packit 6c4009
library functions and so are used instead of the latter.  In the case of
Packit 6c4009
inline functions the compiler will decide whether it is reasonable to
Packit 6c4009
use them, and this decision is usually correct.
Packit 6c4009
Packit 6c4009
This means that no calls to the library functions may be necessary, and
Packit 6c4009
can increase the speed of generated code significantly.  The drawback is
Packit 6c4009
that code size will increase, and the increase is not always negligible.
Packit 6c4009
Packit 6c4009
There are two kinds of inline functions: those that give the same result
Packit 6c4009
as the library functions and others that might not set @code{errno} and
Packit 6c4009
might have a reduced precision and/or argument range in comparison with
Packit 6c4009
the library functions.  The latter inline functions are only available
Packit 6c4009
if the flag @code{-ffast-math} is given to GNU CC.
Packit 6c4009
Packit 6c4009
In cases where the inline functions and macros are not wanted the symbol
Packit 6c4009
@code{__NO_MATH_INLINES} should be defined before any system header is
Packit 6c4009
included.  This will ensure that only library functions are used.  Of
Packit 6c4009
course, it can be determined for each file in the project whether
Packit 6c4009
giving this option is preferable or not.
Packit 6c4009
Packit 6c4009
Not all hardware implements the entire @w{IEEE 754} standard, and even
Packit 6c4009
if it does there may be a substantial performance penalty for using some
Packit 6c4009
of its features.  For example, enabling traps on some processors forces
Packit 6c4009
the FPU to run un-pipelined, which can more than double calculation time.
Packit 6c4009
@c ***Add explanation of -lieee, -mieee.