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