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