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