]> git.ipfire.org Git - thirdparty/glibc.git/blame - manual/math.texi
* manual/math.texi: Document MTASC-safety properties.
[thirdparty/glibc.git] / manual / math.texi
CommitLineData
55c14926 1@c We need some definitions here.
7a68c94a 2@ifclear mult
55c14926 3@ifhtml
7a68c94a
UD
4@set mult ·
5@set infty ∞
6@set pie π
55c14926 7@end ifhtml
ca34d7a7 8@iftex
838e5ffe 9@set mult @cdot
7a68c94a 10@set infty @infty
ca34d7a7 11@end iftex
838e5ffe 12@ifclear mult
7a68c94a
UD
13@set mult *
14@set infty oo
15@set pie pi
838e5ffe 16@end ifclear
fe0ec73e 17@macro mul
838e5ffe 18@value{mult}
fe0ec73e 19@end macro
ca34d7a7
UD
20@macro infinity
21@value{infty}
22@end macro
7a68c94a
UD
23@ifnottex
24@macro pi
25@value{pie}
26@end macro
27@end ifnottex
28@end ifclear
55c14926 29
d52b6462 30@node Mathematics, Arithmetic, Syslog, Top
7a68c94a 31@c %MENU% Math functions, useful constants, random numbers
28f540f4
RM
32@chapter Mathematics
33
34This chapter contains information about functions for performing
35mathematical computations, such as trigonometric functions. Most of
36these functions have prototypes declared in the header file
7a68c94a
UD
37@file{math.h}. The complex-valued functions are defined in
38@file{complex.h}.
28f540f4 39@pindex math.h
7a68c94a
UD
40@pindex complex.h
41
42All mathematical functions which take a floating-point argument
43have three variants, one each for @code{double}, @code{float}, and
44@code{long double} arguments. The @code{double} versions are mostly
ec751a23
UD
45defined in @w{ISO C89}. The @code{float} and @code{long double}
46versions are from the numeric extensions to C included in @w{ISO C99}.
7a68c94a
UD
47
48Which of the three versions of a function should be used depends on the
49situation. For most calculations, the @code{float} functions are the
50fastest. On the other hand, the @code{long double} functions have the
51highest precision. @code{double} is somewhere in between. It is
04b9968b 52usually wise to pick the narrowest type that can accommodate your data.
7a68c94a
UD
53Not all machines have a distinct @code{long double} type; it may be the
54same as @code{double}.
28f540f4
RM
55
56@menu
7a68c94a
UD
57* Mathematical Constants:: Precise numeric values for often-used
58 constants.
59* Trig Functions:: Sine, cosine, tangent, and friends.
60* Inverse Trig Functions:: Arcsine, arccosine, etc.
61* Exponents and Logarithms:: Also pow and sqrt.
62* Hyperbolic Functions:: sinh, cosh, tanh, etc.
63* Special Functions:: Bessel, gamma, erf.
aaa1276e 64* Errors in Math Functions:: Known Maximum Errors in Math Functions.
7a68c94a
UD
65* Pseudo-Random Numbers:: Functions for generating pseudo-random
66 numbers.
67* FP Function Optimizations:: Fast code or small code.
28f540f4
RM
68@end menu
69
55c14926
UD
70@node Mathematical Constants
71@section Predefined Mathematical Constants
72@cindex constants
73@cindex mathematical constants
74
7a68c94a
UD
75The header @file{math.h} defines several useful mathematical constants.
76All values are defined as preprocessor macros starting with @code{M_}.
77The values provided are:
55c14926
UD
78
79@vtable @code
80@item M_E
7a68c94a 81The base of natural logarithms.
55c14926 82@item M_LOG2E
7a68c94a 83The logarithm to base @code{2} of @code{M_E}.
55c14926 84@item M_LOG10E
7a68c94a 85The logarithm to base @code{10} of @code{M_E}.
55c14926 86@item M_LN2
7a68c94a 87The natural logarithm of @code{2}.
55c14926 88@item M_LN10
7a68c94a 89The natural logarithm of @code{10}.
55c14926 90@item M_PI
04b9968b 91Pi, the ratio of a circle's circumference to its diameter.
55c14926 92@item M_PI_2
7a68c94a 93Pi divided by two.
55c14926 94@item M_PI_4
7a68c94a 95Pi divided by four.
55c14926 96@item M_1_PI
7a68c94a 97The reciprocal of pi (1/pi)
55c14926 98@item M_2_PI
7a68c94a 99Two times the reciprocal of pi.
55c14926 100@item M_2_SQRTPI
7a68c94a 101Two times the reciprocal of the square root of pi.
55c14926 102@item M_SQRT2
7a68c94a 103The square root of two.
55c14926 104@item M_SQRT1_2
7a68c94a 105The reciprocal of the square root of two (also the square root of 1/2).
55c14926
UD
106@end vtable
107
7a68c94a 108These constants come from the Unix98 standard and were also available in
04b9968b 1094.4BSD; therefore they are only defined if @code{_BSD_SOURCE} or
7a68c94a
UD
110@code{_XOPEN_SOURCE=500}, or a more general feature select macro, is
111defined. The default set of features includes these constants.
112@xref{Feature Test Macros}.
113
1f77f049
JM
114All values are of type @code{double}. As an extension, @theglibc{}
115also defines these constants with type @code{long double}. The
7a68c94a
UD
116@code{long double} macros have a lowercase @samp{l} appended to their
117names: @code{M_El}, @code{M_PIl}, and so forth. These are only
118available if @code{_GNU_SOURCE} is defined.
55c14926
UD
119
120@vindex PI
121@emph{Note:} Some programs use a constant named @code{PI} which has the
7a68c94a
UD
122same value as @code{M_PI}. This constant is not standard; it may have
123appeared in some old AT&T headers, and is mentioned in Stroustrup's book
1f77f049 124on C++. It infringes on the user's name space, so @theglibc{}
7a68c94a
UD
125does not define it. Fixing programs written to expect it is simple:
126replace @code{PI} with @code{M_PI} throughout, or put @samp{-DPI=M_PI}
127on the compiler command line.
61eb22d3 128
28f540f4
RM
129@node Trig Functions
130@section Trigonometric Functions
131@cindex trigonometric functions
132
133These are the familiar @code{sin}, @code{cos}, and @code{tan} functions.
134The arguments to all of these functions are in units of radians; recall
135that pi radians equals 180 degrees.
136
137@cindex pi (trigonometric constant)
7a68c94a
UD
138The math library normally defines @code{M_PI} to a @code{double}
139approximation of pi. If strict ISO and/or POSIX compliance
140are requested this constant is not defined, but you can easily define it
141yourself:
28f540f4
RM
142
143@smallexample
b4012b75 144#define M_PI 3.14159265358979323846264338327
28f540f4
RM
145@end smallexample
146
147@noindent
148You can also compute the value of pi with the expression @code{acos
149(-1.0)}.
150
28f540f4 151@comment math.h
f65fd747 152@comment ISO
28f540f4 153@deftypefun double sin (double @var{x})
4260bc74
UD
154@comment math.h
155@comment ISO
779ae82e 156@deftypefunx float sinf (float @var{x})
4260bc74
UD
157@comment math.h
158@comment ISO
779ae82e 159@deftypefunx {long double} sinl (long double @var{x})
27aaa791 160@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
b4012b75 161These functions return the sine of @var{x}, where @var{x} is given in
28f540f4
RM
162radians. The return value is in the range @code{-1} to @code{1}.
163@end deftypefun
164
165@comment math.h
f65fd747 166@comment ISO
28f540f4 167@deftypefun double cos (double @var{x})
4260bc74
UD
168@comment math.h
169@comment ISO
779ae82e 170@deftypefunx float cosf (float @var{x})
4260bc74
UD
171@comment math.h
172@comment ISO
779ae82e 173@deftypefunx {long double} cosl (long double @var{x})
27aaa791 174@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
b4012b75 175These functions return the cosine of @var{x}, where @var{x} is given in
28f540f4
RM
176radians. The return value is in the range @code{-1} to @code{1}.
177@end deftypefun
178
179@comment math.h
f65fd747 180@comment ISO
28f540f4 181@deftypefun double tan (double @var{x})
4260bc74
UD
182@comment math.h
183@comment ISO
779ae82e 184@deftypefunx float tanf (float @var{x})
4260bc74
UD
185@comment math.h
186@comment ISO
779ae82e 187@deftypefunx {long double} tanl (long double @var{x})
27aaa791 188@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
b4012b75 189These functions return the tangent of @var{x}, where @var{x} is given in
28f540f4
RM
190radians.
191
28f540f4
RM
192Mathematically, the tangent function has singularities at odd multiples
193of pi/2. If the argument @var{x} is too close to one of these
7a68c94a 194singularities, @code{tan} will signal overflow.
28f540f4
RM
195@end deftypefun
196
7a68c94a
UD
197In many applications where @code{sin} and @code{cos} are used, the sine
198and cosine of the same angle are needed at the same time. It is more
199efficient to compute them simultaneously, so the library provides a
200function to do that.
b4012b75
UD
201
202@comment math.h
203@comment GNU
204@deftypefun void sincos (double @var{x}, double *@var{sinx}, double *@var{cosx})
4260bc74
UD
205@comment math.h
206@comment GNU
779ae82e 207@deftypefunx void sincosf (float @var{x}, float *@var{sinx}, float *@var{cosx})
4260bc74
UD
208@comment math.h
209@comment GNU
779ae82e 210@deftypefunx void sincosl (long double @var{x}, long double *@var{sinx}, long double *@var{cosx})
27aaa791 211@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
b4012b75
UD
212These functions return the sine of @var{x} in @code{*@var{sinx}} and the
213cosine of @var{x} in @code{*@var{cos}}, where @var{x} is given in
214radians. Both values, @code{*@var{sinx}} and @code{*@var{cosx}}, are in
215the range of @code{-1} to @code{1}.
ca34d7a7 216
7a68c94a
UD
217This function is a GNU extension. Portable programs should be prepared
218to cope with its absence.
b4012b75
UD
219@end deftypefun
220
221@cindex complex trigonometric functions
222
ec751a23 223@w{ISO C99} defines variants of the trig functions which work on
1f77f049 224complex numbers. @Theglibc{} provides these functions, but they
7a68c94a
UD
225are only useful if your compiler supports the new complex types defined
226by the standard.
ec751a23 227@c XXX Change this when gcc is fixed. -zw
7a68c94a
UD
228(As of this writing GCC supports complex numbers, but there are bugs in
229the implementation.)
b4012b75
UD
230
231@comment complex.h
232@comment ISO
233@deftypefun {complex double} csin (complex double @var{z})
4260bc74
UD
234@comment complex.h
235@comment ISO
779ae82e 236@deftypefunx {complex float} csinf (complex float @var{z})
4260bc74
UD
237@comment complex.h
238@comment ISO
779ae82e 239@deftypefunx {complex long double} csinl (complex long double @var{z})
27aaa791
AO
240@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
241@c There are calls to nan* that could trigger @mtslocale if they didn't get
242@c empty strings.
7a68c94a 243These functions return the complex sine of @var{z}.
b4012b75
UD
244The mathematical definition of the complex sine is
245
4c78249d 246@ifnottex
779ae82e 247@math{sin (z) = 1/(2*i) * (exp (z*i) - exp (-z*i))}.
4c78249d 248@end ifnottex
779ae82e
UD
249@tex
250$$\sin(z) = {1\over 2i} (e^{zi} - e^{-zi})$$
251@end tex
b4012b75
UD
252@end deftypefun
253
254@comment complex.h
255@comment ISO
256@deftypefun {complex double} ccos (complex double @var{z})
4260bc74
UD
257@comment complex.h
258@comment ISO
779ae82e 259@deftypefunx {complex float} ccosf (complex float @var{z})
4260bc74
UD
260@comment complex.h
261@comment ISO
779ae82e 262@deftypefunx {complex long double} ccosl (complex long double @var{z})
27aaa791 263@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
7a68c94a 264These functions return the complex cosine of @var{z}.
b4012b75
UD
265The mathematical definition of the complex cosine is
266
4c78249d 267@ifnottex
779ae82e 268@math{cos (z) = 1/2 * (exp (z*i) + exp (-z*i))}
4c78249d 269@end ifnottex
779ae82e
UD
270@tex
271$$\cos(z) = {1\over 2} (e^{zi} + e^{-zi})$$
272@end tex
b4012b75
UD
273@end deftypefun
274
275@comment complex.h
276@comment ISO
277@deftypefun {complex double} ctan (complex double @var{z})
4260bc74
UD
278@comment complex.h
279@comment ISO
779ae82e 280@deftypefunx {complex float} ctanf (complex float @var{z})
4260bc74
UD
281@comment complex.h
282@comment ISO
779ae82e 283@deftypefunx {complex long double} ctanl (complex long double @var{z})
27aaa791 284@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
7a68c94a 285These functions return the complex tangent of @var{z}.
b4012b75
UD
286The mathematical definition of the complex tangent is
287
4c78249d 288@ifnottex
7a68c94a 289@math{tan (z) = -i * (exp (z*i) - exp (-z*i)) / (exp (z*i) + exp (-z*i))}
4c78249d 290@end ifnottex
779ae82e 291@tex
7a68c94a 292$$\tan(z) = -i \cdot {e^{zi} - e^{-zi}\over e^{zi} + e^{-zi}}$$
779ae82e 293@end tex
7a68c94a
UD
294
295@noindent
296The complex tangent has poles at @math{pi/2 + 2n}, where @math{n} is an
297integer. @code{ctan} may signal overflow if @var{z} is too close to a
298pole.
b4012b75
UD
299@end deftypefun
300
28f540f4
RM
301
302@node Inverse Trig Functions
303@section Inverse Trigonometric Functions
6d52618b 304@cindex inverse trigonometric functions
28f540f4
RM
305
306These are the usual arc sine, arc cosine and arc tangent functions,
04b9968b 307which are the inverses of the sine, cosine and tangent functions
28f540f4
RM
308respectively.
309
310@comment math.h
f65fd747 311@comment ISO
28f540f4 312@deftypefun double asin (double @var{x})
4260bc74
UD
313@comment math.h
314@comment ISO
779ae82e 315@deftypefunx float asinf (float @var{x})
4260bc74
UD
316@comment math.h
317@comment ISO
779ae82e 318@deftypefunx {long double} asinl (long double @var{x})
27aaa791 319@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
b4012b75 320These functions compute the arc sine of @var{x}---that is, the value whose
28f540f4
RM
321sine is @var{x}. The value is in units of radians. Mathematically,
322there are infinitely many such values; the one actually returned is the
323one between @code{-pi/2} and @code{pi/2} (inclusive).
324
7a68c94a
UD
325The arc sine function is defined mathematically only
326over the domain @code{-1} to @code{1}. If @var{x} is outside the
327domain, @code{asin} signals a domain error.
28f540f4
RM
328@end deftypefun
329
330@comment math.h
f65fd747 331@comment ISO
28f540f4 332@deftypefun double acos (double @var{x})
4260bc74
UD
333@comment math.h
334@comment ISO
779ae82e 335@deftypefunx float acosf (float @var{x})
4260bc74
UD
336@comment math.h
337@comment ISO
779ae82e 338@deftypefunx {long double} acosl (long double @var{x})
27aaa791 339@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
b4012b75 340These functions compute the arc cosine of @var{x}---that is, the value
28f540f4
RM
341whose cosine is @var{x}. The value is in units of radians.
342Mathematically, there are infinitely many such values; the one actually
343returned is the one between @code{0} and @code{pi} (inclusive).
344
7a68c94a
UD
345The arc cosine function is defined mathematically only
346over the domain @code{-1} to @code{1}. If @var{x} is outside the
347domain, @code{acos} signals a domain error.
28f540f4
RM
348@end deftypefun
349
28f540f4 350@comment math.h
f65fd747 351@comment ISO
28f540f4 352@deftypefun double atan (double @var{x})
4260bc74
UD
353@comment math.h
354@comment ISO
779ae82e 355@deftypefunx float atanf (float @var{x})
4260bc74
UD
356@comment math.h
357@comment ISO
779ae82e 358@deftypefunx {long double} atanl (long double @var{x})
27aaa791 359@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
b4012b75 360These functions compute the arc tangent of @var{x}---that is, the value
28f540f4
RM
361whose tangent is @var{x}. The value is in units of radians.
362Mathematically, there are infinitely many such values; the one actually
7a68c94a 363returned is the one between @code{-pi/2} and @code{pi/2} (inclusive).
28f540f4
RM
364@end deftypefun
365
366@comment math.h
f65fd747 367@comment ISO
28f540f4 368@deftypefun double atan2 (double @var{y}, double @var{x})
4260bc74
UD
369@comment math.h
370@comment ISO
779ae82e 371@deftypefunx float atan2f (float @var{y}, float @var{x})
4260bc74
UD
372@comment math.h
373@comment ISO
779ae82e 374@deftypefunx {long double} atan2l (long double @var{y}, long double @var{x})
27aaa791 375@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
7a68c94a
UD
376This function computes the arc tangent of @var{y}/@var{x}, but the signs
377of both arguments are used to determine the quadrant of the result, and
378@var{x} is permitted to be zero. The return value is given in radians
379and is in the range @code{-pi} to @code{pi}, inclusive.
28f540f4
RM
380
381If @var{x} and @var{y} are coordinates of a point in the plane,
382@code{atan2} returns the signed angle between the line from the origin
383to that point and the x-axis. Thus, @code{atan2} is useful for
384converting Cartesian coordinates to polar coordinates. (To compute the
385radial coordinate, use @code{hypot}; see @ref{Exponents and
386Logarithms}.)
387
7a68c94a
UD
388@c This is experimentally true. Should it be so? -zw
389If both @var{x} and @var{y} are zero, @code{atan2} returns zero.
28f540f4
RM
390@end deftypefun
391
b4012b75 392@cindex inverse complex trigonometric functions
ec751a23 393@w{ISO C99} defines complex versions of the inverse trig functions.
b4012b75
UD
394
395@comment complex.h
396@comment ISO
397@deftypefun {complex double} casin (complex double @var{z})
4260bc74
UD
398@comment complex.h
399@comment ISO
779ae82e 400@deftypefunx {complex float} casinf (complex float @var{z})
4260bc74
UD
401@comment complex.h
402@comment ISO
779ae82e 403@deftypefunx {complex long double} casinl (complex long double @var{z})
27aaa791 404@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
b4012b75 405These functions compute the complex arc sine of @var{z}---that is, the
7a68c94a 406value whose sine is @var{z}. The value returned is in radians.
b4012b75 407
7a68c94a
UD
408Unlike the real-valued functions, @code{casin} is defined for all
409values of @var{z}.
b4012b75
UD
410@end deftypefun
411
412@comment complex.h
413@comment ISO
414@deftypefun {complex double} cacos (complex double @var{z})
4260bc74
UD
415@comment complex.h
416@comment ISO
779ae82e 417@deftypefunx {complex float} cacosf (complex float @var{z})
4260bc74
UD
418@comment complex.h
419@comment ISO
779ae82e 420@deftypefunx {complex long double} cacosl (complex long double @var{z})
27aaa791 421@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
b4012b75 422These functions compute the complex arc cosine of @var{z}---that is, the
7a68c94a 423value whose cosine is @var{z}. The value returned is in radians.
b4012b75 424
7a68c94a
UD
425Unlike the real-valued functions, @code{cacos} is defined for all
426values of @var{z}.
b4012b75
UD
427@end deftypefun
428
429
430@comment complex.h
431@comment ISO
432@deftypefun {complex double} catan (complex double @var{z})
4260bc74
UD
433@comment complex.h
434@comment ISO
779ae82e 435@deftypefunx {complex float} catanf (complex float @var{z})
4260bc74
UD
436@comment complex.h
437@comment ISO
779ae82e 438@deftypefunx {complex long double} catanl (complex long double @var{z})
27aaa791 439@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
b4012b75
UD
440These functions compute the complex arc tangent of @var{z}---that is,
441the value whose tangent is @var{z}. The value is in units of radians.
442@end deftypefun
443
28f540f4
RM
444
445@node Exponents and Logarithms
446@section Exponentiation and Logarithms
447@cindex exponentiation functions
448@cindex power functions
449@cindex logarithm functions
450
451@comment math.h
f65fd747 452@comment ISO
28f540f4 453@deftypefun double exp (double @var{x})
4260bc74
UD
454@comment math.h
455@comment ISO
779ae82e 456@deftypefunx float expf (float @var{x})
4260bc74
UD
457@comment math.h
458@comment ISO
779ae82e 459@deftypefunx {long double} expl (long double @var{x})
27aaa791 460@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
7a68c94a
UD
461These functions compute @code{e} (the base of natural logarithms) raised
462to the power @var{x}.
28f540f4 463
7a68c94a
UD
464If the magnitude of the result is too large to be representable,
465@code{exp} signals overflow.
28f540f4
RM
466@end deftypefun
467
b4012b75
UD
468@comment math.h
469@comment ISO
04a96fd4 470@deftypefun double exp2 (double @var{x})
4260bc74
UD
471@comment math.h
472@comment ISO
04a96fd4 473@deftypefunx float exp2f (float @var{x})
4260bc74
UD
474@comment math.h
475@comment ISO
04a96fd4 476@deftypefunx {long double} exp2l (long double @var{x})
27aaa791 477@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
7a68c94a 478These functions compute @code{2} raised to the power @var{x}.
04a96fd4 479Mathematically, @code{exp2 (x)} is the same as @code{exp (x * log (2))}.
b4012b75
UD
480@end deftypefun
481
482@comment math.h
04a96fd4
UD
483@comment GNU
484@deftypefun double exp10 (double @var{x})
4260bc74
UD
485@comment math.h
486@comment GNU
04a96fd4 487@deftypefunx float exp10f (float @var{x})
4260bc74
UD
488@comment math.h
489@comment GNU
04a96fd4 490@deftypefunx {long double} exp10l (long double @var{x})
4260bc74
UD
491@comment math.h
492@comment GNU
04a96fd4 493@deftypefunx double pow10 (double @var{x})
4260bc74
UD
494@comment math.h
495@comment GNU
04a96fd4 496@deftypefunx float pow10f (float @var{x})
4260bc74
UD
497@comment math.h
498@comment GNU
04a96fd4 499@deftypefunx {long double} pow10l (long double @var{x})
27aaa791 500@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
7a68c94a
UD
501These functions compute @code{10} raised to the power @var{x}.
502Mathematically, @code{exp10 (x)} is the same as @code{exp (x * log (10))}.
b4012b75 503
7a68c94a
UD
504These functions are GNU extensions. The name @code{exp10} is
505preferred, since it is analogous to @code{exp} and @code{exp2}.
b4012b75
UD
506@end deftypefun
507
508
28f540f4 509@comment math.h
f65fd747 510@comment ISO
28f540f4 511@deftypefun double log (double @var{x})
4260bc74
UD
512@comment math.h
513@comment ISO
f2ea0f5b 514@deftypefunx float logf (float @var{x})
4260bc74
UD
515@comment math.h
516@comment ISO
779ae82e 517@deftypefunx {long double} logl (long double @var{x})
27aaa791 518@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
7a68c94a 519These functions compute the natural logarithm of @var{x}. @code{exp (log
28f540f4
RM
520(@var{x}))} equals @var{x}, exactly in mathematics and approximately in
521C.
522
7a68c94a
UD
523If @var{x} is negative, @code{log} signals a domain error. If @var{x}
524is zero, it returns negative infinity; if @var{x} is too close to zero,
525it may signal overflow.
28f540f4
RM
526@end deftypefun
527
528@comment math.h
f65fd747 529@comment ISO
28f540f4 530@deftypefun double log10 (double @var{x})
4260bc74
UD
531@comment math.h
532@comment ISO
779ae82e 533@deftypefunx float log10f (float @var{x})
4260bc74
UD
534@comment math.h
535@comment ISO
779ae82e 536@deftypefunx {long double} log10l (long double @var{x})
27aaa791 537@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
7a68c94a 538These functions return the base-10 logarithm of @var{x}.
28f540f4 539@code{log10 (@var{x})} equals @code{log (@var{x}) / log (10)}.
7a68c94a 540
28f540f4
RM
541@end deftypefun
542
b4012b75
UD
543@comment math.h
544@comment ISO
545@deftypefun double log2 (double @var{x})
4260bc74
UD
546@comment math.h
547@comment ISO
779ae82e 548@deftypefunx float log2f (float @var{x})
4260bc74
UD
549@comment math.h
550@comment ISO
779ae82e 551@deftypefunx {long double} log2l (long double @var{x})
27aaa791 552@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
7a68c94a 553These functions return the base-2 logarithm of @var{x}.
b4012b75
UD
554@code{log2 (@var{x})} equals @code{log (@var{x}) / log (2)}.
555@end deftypefun
556
55c14926
UD
557@comment math.h
558@comment ISO
559@deftypefun double logb (double @var{x})
4260bc74
UD
560@comment math.h
561@comment ISO
55c14926 562@deftypefunx float logbf (float @var{x})
4260bc74
UD
563@comment math.h
564@comment ISO
55c14926 565@deftypefunx {long double} logbl (long double @var{x})
27aaa791 566@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
55c14926 567These functions extract the exponent of @var{x} and return it as a
7a68c94a
UD
568floating-point value. If @code{FLT_RADIX} is two, @code{logb} is equal
569to @code{floor (log2 (x))}, except it's probably faster.
55c14926 570
04b9968b 571If @var{x} is de-normalized, @code{logb} returns the exponent @var{x}
7a68c94a
UD
572would have if it were normalized. If @var{x} is infinity (positive or
573negative), @code{logb} returns @math{@infinity{}}. If @var{x} is zero,
574@code{logb} returns @math{@infinity{}}. It does not signal.
55c14926
UD
575@end deftypefun
576
577@comment math.h
578@comment ISO
579@deftypefun int ilogb (double @var{x})
4260bc74
UD
580@comment math.h
581@comment ISO
55c14926 582@deftypefunx int ilogbf (float @var{x})
4260bc74
UD
583@comment math.h
584@comment ISO
55c14926 585@deftypefunx int ilogbl (long double @var{x})
27aaa791 586@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
55c14926 587These functions are equivalent to the corresponding @code{logb}
7a68c94a
UD
588functions except that they return signed integer values.
589@end deftypefun
590
591@noindent
592Since integers cannot represent infinity and NaN, @code{ilogb} instead
593returns an integer that can't be the exponent of a normal floating-point
594number. @file{math.h} defines constants so you can check for this.
595
596@comment math.h
597@comment ISO
598@deftypevr Macro int FP_ILOGB0
599@code{ilogb} returns this value if its argument is @code{0}. The
600numeric value is either @code{INT_MIN} or @code{-INT_MAX}.
601
ec751a23 602This macro is defined in @w{ISO C99}.
7a68c94a
UD
603@end deftypevr
604
605@comment math.h
606@comment ISO
607@deftypevr Macro int FP_ILOGBNAN
608@code{ilogb} returns this value if its argument is @code{NaN}. The
609numeric value is either @code{INT_MIN} or @code{INT_MAX}.
610
ec751a23 611This macro is defined in @w{ISO C99}.
7a68c94a
UD
612@end deftypevr
613
614These values are system specific. They might even be the same. The
615proper way to test the result of @code{ilogb} is as follows:
55c14926
UD
616
617@smallexample
618i = ilogb (f);
619if (i == FP_ILOGB0 || i == FP_ILOGBNAN)
620 @{
621 if (isnan (f))
622 @{
623 /* @r{Handle NaN.} */
624 @}
625 else if (f == 0.0)
626 @{
627 /* @r{Handle 0.0.} */
628 @}
629 else
630 @{
631 /* @r{Some other value with large exponent,}
632 @r{perhaps +Inf.} */
633 @}
634 @}
635@end smallexample
636
28f540f4 637@comment math.h
f65fd747 638@comment ISO
28f540f4 639@deftypefun double pow (double @var{base}, double @var{power})
4260bc74
UD
640@comment math.h
641@comment ISO
779ae82e 642@deftypefunx float powf (float @var{base}, float @var{power})
4260bc74
UD
643@comment math.h
644@comment ISO
779ae82e 645@deftypefunx {long double} powl (long double @var{base}, long double @var{power})
27aaa791 646@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
b4012b75 647These are general exponentiation functions, returning @var{base} raised
28f540f4
RM
648to @var{power}.
649
7a68c94a
UD
650Mathematically, @code{pow} would return a complex number when @var{base}
651is negative and @var{power} is not an integral value. @code{pow} can't
652do that, so instead it signals a domain error. @code{pow} may also
653underflow or overflow the destination type.
28f540f4
RM
654@end deftypefun
655
656@cindex square root function
657@comment math.h
f65fd747 658@comment ISO
28f540f4 659@deftypefun double sqrt (double @var{x})
4260bc74
UD
660@comment math.h
661@comment ISO
779ae82e 662@deftypefunx float sqrtf (float @var{x})
4260bc74
UD
663@comment math.h
664@comment ISO
779ae82e 665@deftypefunx {long double} sqrtl (long double @var{x})
27aaa791 666@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
b4012b75 667These functions return the nonnegative square root of @var{x}.
28f540f4 668
7a68c94a
UD
669If @var{x} is negative, @code{sqrt} signals a domain error.
670Mathematically, it should return a complex number.
28f540f4
RM
671@end deftypefun
672
673@cindex cube root function
674@comment math.h
675@comment BSD
676@deftypefun double cbrt (double @var{x})
4260bc74
UD
677@comment math.h
678@comment BSD
779ae82e 679@deftypefunx float cbrtf (float @var{x})
4260bc74
UD
680@comment math.h
681@comment BSD
779ae82e 682@deftypefunx {long double} cbrtl (long double @var{x})
27aaa791 683@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
b4012b75 684These functions return the cube root of @var{x}. They cannot
28f540f4
RM
685fail; every representable real value has a representable real cube root.
686@end deftypefun
687
688@comment math.h
b4012b75 689@comment ISO
28f540f4 690@deftypefun double hypot (double @var{x}, double @var{y})
4260bc74
UD
691@comment math.h
692@comment ISO
779ae82e 693@deftypefunx float hypotf (float @var{x}, float @var{y})
4260bc74
UD
694@comment math.h
695@comment ISO
779ae82e 696@deftypefunx {long double} hypotl (long double @var{x}, long double @var{y})
27aaa791 697@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
b4012b75 698These functions return @code{sqrt (@var{x}*@var{x} +
7a68c94a 699@var{y}*@var{y})}. This is the length of the hypotenuse of a right
28f540f4 700triangle with sides of length @var{x} and @var{y}, or the distance
7a68c94a
UD
701of the point (@var{x}, @var{y}) from the origin. Using this function
702instead of the direct formula is wise, since the error is
b4012b75 703much smaller. See also the function @code{cabs} in @ref{Absolute Value}.
28f540f4
RM
704@end deftypefun
705
706@comment math.h
b4012b75 707@comment ISO
28f540f4 708@deftypefun double expm1 (double @var{x})
4260bc74
UD
709@comment math.h
710@comment ISO
779ae82e 711@deftypefunx float expm1f (float @var{x})
4260bc74
UD
712@comment math.h
713@comment ISO
779ae82e 714@deftypefunx {long double} expm1l (long double @var{x})
27aaa791 715@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
b4012b75 716These functions return a value equivalent to @code{exp (@var{x}) - 1}.
7a68c94a 717They are computed in a way that is accurate even if @var{x} is
04b9968b 718near zero---a case where @code{exp (@var{x}) - 1} would be inaccurate owing
28f540f4
RM
719to subtraction of two numbers that are nearly equal.
720@end deftypefun
721
722@comment math.h
b4012b75 723@comment ISO
28f540f4 724@deftypefun double log1p (double @var{x})
4260bc74
UD
725@comment math.h
726@comment ISO
779ae82e 727@deftypefunx float log1pf (float @var{x})
4260bc74
UD
728@comment math.h
729@comment ISO
779ae82e 730@deftypefunx {long double} log1pl (long double @var{x})
27aaa791 731@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
7a68c94a
UD
732These functions returns a value equivalent to @w{@code{log (1 + @var{x})}}.
733They are computed in a way that is accurate even if @var{x} is
28f540f4
RM
734near zero.
735@end deftypefun
736
b4012b75
UD
737@cindex complex exponentiation functions
738@cindex complex logarithm functions
739
ec751a23 740@w{ISO C99} defines complex variants of some of the exponentiation and
7a68c94a 741logarithm functions.
b4012b75
UD
742
743@comment complex.h
744@comment ISO
745@deftypefun {complex double} cexp (complex double @var{z})
4260bc74
UD
746@comment complex.h
747@comment ISO
779ae82e 748@deftypefunx {complex float} cexpf (complex float @var{z})
4260bc74
UD
749@comment complex.h
750@comment ISO
779ae82e 751@deftypefunx {complex long double} cexpl (complex long double @var{z})
27aaa791 752@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
7a68c94a
UD
753These functions return @code{e} (the base of natural
754logarithms) raised to the power of @var{z}.
04b9968b 755Mathematically, this corresponds to the value
b4012b75 756
4c78249d 757@ifnottex
779ae82e 758@math{exp (z) = exp (creal (z)) * (cos (cimag (z)) + I * sin (cimag (z)))}
4c78249d 759@end ifnottex
779ae82e 760@tex
7a68c94a 761$$\exp(z) = e^z = e^{{\rm Re}\,z} (\cos ({\rm Im}\,z) + i \sin ({\rm Im}\,z))$$
779ae82e 762@end tex
b4012b75
UD
763@end deftypefun
764
765@comment complex.h
766@comment ISO
767@deftypefun {complex double} clog (complex double @var{z})
4260bc74
UD
768@comment complex.h
769@comment ISO
779ae82e 770@deftypefunx {complex float} clogf (complex float @var{z})
4260bc74
UD
771@comment complex.h
772@comment ISO
779ae82e 773@deftypefunx {complex long double} clogl (complex long double @var{z})
27aaa791 774@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
7a68c94a 775These functions return the natural logarithm of @var{z}.
04b9968b 776Mathematically, this corresponds to the value
b4012b75 777
4c78249d 778@ifnottex
779ae82e 779@math{log (z) = log (cabs (z)) + I * carg (z)}
4c78249d 780@end ifnottex
779ae82e 781@tex
7a68c94a 782$$\log(z) = \log |z| + i \arg z$$
779ae82e 783@end tex
7a68c94a
UD
784
785@noindent
786@code{clog} has a pole at 0, and will signal overflow if @var{z} equals
787or is very close to 0. It is well-defined for all other values of
788@var{z}.
b4012b75
UD
789@end deftypefun
790
dfd2257a
UD
791
792@comment complex.h
793@comment GNU
794@deftypefun {complex double} clog10 (complex double @var{z})
4260bc74
UD
795@comment complex.h
796@comment GNU
dfd2257a 797@deftypefunx {complex float} clog10f (complex float @var{z})
4260bc74
UD
798@comment complex.h
799@comment GNU
dfd2257a 800@deftypefunx {complex long double} clog10l (complex long double @var{z})
27aaa791 801@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
dfd2257a 802These functions return the base 10 logarithm of the complex value
04b9968b 803@var{z}. Mathematically, this corresponds to the value
dfd2257a 804
4c78249d 805@ifnottex
dfd2257a 806@math{log (z) = log10 (cabs (z)) + I * carg (z)}
4c78249d 807@end ifnottex
dfd2257a 808@tex
7a68c94a 809$$\log_{10}(z) = \log_{10}|z| + i \arg z$$
dfd2257a 810@end tex
dfd2257a 811
7a68c94a 812These functions are GNU extensions.
dfd2257a
UD
813@end deftypefun
814
b4012b75
UD
815@comment complex.h
816@comment ISO
817@deftypefun {complex double} csqrt (complex double @var{z})
4260bc74
UD
818@comment complex.h
819@comment ISO
779ae82e 820@deftypefunx {complex float} csqrtf (complex float @var{z})
4260bc74
UD
821@comment complex.h
822@comment ISO
779ae82e 823@deftypefunx {complex long double} csqrtl (complex long double @var{z})
27aaa791 824@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
7a68c94a
UD
825These functions return the complex square root of the argument @var{z}. Unlike
826the real-valued functions, they are defined for all values of @var{z}.
b4012b75
UD
827@end deftypefun
828
829@comment complex.h
830@comment ISO
831@deftypefun {complex double} cpow (complex double @var{base}, complex double @var{power})
4260bc74
UD
832@comment complex.h
833@comment ISO
779ae82e 834@deftypefunx {complex float} cpowf (complex float @var{base}, complex float @var{power})
4260bc74
UD
835@comment complex.h
836@comment ISO
779ae82e 837@deftypefunx {complex long double} cpowl (complex long double @var{base}, complex long double @var{power})
27aaa791 838@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
7a68c94a
UD
839These functions return @var{base} raised to the power of
840@var{power}. This is equivalent to @w{@code{cexp (y * clog (x))}}
b4012b75
UD
841@end deftypefun
842
28f540f4
RM
843@node Hyperbolic Functions
844@section Hyperbolic Functions
845@cindex hyperbolic functions
846
847The functions in this section are related to the exponential functions;
848see @ref{Exponents and Logarithms}.
849
850@comment math.h
f65fd747 851@comment ISO
28f540f4 852@deftypefun double sinh (double @var{x})
4260bc74
UD
853@comment math.h
854@comment ISO
779ae82e 855@deftypefunx float sinhf (float @var{x})
4260bc74
UD
856@comment math.h
857@comment ISO
779ae82e 858@deftypefunx {long double} sinhl (long double @var{x})
27aaa791 859@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
b4012b75 860These functions return the hyperbolic sine of @var{x}, defined
7a68c94a
UD
861mathematically as @w{@code{(exp (@var{x}) - exp (-@var{x})) / 2}}. They
862may signal overflow if @var{x} is too large.
28f540f4
RM
863@end deftypefun
864
865@comment math.h
f65fd747 866@comment ISO
28f540f4 867@deftypefun double cosh (double @var{x})
4260bc74
UD
868@comment math.h
869@comment ISO
779ae82e 870@deftypefunx float coshf (float @var{x})
4260bc74
UD
871@comment math.h
872@comment ISO
779ae82e 873@deftypefunx {long double} coshl (long double @var{x})
27aaa791 874@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
b4012b75
UD
875These function return the hyperbolic cosine of @var{x},
876defined mathematically as @w{@code{(exp (@var{x}) + exp (-@var{x})) / 2}}.
7a68c94a 877They may signal overflow if @var{x} is too large.
28f540f4
RM
878@end deftypefun
879
880@comment math.h
f65fd747 881@comment ISO
28f540f4 882@deftypefun double tanh (double @var{x})
4260bc74
UD
883@comment math.h
884@comment ISO
779ae82e 885@deftypefunx float tanhf (float @var{x})
4260bc74
UD
886@comment math.h
887@comment ISO
779ae82e 888@deftypefunx {long double} tanhl (long double @var{x})
27aaa791 889@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
7a68c94a
UD
890These functions return the hyperbolic tangent of @var{x},
891defined mathematically as @w{@code{sinh (@var{x}) / cosh (@var{x})}}.
892They may signal overflow if @var{x} is too large.
28f540f4
RM
893@end deftypefun
894
b4012b75
UD
895@cindex hyperbolic functions
896
7a68c94a
UD
897There are counterparts for the hyperbolic functions which take
898complex arguments.
b4012b75
UD
899
900@comment complex.h
901@comment ISO
902@deftypefun {complex double} csinh (complex double @var{z})
4260bc74
UD
903@comment complex.h
904@comment ISO
779ae82e 905@deftypefunx {complex float} csinhf (complex float @var{z})
4260bc74
UD
906@comment complex.h
907@comment ISO
779ae82e 908@deftypefunx {complex long double} csinhl (complex long double @var{z})
27aaa791 909@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
b4012b75 910These functions return the complex hyperbolic sine of @var{z}, defined
7a68c94a 911mathematically as @w{@code{(exp (@var{z}) - exp (-@var{z})) / 2}}.
b4012b75
UD
912@end deftypefun
913
914@comment complex.h
915@comment ISO
916@deftypefun {complex double} ccosh (complex double @var{z})
4260bc74
UD
917@comment complex.h
918@comment ISO
779ae82e 919@deftypefunx {complex float} ccoshf (complex float @var{z})
4260bc74
UD
920@comment complex.h
921@comment ISO
779ae82e 922@deftypefunx {complex long double} ccoshl (complex long double @var{z})
27aaa791 923@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
b4012b75 924These functions return the complex hyperbolic cosine of @var{z}, defined
7a68c94a 925mathematically as @w{@code{(exp (@var{z}) + exp (-@var{z})) / 2}}.
b4012b75
UD
926@end deftypefun
927
928@comment complex.h
929@comment ISO
930@deftypefun {complex double} ctanh (complex double @var{z})
4260bc74
UD
931@comment complex.h
932@comment ISO
779ae82e 933@deftypefunx {complex float} ctanhf (complex float @var{z})
4260bc74
UD
934@comment complex.h
935@comment ISO
779ae82e 936@deftypefunx {complex long double} ctanhl (complex long double @var{z})
27aaa791 937@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
7a68c94a
UD
938These functions return the complex hyperbolic tangent of @var{z},
939defined mathematically as @w{@code{csinh (@var{z}) / ccosh (@var{z})}}.
b4012b75
UD
940@end deftypefun
941
942
28f540f4
RM
943@cindex inverse hyperbolic functions
944
945@comment math.h
b4012b75 946@comment ISO
28f540f4 947@deftypefun double asinh (double @var{x})
4260bc74
UD
948@comment math.h
949@comment ISO
779ae82e 950@deftypefunx float asinhf (float @var{x})
4260bc74
UD
951@comment math.h
952@comment ISO
779ae82e 953@deftypefunx {long double} asinhl (long double @var{x})
27aaa791 954@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
b4012b75 955These functions return the inverse hyperbolic sine of @var{x}---the
28f540f4
RM
956value whose hyperbolic sine is @var{x}.
957@end deftypefun
958
959@comment math.h
b4012b75 960@comment ISO
28f540f4 961@deftypefun double acosh (double @var{x})
4260bc74
UD
962@comment math.h
963@comment ISO
779ae82e 964@deftypefunx float acoshf (float @var{x})
4260bc74
UD
965@comment math.h
966@comment ISO
779ae82e 967@deftypefunx {long double} acoshl (long double @var{x})
27aaa791 968@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
b4012b75 969These functions return the inverse hyperbolic cosine of @var{x}---the
28f540f4 970value whose hyperbolic cosine is @var{x}. If @var{x} is less than
7a68c94a 971@code{1}, @code{acosh} signals a domain error.
28f540f4
RM
972@end deftypefun
973
974@comment math.h
b4012b75 975@comment ISO
28f540f4 976@deftypefun double atanh (double @var{x})
4260bc74
UD
977@comment math.h
978@comment ISO
779ae82e 979@deftypefunx float atanhf (float @var{x})
4260bc74
UD
980@comment math.h
981@comment ISO
779ae82e 982@deftypefunx {long double} atanhl (long double @var{x})
27aaa791 983@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
b4012b75 984These functions return the inverse hyperbolic tangent of @var{x}---the
28f540f4 985value whose hyperbolic tangent is @var{x}. If the absolute value of
7a68c94a
UD
986@var{x} is greater than @code{1}, @code{atanh} signals a domain error;
987if it is equal to 1, @code{atanh} returns infinity.
28f540f4
RM
988@end deftypefun
989
b4012b75
UD
990@cindex inverse complex hyperbolic functions
991
992@comment complex.h
993@comment ISO
994@deftypefun {complex double} casinh (complex double @var{z})
4260bc74
UD
995@comment complex.h
996@comment ISO
779ae82e 997@deftypefunx {complex float} casinhf (complex float @var{z})
4260bc74
UD
998@comment complex.h
999@comment ISO
779ae82e 1000@deftypefunx {complex long double} casinhl (complex long double @var{z})
27aaa791 1001@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
b4012b75
UD
1002These functions return the inverse complex hyperbolic sine of
1003@var{z}---the value whose complex hyperbolic sine is @var{z}.
1004@end deftypefun
1005
1006@comment complex.h
1007@comment ISO
1008@deftypefun {complex double} cacosh (complex double @var{z})
4260bc74
UD
1009@comment complex.h
1010@comment ISO
779ae82e 1011@deftypefunx {complex float} cacoshf (complex float @var{z})
4260bc74
UD
1012@comment complex.h
1013@comment ISO
779ae82e 1014@deftypefunx {complex long double} cacoshl (complex long double @var{z})
27aaa791 1015@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
b4012b75
UD
1016These functions return the inverse complex hyperbolic cosine of
1017@var{z}---the value whose complex hyperbolic cosine is @var{z}. Unlike
7a68c94a 1018the real-valued functions, there are no restrictions on the value of @var{z}.
b4012b75
UD
1019@end deftypefun
1020
1021@comment complex.h
1022@comment ISO
1023@deftypefun {complex double} catanh (complex double @var{z})
4260bc74
UD
1024@comment complex.h
1025@comment ISO
779ae82e 1026@deftypefunx {complex float} catanhf (complex float @var{z})
4260bc74
UD
1027@comment complex.h
1028@comment ISO
779ae82e 1029@deftypefunx {complex long double} catanhl (complex long double @var{z})
27aaa791 1030@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
b4012b75
UD
1031These functions return the inverse complex hyperbolic tangent of
1032@var{z}---the value whose complex hyperbolic tangent is @var{z}. Unlike
7a68c94a
UD
1033the real-valued functions, there are no restrictions on the value of
1034@var{z}.
b4012b75
UD
1035@end deftypefun
1036
7a68c94a
UD
1037@node Special Functions
1038@section Special Functions
1039@cindex special functions
1040@cindex Bessel functions
1041@cindex gamma function
1042
04b9968b 1043These are some more exotic mathematical functions which are sometimes
7a68c94a
UD
1044useful. Currently they only have real-valued versions.
1045
1046@comment math.h
1047@comment SVID
1048@deftypefun double erf (double @var{x})
4260bc74
UD
1049@comment math.h
1050@comment SVID
7a68c94a 1051@deftypefunx float erff (float @var{x})
4260bc74
UD
1052@comment math.h
1053@comment SVID
7a68c94a 1054@deftypefunx {long double} erfl (long double @var{x})
27aaa791 1055@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
7a68c94a
UD
1056@code{erf} returns the error function of @var{x}. The error
1057function is defined as
1058@tex
1059$$\hbox{erf}(x) = {2\over\sqrt{\pi}}\cdot\int_0^x e^{-t^2} \hbox{d}t$$
1060@end tex
1061@ifnottex
1062@smallexample
1063erf (x) = 2/sqrt(pi) * integral from 0 to x of exp(-t^2) dt
1064@end smallexample
1065@end ifnottex
1066@end deftypefun
1067
1068@comment math.h
1069@comment SVID
1070@deftypefun double erfc (double @var{x})
4260bc74
UD
1071@comment math.h
1072@comment SVID
7a68c94a 1073@deftypefunx float erfcf (float @var{x})
4260bc74
UD
1074@comment math.h
1075@comment SVID
7a68c94a 1076@deftypefunx {long double} erfcl (long double @var{x})
27aaa791 1077@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
7a68c94a
UD
1078@code{erfc} returns @code{1.0 - erf(@var{x})}, but computed in a
1079fashion that avoids round-off error when @var{x} is large.
1080@end deftypefun
1081
1082@comment math.h
1083@comment SVID
1084@deftypefun double lgamma (double @var{x})
4260bc74
UD
1085@comment math.h
1086@comment SVID
7a68c94a 1087@deftypefunx float lgammaf (float @var{x})
4260bc74
UD
1088@comment math.h
1089@comment SVID
7a68c94a 1090@deftypefunx {long double} lgammal (long double @var{x})
27aaa791 1091@safety{@prelim{}@mtunsafe{@mtasurace{:signgam}}@asunsafe{}@acsafe{}}
7a68c94a
UD
1092@code{lgamma} returns the natural logarithm of the absolute value of
1093the gamma function of @var{x}. The gamma function is defined as
1094@tex
1095$$\Gamma(x) = \int_0^\infty t^{x-1} e^{-t} \hbox{d}t$$
1096@end tex
1097@ifnottex
1098@smallexample
1099gamma (x) = integral from 0 to @infinity{} of t^(x-1) e^-t dt
1100@end smallexample
1101@end ifnottex
1102
1103@vindex signgam
1104The sign of the gamma function is stored in the global variable
1105@var{signgam}, which is declared in @file{math.h}. It is @code{1} if
04b9968b 1106the intermediate result was positive or zero, or @code{-1} if it was
7a68c94a
UD
1107negative.
1108
e852e889
UD
1109To compute the real gamma function you can use the @code{tgamma}
1110function or you can compute the values as follows:
7a68c94a
UD
1111@smallexample
1112lgam = lgamma(x);
1113gam = signgam*exp(lgam);
1114@end smallexample
1115
04b9968b 1116The gamma function has singularities at the non-positive integers.
7a68c94a
UD
1117@code{lgamma} will raise the zero divide exception if evaluated at a
1118singularity.
1119@end deftypefun
1120
1121@comment math.h
1122@comment XPG
07435eb4 1123@deftypefun double lgamma_r (double @var{x}, int *@var{signp})
4260bc74
UD
1124@comment math.h
1125@comment XPG
07435eb4 1126@deftypefunx float lgammaf_r (float @var{x}, int *@var{signp})
4260bc74
UD
1127@comment math.h
1128@comment XPG
07435eb4 1129@deftypefunx {long double} lgammal_r (long double @var{x}, int *@var{signp})
27aaa791 1130@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
7a68c94a
UD
1131@code{lgamma_r} is just like @code{lgamma}, but it stores the sign of
1132the intermediate result in the variable pointed to by @var{signp}
04b9968b 1133instead of in the @var{signgam} global. This means it is reentrant.
7a68c94a
UD
1134@end deftypefun
1135
7a68c94a
UD
1136@comment math.h
1137@comment SVID
1138@deftypefun double gamma (double @var{x})
4260bc74
UD
1139@comment math.h
1140@comment SVID
7a68c94a 1141@deftypefunx float gammaf (float @var{x})
4260bc74
UD
1142@comment math.h
1143@comment SVID
7a68c94a 1144@deftypefunx {long double} gammal (long double @var{x})
27aaa791 1145@safety{@prelim{}@mtunsafe{@mtasurace{:signgam}}@asunsafe{}@acsafe{}}
e852e889
UD
1146These functions exist for compatibility reasons. They are equivalent to
1147@code{lgamma} etc. It is better to use @code{lgamma} since for one the
04b9968b 1148name reflects better the actual computation, moreover @code{lgamma} is
ec751a23 1149standardized in @w{ISO C99} while @code{gamma} is not.
e852e889
UD
1150@end deftypefun
1151
1152@comment math.h
ec751a23 1153@comment XPG, ISO
e852e889 1154@deftypefun double tgamma (double @var{x})
4260bc74 1155@comment math.h
ec751a23 1156@comment XPG, ISO
e852e889 1157@deftypefunx float tgammaf (float @var{x})
4260bc74 1158@comment math.h
ec751a23 1159@comment XPG, ISO
e852e889 1160@deftypefunx {long double} tgammal (long double @var{x})
27aaa791 1161@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
e852e889
UD
1162@code{tgamma} applies the gamma function to @var{x}. The gamma
1163function is defined as
1164@tex
1165$$\Gamma(x) = \int_0^\infty t^{x-1} e^{-t} \hbox{d}t$$
1166@end tex
1167@ifnottex
1168@smallexample
1169gamma (x) = integral from 0 to @infinity{} of t^(x-1) e^-t dt
1170@end smallexample
1171@end ifnottex
1172
ec751a23 1173This function was introduced in @w{ISO C99}.
7a68c94a 1174@end deftypefun
7a68c94a
UD
1175
1176@comment math.h
1177@comment SVID
1178@deftypefun double j0 (double @var{x})
4260bc74
UD
1179@comment math.h
1180@comment SVID
7a68c94a 1181@deftypefunx float j0f (float @var{x})
4260bc74
UD
1182@comment math.h
1183@comment SVID
7a68c94a 1184@deftypefunx {long double} j0l (long double @var{x})
27aaa791 1185@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
7a68c94a
UD
1186@code{j0} returns the Bessel function of the first kind of order 0 of
1187@var{x}. It may signal underflow if @var{x} is too large.
1188@end deftypefun
1189
1190@comment math.h
1191@comment SVID
1192@deftypefun double j1 (double @var{x})
4260bc74
UD
1193@comment math.h
1194@comment SVID
7a68c94a 1195@deftypefunx float j1f (float @var{x})
4260bc74
UD
1196@comment math.h
1197@comment SVID
7a68c94a 1198@deftypefunx {long double} j1l (long double @var{x})
27aaa791 1199@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
7a68c94a
UD
1200@code{j1} returns the Bessel function of the first kind of order 1 of
1201@var{x}. It may signal underflow if @var{x} is too large.
1202@end deftypefun
1203
1204@comment math.h
1205@comment SVID
cc6e48bc 1206@deftypefun double jn (int @var{n}, double @var{x})
4260bc74
UD
1207@comment math.h
1208@comment SVID
cc6e48bc 1209@deftypefunx float jnf (int @var{n}, float @var{x})
4260bc74
UD
1210@comment math.h
1211@comment SVID
cc6e48bc 1212@deftypefunx {long double} jnl (int @var{n}, long double @var{x})
27aaa791 1213@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
7a68c94a
UD
1214@code{jn} returns the Bessel function of the first kind of order
1215@var{n} of @var{x}. It may signal underflow if @var{x} is too large.
1216@end deftypefun
1217
1218@comment math.h
1219@comment SVID
1220@deftypefun double y0 (double @var{x})
4260bc74
UD
1221@comment math.h
1222@comment SVID
7a68c94a 1223@deftypefunx float y0f (float @var{x})
4260bc74
UD
1224@comment math.h
1225@comment SVID
7a68c94a 1226@deftypefunx {long double} y0l (long double @var{x})
27aaa791 1227@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
7a68c94a
UD
1228@code{y0} returns the Bessel function of the second kind of order 0 of
1229@var{x}. It may signal underflow if @var{x} is too large. If @var{x}
1230is negative, @code{y0} signals a domain error; if it is zero,
1231@code{y0} signals overflow and returns @math{-@infinity}.
1232@end deftypefun
1233
1234@comment math.h
1235@comment SVID
1236@deftypefun double y1 (double @var{x})
4260bc74
UD
1237@comment math.h
1238@comment SVID
7a68c94a 1239@deftypefunx float y1f (float @var{x})
4260bc74
UD
1240@comment math.h
1241@comment SVID
7a68c94a 1242@deftypefunx {long double} y1l (long double @var{x})
27aaa791 1243@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
7a68c94a
UD
1244@code{y1} returns the Bessel function of the second kind of order 1 of
1245@var{x}. It may signal underflow if @var{x} is too large. If @var{x}
1246is negative, @code{y1} signals a domain error; if it is zero,
1247@code{y1} signals overflow and returns @math{-@infinity}.
1248@end deftypefun
1249
1250@comment math.h
1251@comment SVID
cc6e48bc 1252@deftypefun double yn (int @var{n}, double @var{x})
4260bc74
UD
1253@comment math.h
1254@comment SVID
cc6e48bc 1255@deftypefunx float ynf (int @var{n}, float @var{x})
4260bc74
UD
1256@comment math.h
1257@comment SVID
cc6e48bc 1258@deftypefunx {long double} ynl (int @var{n}, long double @var{x})
27aaa791 1259@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
7a68c94a
UD
1260@code{yn} returns the Bessel function of the second kind of order @var{n} of
1261@var{x}. It may signal underflow if @var{x} is too large. If @var{x}
1262is negative, @code{yn} signals a domain error; if it is zero,
1263@code{yn} signals overflow and returns @math{-@infinity}.
1264@end deftypefun
55c14926 1265
aaa1276e
UD
1266@node Errors in Math Functions
1267@section Known Maximum Errors in Math Functions
1268@cindex math errors
1269@cindex ulps
1270
1271This section lists the known errors of the functions in the math
1272library. Errors are measured in ``units of the last place''. This is a
1273measure for the relative error. For a number @math{z} with the
1274representation @math{d.d@dots{}d@mul{}2^e} (we assume IEEE
1275floating-point numbers with base 2) the ULP is represented by
1276
1277@tex
ec751a23 1278$${|d.d\dots d - (z/2^e)|}\over {2^{p-1}}$$
aaa1276e
UD
1279@end tex
1280@ifnottex
1281@smallexample
1282|d.d...d - (z / 2^e)| / 2^(p - 1)
1283@end smallexample
1284@end ifnottex
1285
1286@noindent
1287where @math{p} is the number of bits in the mantissa of the
1288floating-point number representation. Ideally the error for all
7475aef5
JM
1289functions is always less than 0.5ulps in round-to-nearest mode. Using
1290rounding bits this is also
1291possible and normally implemented for the basic operations. Except
1292for certain functions such as @code{sqrt}, @code{fma} and @code{rint}
1293whose results are fully specified by reference to corresponding IEEE
1294754 floating-point operations, and conversions between strings and
1295floating point, @theglibc{} does not aim for correctly rounded results
1296for functions in the math library, and does not aim for correctness in
1297whether ``inexact'' exceptions are raised. Instead, the goals for
1298accuracy of functions without fully specified results are as follows;
1299some functions have bugs meaning they do not meet these goals in all
1300cases. In future, @theglibc{} may provide some other correctly
1301rounding functions under the names such as @code{crsin} proposed for
1302an extension to ISO C.
1303
1304@itemize @bullet
1305
1306@item
1307Each function with a floating-point result behaves as if it computes
1308an infinite-precision result that is within a few ulp (in both real
1309and complex parts, for functions with complex results) of the
1310mathematically correct value of the function (interpreted together
1311with ISO C or POSIX semantics for the function in question) at the
1312exact value passed as the input. Exceptions are raised appropriately
1313for this value and in accordance with IEEE 754 / ISO C / POSIX
1314semantics, and it is then rounded according to the current rounding
1315direction to the result that is returned to the user. @code{errno}
1316may also be set (@pxref{Math Error Reporting}).
1317
1318@item
1319For the IBM @code{long double} format, as used on PowerPC GNU/Linux,
1320the accuracy goal is weaker for input values not exactly representable
1321in 106 bits of precision; it is as if the input value is some value
1322within 0.5ulp of the value actually passed, where ``ulp'' is
1323interpreted in terms of a fixed-precision 106-bit mantissa, but not
1324necessarily the exact value actually passed with discontiguous
1325mantissa bits.
1326
1327@item
1328Functions behave as if the infinite-precision result computed is zero,
1329infinity or NaN if and only if that is the mathematically correct
1330infinite-precision result. They behave as if the infinite-precision
1331result computed always has the same sign as the mathematically correct
1332result.
1333
1334@item
1335If the mathematical result is more than a few ulp above the overflow
1336threshold for the current rounding direction, the value returned is
1337the appropriate overflow value for the current rounding direction,
1338with the overflow exception raised.
1339
1340@item
1341If the mathematical result has magnitude well below half the least
1342subnormal magnitude, the returned value is either zero or the least
1343subnormal (in each case, with the correct sign), according to the
1344current rounding direction and with the underflow exception raised.
1345
1346@item
1347Where the mathematical result underflows and is not exactly
1348representable as a floating-point value, the underflow exception is
1349raised (so there may be spurious underflow exceptions in cases where
1350the underflowing result is exact, but not missing underflow exceptions
1351in cases where it is inexact).
1352
1353@item
1354@Theglibc{} does not aim for functions to satisfy other properties of
1355the underlying mathematical function, such as monotonicity, where not
1356implied by the above goals.
1357
1358@item
1359All the above applies to both real and complex parts, for complex
1360functions.
1361
1362@end itemize
aaa1276e
UD
1363
1364Therefore many of the functions in the math library have errors. The
1365table lists the maximum error for each function which is exposed by one
41713d4e
AJ
1366of the existing tests in the test suite. The table tries to cover as much
1367as possible and list the actual maximum error (or at least a ballpark
aaa1276e
UD
1368figure) but this is often not achieved due to the large search space.
1369
1370The table lists the ULP values for different architectures. Different
1371architectures have different results since their hardware support for
1372floating-point operations varies and also the existing hardware support
1373is different.
1374
41713d4e
AJ
1375@page
1376@c This multitable does not fit on a single page
aaa1276e
UD
1377@include libm-err.texi
1378
28f540f4
RM
1379@node Pseudo-Random Numbers
1380@section Pseudo-Random Numbers
1381@cindex random numbers
1382@cindex pseudo-random numbers
1383@cindex seed (for random numbers)
1384
1385This section describes the GNU facilities for generating a series of
1386pseudo-random numbers. The numbers generated are not truly random;
7a68c94a
UD
1387typically, they form a sequence that repeats periodically, with a period
1388so large that you can ignore it for ordinary purposes. The random
1389number generator works by remembering a @dfn{seed} value which it uses
1390to compute the next random number and also to compute a new seed.
28f540f4
RM
1391
1392Although the generated numbers look unpredictable within one run of a
1393program, the sequence of numbers is @emph{exactly the same} from one run
1394to the next. This is because the initial seed is always the same. This
1395is convenient when you are debugging a program, but it is unhelpful if
7a68c94a
UD
1396you want the program to behave unpredictably. If you want a different
1397pseudo-random series each time your program runs, you must specify a
1398different seed each time. For ordinary purposes, basing the seed on the
1399current time works well.
28f540f4 1400
04b9968b 1401You can obtain repeatable sequences of numbers on a particular machine type
28f540f4
RM
1402by specifying the same initial seed value for the random number
1403generator. There is no standard meaning for a particular seed value;
1404the same seed, used in different C libraries or on different CPU types,
1405will give you different random numbers.
1406
1f77f049 1407@Theglibc{} supports the standard @w{ISO C} random number functions
7a68c94a
UD
1408plus two other sets derived from BSD and SVID. The BSD and @w{ISO C}
1409functions provide identical, somewhat limited functionality. If only a
1410small number of random bits are required, we recommend you use the
1411@w{ISO C} interface, @code{rand} and @code{srand}. The SVID functions
1412provide a more flexible interface, which allows better random number
1413generator algorithms, provides more random bits (up to 48) per call, and
1414can provide random floating-point numbers. These functions are required
1415by the XPG standard and therefore will be present in all modern Unix
1416systems.
28f540f4
RM
1417
1418@menu
7a68c94a
UD
1419* ISO Random:: @code{rand} and friends.
1420* BSD Random:: @code{random} and friends.
1421* SVID Random:: @code{drand48} and friends.
28f540f4
RM
1422@end menu
1423
f65fd747
UD
1424@node ISO Random
1425@subsection ISO C Random Number Functions
28f540f4
RM
1426
1427This section describes the random number functions that are part of
f65fd747 1428the @w{ISO C} standard.
28f540f4
RM
1429
1430To use these facilities, you should include the header file
1431@file{stdlib.h} in your program.
1432@pindex stdlib.h
1433
1434@comment stdlib.h
f65fd747 1435@comment ISO
28f540f4 1436@deftypevr Macro int RAND_MAX
7a68c94a 1437The value of this macro is an integer constant representing the largest
1f77f049 1438value the @code{rand} function can return. In @theglibc{}, it is
7a68c94a
UD
1439@code{2147483647}, which is the largest signed integer representable in
144032 bits. In other libraries, it may be as low as @code{32767}.
28f540f4
RM
1441@end deftypevr
1442
1443@comment stdlib.h
f65fd747 1444@comment ISO
ca34d7a7 1445@deftypefun int rand (void)
27aaa791
AO
1446@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
1447@c Just calls random.
28f540f4 1448The @code{rand} function returns the next pseudo-random number in the
7a68c94a 1449series. The value ranges from @code{0} to @code{RAND_MAX}.
28f540f4
RM
1450@end deftypefun
1451
1452@comment stdlib.h
f65fd747 1453@comment ISO
28f540f4 1454@deftypefun void srand (unsigned int @var{seed})
27aaa791
AO
1455@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
1456@c Alias to srandom.
28f540f4
RM
1457This function establishes @var{seed} as the seed for a new series of
1458pseudo-random numbers. If you call @code{rand} before a seed has been
1459established with @code{srand}, it uses the value @code{1} as a default
1460seed.
1461
7a68c94a
UD
1462To produce a different pseudo-random series each time your program is
1463run, do @code{srand (time (0))}.
28f540f4
RM
1464@end deftypefun
1465
7a68c94a
UD
1466POSIX.1 extended the C standard functions to support reproducible random
1467numbers in multi-threaded programs. However, the extension is badly
1468designed and unsuitable for serious work.
61eb22d3
UD
1469
1470@comment stdlib.h
1471@comment POSIX.1
1472@deftypefun int rand_r (unsigned int *@var{seed})
27aaa791 1473@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
61eb22d3 1474This function returns a random number in the range 0 to @code{RAND_MAX}
7a68c94a
UD
1475just as @code{rand} does. However, all its state is stored in the
1476@var{seed} argument. This means the RNG's state can only have as many
1477bits as the type @code{unsigned int} has. This is far too few to
1478provide a good RNG.
61eb22d3 1479
7a68c94a
UD
1480If your program requires a reentrant RNG, we recommend you use the
1481reentrant GNU extensions to the SVID random number generator. The
1482POSIX.1 interface should only be used when the GNU extensions are not
1483available.
61eb22d3
UD
1484@end deftypefun
1485
1486
28f540f4
RM
1487@node BSD Random
1488@subsection BSD Random Number Functions
1489
1490This section describes a set of random number generation functions that
1491are derived from BSD. There is no advantage to using these functions
1f77f049 1492with @theglibc{}; we support them for BSD compatibility only.
28f540f4
RM
1493
1494The prototypes for these functions are in @file{stdlib.h}.
1495@pindex stdlib.h
1496
1497@comment stdlib.h
1498@comment BSD
0423ee17 1499@deftypefun {long int} random (void)
27aaa791
AO
1500@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
1501@c Takes a lock and calls random_r with an automatic variable and the
1502@c global state, while holding a lock.
28f540f4 1503This function returns the next pseudo-random number in the sequence.
8c5c2600 1504The value returned ranges from @code{0} to @code{2147483647}.
ca34d7a7 1505
48b22986 1506@strong{NB:} Temporarily this function was defined to return a
0423ee17
UD
1507@code{int32_t} value to indicate that the return value always contains
150832 bits even if @code{long int} is wider. The standard demands it
1509differently. Users must always be aware of the 32-bit limitation,
1510though.
28f540f4
RM
1511@end deftypefun
1512
1513@comment stdlib.h
1514@comment BSD
1515@deftypefun void srandom (unsigned int @var{seed})
27aaa791
AO
1516@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
1517@c Takes a lock and calls srandom_r with an automatic variable and a
1518@c static buffer. There's no MT-safety issue because the static buffer
1519@c is internally protected by a lock, although other threads may modify
1520@c the set state before it is used.
7a68c94a
UD
1521The @code{srandom} function sets the state of the random number
1522generator based on the integer @var{seed}. If you supply a @var{seed} value
28f540f4
RM
1523of @code{1}, this will cause @code{random} to reproduce the default set
1524of random numbers.
1525
7a68c94a
UD
1526To produce a different set of pseudo-random numbers each time your
1527program runs, do @code{srandom (time (0))}.
28f540f4
RM
1528@end deftypefun
1529
1530@comment stdlib.h
1531@comment BSD
8ded91fb 1532@deftypefun {char *} initstate (unsigned int @var{seed}, char *@var{state}, size_t @var{size})
27aaa791 1533@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
28f540f4
RM
1534The @code{initstate} function is used to initialize the random number
1535generator state. The argument @var{state} is an array of @var{size}
7a68c94a
UD
1536bytes, used to hold the state information. It is initialized based on
1537@var{seed}. The size must be between 8 and 256 bytes, and should be a
1538power of two. The bigger the @var{state} array, the better.
28f540f4
RM
1539
1540The return value is the previous value of the state information array.
1541You can use this value later as an argument to @code{setstate} to
1542restore that state.
1543@end deftypefun
1544
1545@comment stdlib.h
1546@comment BSD
8ded91fb 1547@deftypefun {char *} setstate (char *@var{state})
27aaa791 1548@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
28f540f4
RM
1549The @code{setstate} function restores the random number state
1550information @var{state}. The argument must have been the result of
2c6fe0bd 1551a previous call to @var{initstate} or @var{setstate}.
28f540f4
RM
1552
1553The return value is the previous value of the state information array.
f2ea0f5b 1554You can use this value later as an argument to @code{setstate} to
28f540f4 1555restore that state.
a785f6c5
UD
1556
1557If the function fails the return value is @code{NULL}.
28f540f4 1558@end deftypefun
b4012b75 1559
4c78249d
UD
1560The four functions described so far in this section all work on a state
1561which is shared by all threads. The state is not directly accessible to
1562the user and can only be modified by these functions. This makes it
1563hard to deal with situations where each thread should have its own
1564pseudo-random number generator.
1565
1f77f049 1566@Theglibc{} contains four additional functions which contain the
4c78249d 1567state as an explicit parameter and therefore make it possible to handle
e2f4aa54 1568thread-local PRNGs. Beside this there is no difference. In fact, the
4c78249d
UD
1569four functions already discussed are implemented internally using the
1570following interfaces.
1571
1572The @file{stdlib.h} header contains a definition of the following type:
1573
1574@comment stdlib.h
1575@comment GNU
1576@deftp {Data Type} {struct random_data}
1577
1578Objects of type @code{struct random_data} contain the information
1579necessary to represent the state of the PRNG. Although a complete
1580definition of the type is present the type should be treated as opaque.
1581@end deftp
1582
1583The functions modifying the state follow exactly the already described
1584functions.
1585
1586@comment stdlib.h
1587@comment GNU
1588@deftypefun int random_r (struct random_data *restrict @var{buf}, int32_t *restrict @var{result})
27aaa791 1589@safety{@prelim{}@mtsafe{@mtsrace{:buf}}@assafe{}@acunsafe{@acucorrupt{}}}
4c78249d
UD
1590The @code{random_r} function behaves exactly like the @code{random}
1591function except that it uses and modifies the state in the object
1592pointed to by the first parameter instead of the global state.
1593@end deftypefun
1594
1595@comment stdlib.h
1596@comment GNU
1597@deftypefun int srandom_r (unsigned int @var{seed}, struct random_data *@var{buf})
27aaa791 1598@safety{@prelim{}@mtsafe{@mtsrace{:buf}}@assafe{}@acunsafe{@acucorrupt{}}}
4c78249d
UD
1599The @code{srandom_r} function behaves exactly like the @code{srandom}
1600function except that it uses and modifies the state in the object
1601pointed to by the second parameter instead of the global state.
1602@end deftypefun
1603
1604@comment stdlib.h
1605@comment GNU
1606@deftypefun int initstate_r (unsigned int @var{seed}, char *restrict @var{statebuf}, size_t @var{statelen}, struct random_data *restrict @var{buf})
27aaa791 1607@safety{@prelim{}@mtsafe{@mtsrace{:buf}}@assafe{}@acunsafe{@acucorrupt{}}}
4c78249d
UD
1608The @code{initstate_r} function behaves exactly like the @code{initstate}
1609function except that it uses and modifies the state in the object
1610pointed to by the fourth parameter instead of the global state.
1611@end deftypefun
1612
1613@comment stdlib.h
1614@comment GNU
1615@deftypefun int setstate_r (char *restrict @var{statebuf}, struct random_data *restrict @var{buf})
27aaa791 1616@safety{@prelim{}@mtsafe{@mtsrace{:buf}}@assafe{}@acunsafe{@acucorrupt{}}}
4c78249d
UD
1617The @code{setstate_r} function behaves exactly like the @code{setstate}
1618function except that it uses and modifies the state in the object
1619pointed to by the first parameter instead of the global state.
1620@end deftypefun
1621
b4012b75
UD
1622@node SVID Random
1623@subsection SVID Random Number Function
1624
1625The C library on SVID systems contains yet another kind of random number
1626generator functions. They use a state of 48 bits of data. The user can
7a68c94a 1627choose among a collection of functions which return the random bits
b4012b75
UD
1628in different forms.
1629
04b9968b 1630Generally there are two kinds of function. The first uses a state of
b4012b75 1631the random number generator which is shared among several functions and
04b9968b
UD
1632by all threads of the process. The second requires the user to handle
1633the state.
b4012b75
UD
1634
1635All functions have in common that they use the same congruential
1636formula with the same constants. The formula is
1637
1638@smallexample
1639Y = (a * X + c) mod m
1640@end smallexample
1641
1642@noindent
1643where @var{X} is the state of the generator at the beginning and
1644@var{Y} the state at the end. @code{a} and @code{c} are constants
04b9968b 1645determining the way the generator works. By default they are
b4012b75
UD
1646
1647@smallexample
1648a = 0x5DEECE66D = 25214903917
1649c = 0xb = 11
1650@end smallexample
1651
1652@noindent
1653but they can also be changed by the user. @code{m} is of course 2^48
04b9968b 1654since the state consists of a 48-bit array.
b4012b75 1655
f2615995
UD
1656The prototypes for these functions are in @file{stdlib.h}.
1657@pindex stdlib.h
1658
b4012b75
UD
1659
1660@comment stdlib.h
1661@comment SVID
55c14926 1662@deftypefun double drand48 (void)
27aaa791
AO
1663@safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
1664@c Uses of the static state buffer are not guarded by a lock (thus
1665@c @mtasurace:drand48), so they may be found or left at a
1666@c partially-updated state in case of calls from within signal handlers
1667@c or cancellation. None of this will break safety rules or invoke
1668@c undefined behavior, but it may affect randomness.
b4012b75
UD
1669This function returns a @code{double} value in the range of @code{0.0}
1670to @code{1.0} (exclusive). The random bits are determined by the global
1671state of the random number generator in the C library.
1672
04b9968b 1673Since the @code{double} type according to @w{IEEE 754} has a 52-bit
b4012b75
UD
1674mantissa this means 4 bits are not initialized by the random number
1675generator. These are (of course) chosen to be the least significant
1676bits and they are initialized to @code{0}.
1677@end deftypefun
1678
1679@comment stdlib.h
1680@comment SVID
1681@deftypefun double erand48 (unsigned short int @var{xsubi}[3])
27aaa791
AO
1682@safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
1683@c The static buffer is just initialized with default parameters, which
1684@c are later read to advance the state held in xsubi.
b4012b75 1685This function returns a @code{double} value in the range of @code{0.0}
04b9968b 1686to @code{1.0} (exclusive), similarly to @code{drand48}. The argument is
b4012b75
UD
1687an array describing the state of the random number generator.
1688
1689This function can be called subsequently since it updates the array to
1690guarantee random numbers. The array should have been initialized before
04b9968b 1691initial use to obtain reproducible results.
b4012b75
UD
1692@end deftypefun
1693
1694@comment stdlib.h
1695@comment SVID
55c14926 1696@deftypefun {long int} lrand48 (void)
27aaa791 1697@safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
04b9968b 1698The @code{lrand48} function returns an integer value in the range of
b4012b75 1699@code{0} to @code{2^31} (exclusive). Even if the size of the @code{long
04b9968b 1700int} type can take more than 32 bits, no higher numbers are returned.
b4012b75
UD
1701The random bits are determined by the global state of the random number
1702generator in the C library.
1703@end deftypefun
1704
1705@comment stdlib.h
1706@comment SVID
1707@deftypefun {long int} nrand48 (unsigned short int @var{xsubi}[3])
27aaa791 1708@safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
b4012b75
UD
1709This function is similar to the @code{lrand48} function in that it
1710returns a number in the range of @code{0} to @code{2^31} (exclusive) but
1711the state of the random number generator used to produce the random bits
1712is determined by the array provided as the parameter to the function.
1713
04b9968b
UD
1714The numbers in the array are updated afterwards so that subsequent calls
1715to this function yield different results (as is expected of a random
1716number generator). The array should have been initialized before the
1717first call to obtain reproducible results.
b4012b75
UD
1718@end deftypefun
1719
1720@comment stdlib.h
1721@comment SVID
55c14926 1722@deftypefun {long int} mrand48 (void)
27aaa791 1723@safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
b4012b75
UD
1724The @code{mrand48} function is similar to @code{lrand48}. The only
1725difference is that the numbers returned are in the range @code{-2^31} to
1726@code{2^31} (exclusive).
1727@end deftypefun
1728
1729@comment stdlib.h
1730@comment SVID
1731@deftypefun {long int} jrand48 (unsigned short int @var{xsubi}[3])
27aaa791 1732@safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
b4012b75
UD
1733The @code{jrand48} function is similar to @code{nrand48}. The only
1734difference is that the numbers returned are in the range @code{-2^31} to
1735@code{2^31} (exclusive). For the @code{xsubi} parameter the same
1736requirements are necessary.
1737@end deftypefun
1738
1739The internal state of the random number generator can be initialized in
04b9968b 1740several ways. The methods differ in the completeness of the
b4012b75
UD
1741information provided.
1742
1743@comment stdlib.h
1744@comment SVID
04b9968b 1745@deftypefun void srand48 (long int @var{seedval})
27aaa791 1746@safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
b4012b75 1747The @code{srand48} function sets the most significant 32 bits of the
04b9968b 1748internal state of the random number generator to the least
f2ea0f5b
UD
1749significant 32 bits of the @var{seedval} parameter. The lower 16 bits
1750are initialized to the value @code{0x330E}. Even if the @code{long
04b9968b 1751int} type contains more than 32 bits only the lower 32 bits are used.
b4012b75 1752
04b9968b
UD
1753Owing to this limitation, initialization of the state of this
1754function is not very useful. But it makes it easy to use a construct
b4012b75
UD
1755like @code{srand48 (time (0))}.
1756
1757A side-effect of this function is that the values @code{a} and @code{c}
1758from the internal state, which are used in the congruential formula,
1759are reset to the default values given above. This is of importance once
04b9968b 1760the user has called the @code{lcong48} function (see below).
b4012b75
UD
1761@end deftypefun
1762
1763@comment stdlib.h
1764@comment SVID
1765@deftypefun {unsigned short int *} seed48 (unsigned short int @var{seed16v}[3])
27aaa791 1766@safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
b4012b75 1767The @code{seed48} function initializes all 48 bits of the state of the
04b9968b 1768internal random number generator from the contents of the parameter
b4012b75
UD
1769@var{seed16v}. Here the lower 16 bits of the first element of
1770@var{see16v} initialize the least significant 16 bits of the internal
1771state, the lower 16 bits of @code{@var{seed16v}[1]} initialize the mid-order
177216 bits of the state and the 16 lower bits of @code{@var{seed16v}[2]}
1773initialize the most significant 16 bits of the state.
1774
1775Unlike @code{srand48} this function lets the user initialize all 48 bits
1776of the state.
1777
1778The value returned by @code{seed48} is a pointer to an array containing
1779the values of the internal state before the change. This might be
1780useful to restart the random number generator at a certain state.
04b9968b 1781Otherwise the value can simply be ignored.
b4012b75
UD
1782
1783As for @code{srand48}, the values @code{a} and @code{c} from the
1784congruential formula are reset to the default values.
1785@end deftypefun
1786
1787There is one more function to initialize the random number generator
04b9968b
UD
1788which enables you to specify even more information by allowing you to
1789change the parameters in the congruential formula.
b4012b75
UD
1790
1791@comment stdlib.h
1792@comment SVID
1793@deftypefun void lcong48 (unsigned short int @var{param}[7])
27aaa791 1794@safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
b4012b75
UD
1795The @code{lcong48} function allows the user to change the complete state
1796of the random number generator. Unlike @code{srand48} and
1797@code{seed48}, this function also changes the constants in the
1798congruential formula.
1799
1800From the seven elements in the array @var{param} the least significant
180116 bits of the entries @code{@var{param}[0]} to @code{@var{param}[2]}
04b9968b 1802determine the initial state, the least significant 16 bits of
b4012b75 1803@code{@var{param}[3]} to @code{@var{param}[5]} determine the 48 bit
04b9968b 1804constant @code{a} and @code{@var{param}[6]} determines the 16-bit value
b4012b75
UD
1805@code{c}.
1806@end deftypefun
1807
1808All the above functions have in common that they use the global
1809parameters for the congruential formula. In multi-threaded programs it
1810might sometimes be useful to have different parameters in different
1811threads. For this reason all the above functions have a counterpart
1812which works on a description of the random number generator in the
1813user-supplied buffer instead of the global state.
1814
1815Please note that it is no problem if several threads use the global
1816state if all threads use the functions which take a pointer to an array
1817containing the state. The random numbers are computed following the
1818same loop but if the state in the array is different all threads will
04b9968b 1819obtain an individual random number generator.
b4012b75 1820
04b9968b
UD
1821The user-supplied buffer must be of type @code{struct drand48_data}.
1822This type should be regarded as opaque and not manipulated directly.
b4012b75
UD
1823
1824@comment stdlib.h
1825@comment GNU
1826@deftypefun int drand48_r (struct drand48_data *@var{buffer}, double *@var{result})
27aaa791 1827@safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
b4012b75 1828This function is equivalent to the @code{drand48} function with the
04b9968b
UD
1829difference that it does not modify the global random number generator
1830parameters but instead the parameters in the buffer supplied through the
1831pointer @var{buffer}. The random number is returned in the variable
1832pointed to by @var{result}.
b4012b75 1833
04b9968b 1834The return value of the function indicates whether the call succeeded.
b4012b75
UD
1835If the value is less than @code{0} an error occurred and @var{errno} is
1836set to indicate the problem.
1837
1838This function is a GNU extension and should not be used in portable
1839programs.
1840@end deftypefun
1841
1842@comment stdlib.h
1843@comment GNU
1844@deftypefun int erand48_r (unsigned short int @var{xsubi}[3], struct drand48_data *@var{buffer}, double *@var{result})
27aaa791 1845@safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
04b9968b
UD
1846The @code{erand48_r} function works like @code{erand48}, but in addition
1847it takes an argument @var{buffer} which describes the random number
1848generator. The state of the random number generator is taken from the
1849@code{xsubi} array, the parameters for the congruential formula from the
1850global random number generator data. The random number is returned in
1851the variable pointed to by @var{result}.
b4012b75 1852
04b9968b 1853The return value is non-negative if the call succeeded.
b4012b75
UD
1854
1855This function is a GNU extension and should not be used in portable
1856programs.
1857@end deftypefun
1858
1859@comment stdlib.h
1860@comment GNU
8ded91fb 1861@deftypefun int lrand48_r (struct drand48_data *@var{buffer}, long int *@var{result})
27aaa791 1862@safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
04b9968b
UD
1863This function is similar to @code{lrand48}, but in addition it takes a
1864pointer to a buffer describing the state of the random number generator
1865just like @code{drand48}.
b4012b75
UD
1866
1867If the return value of the function is non-negative the variable pointed
1868to by @var{result} contains the result. Otherwise an error occurred.
1869
1870This function is a GNU extension and should not be used in portable
1871programs.
1872@end deftypefun
1873
1874@comment stdlib.h
1875@comment GNU
1876@deftypefun int nrand48_r (unsigned short int @var{xsubi}[3], struct drand48_data *@var{buffer}, long int *@var{result})
27aaa791 1877@safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
b4012b75 1878The @code{nrand48_r} function works like @code{nrand48} in that it
04b9968b 1879produces a random number in the range @code{0} to @code{2^31}. But instead
b4012b75
UD
1880of using the global parameters for the congruential formula it uses the
1881information from the buffer pointed to by @var{buffer}. The state is
1882described by the values in @var{xsubi}.
1883
1884If the return value is non-negative the variable pointed to by
1885@var{result} contains the result.
1886
1887This function is a GNU extension and should not be used in portable
1888programs.
1889@end deftypefun
1890
1891@comment stdlib.h
1892@comment GNU
8ded91fb 1893@deftypefun int mrand48_r (struct drand48_data *@var{buffer}, long int *@var{result})
27aaa791 1894@safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
04b9968b
UD
1895This function is similar to @code{mrand48} but like the other reentrant
1896functions it uses the random number generator described by the value in
b4012b75
UD
1897the buffer pointed to by @var{buffer}.
1898
1899If the return value is non-negative the variable pointed to by
1900@var{result} contains the result.
1901
1902This function is a GNU extension and should not be used in portable
1903programs.
1904@end deftypefun
1905
1906@comment stdlib.h
1907@comment GNU
1908@deftypefun int jrand48_r (unsigned short int @var{xsubi}[3], struct drand48_data *@var{buffer}, long int *@var{result})
27aaa791 1909@safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
04b9968b 1910The @code{jrand48_r} function is similar to @code{jrand48}. Like the
b4012b75
UD
1911other reentrant functions of this function family it uses the
1912congruential formula parameters from the buffer pointed to by
1913@var{buffer}.
1914
1915If the return value is non-negative the variable pointed to by
1916@var{result} contains the result.
1917
1918This function is a GNU extension and should not be used in portable
1919programs.
1920@end deftypefun
1921
04b9968b
UD
1922Before any of the above functions are used the buffer of type
1923@code{struct drand48_data} should be initialized. The easiest way to do
1924this is to fill the whole buffer with null bytes, e.g. by
b4012b75
UD
1925
1926@smallexample
1927memset (buffer, '\0', sizeof (struct drand48_data));
1928@end smallexample
1929
1930@noindent
f2ea0f5b 1931Using any of the reentrant functions of this family now will
b4012b75
UD
1932automatically initialize the random number generator to the default
1933values for the state and the parameters of the congruential formula.
1934
04b9968b 1935The other possibility is to use any of the functions which explicitly
b4012b75 1936initialize the buffer. Though it might be obvious how to initialize the
04b9968b 1937buffer from looking at the parameter to the function, it is highly
b4012b75
UD
1938recommended to use these functions since the result might not always be
1939what you expect.
1940
1941@comment stdlib.h
1942@comment GNU
1943@deftypefun int srand48_r (long int @var{seedval}, struct drand48_data *@var{buffer})
27aaa791 1944@safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
b4012b75 1945The description of the random number generator represented by the
04b9968b 1946information in @var{buffer} is initialized similarly to what the function
f2ea0f5b
UD
1947@code{srand48} does. The state is initialized from the parameter
1948@var{seedval} and the parameters for the congruential formula are
04b9968b 1949initialized to their default values.
b4012b75
UD
1950
1951If the return value is non-negative the function call succeeded.
1952
1953This function is a GNU extension and should not be used in portable
1954programs.
1955@end deftypefun
1956
1957@comment stdlib.h
1958@comment GNU
1959@deftypefun int seed48_r (unsigned short int @var{seed16v}[3], struct drand48_data *@var{buffer})
27aaa791 1960@safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
b4012b75
UD
1961This function is similar to @code{srand48_r} but like @code{seed48} it
1962initializes all 48 bits of the state from the parameter @var{seed16v}.
1963
1964If the return value is non-negative the function call succeeded. It
1965does not return a pointer to the previous state of the random number
04b9968b
UD
1966generator like the @code{seed48} function does. If the user wants to
1967preserve the state for a later re-run s/he can copy the whole buffer
b4012b75
UD
1968pointed to by @var{buffer}.
1969
1970This function is a GNU extension and should not be used in portable
1971programs.
1972@end deftypefun
1973
1974@comment stdlib.h
1975@comment GNU
1976@deftypefun int lcong48_r (unsigned short int @var{param}[7], struct drand48_data *@var{buffer})
27aaa791 1977@safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
b4012b75 1978This function initializes all aspects of the random number generator
04b9968b
UD
1979described in @var{buffer} with the data in @var{param}. Here it is
1980especially true that the function does more than just copying the
1981contents of @var{param} and @var{buffer}. More work is required and
1982therefore it is important to use this function rather than initializing
1983the random number generator directly.
b4012b75
UD
1984
1985If the return value is non-negative the function call succeeded.
1986
1987This function is a GNU extension and should not be used in portable
1988programs.
1989@end deftypefun
7a68c94a
UD
1990
1991@node FP Function Optimizations
1992@section Is Fast Code or Small Code preferred?
1993@cindex Optimization
1994
04b9968b
UD
1995If an application uses many floating point functions it is often the case
1996that the cost of the function calls themselves is not negligible.
1997Modern processors can often execute the operations themselves
1998very fast, but the function call disrupts the instruction pipeline.
7a68c94a 1999
1f77f049 2000For this reason @theglibc{} provides optimizations for many of the
04b9968b
UD
2001frequently-used math functions. When GNU CC is used and the user
2002activates the optimizer, several new inline functions and macros are
7a68c94a 2003defined. These new functions and macros have the same names as the
04b9968b 2004library functions and so are used instead of the latter. In the case of
7a68c94a 2005inline functions the compiler will decide whether it is reasonable to
04b9968b 2006use them, and this decision is usually correct.
7a68c94a 2007
04b9968b
UD
2008This means that no calls to the library functions may be necessary, and
2009can increase the speed of generated code significantly. The drawback is
2010that code size will increase, and the increase is not always negligible.
7a68c94a 2011
378fbeb4
UD
2012There are two kind of inline functions: Those that give the same result
2013as the library functions and others that might not set @code{errno} and
2014might have a reduced precision and/or argument range in comparison with
2015the library functions. The latter inline functions are only available
2016if the flag @code{-ffast-math} is given to GNU CC.
aa847ee5 2017
7a68c94a
UD
2018In cases where the inline functions and macros are not wanted the symbol
2019@code{__NO_MATH_INLINES} should be defined before any system header is
04b9968b
UD
2020included. This will ensure that only library functions are used. Of
2021course, it can be determined for each file in the project whether
2022giving this option is preferable or not.
2023
2024Not all hardware implements the entire @w{IEEE 754} standard, and even
2025if it does there may be a substantial performance penalty for using some
2026of its features. For example, enabling traps on some processors forces
2027the FPU to run un-pipelined, which can more than double calculation time.
7a68c94a 2028@c ***Add explanation of -lieee, -mieee.