manual: Do not mention STATIC_TLS in dynamic linker hardening recommendations
[glibc.git] / manual / math.texi
blobe4f91226092f81e94ad550e734303ae7a6ceec3b
1 @c We need some definitions here.
2 @ifclear mult
3 @ifhtml
4 @set mult @U{00B7}
5 @set infty @U{221E}
6 @set pie @U{03C0}
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 @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 Support for @code{_Float@var{N}} or @code{_Float@var{N}x} types is
69 provided for @code{_Float32}, @code{_Float64} and @code{_Float32x} on
70 all platforms.
71 It is also provided for @code{_Float128} and @code{_Float64x} on
72 powerpc64le (PowerPC 64-bits little-endian), x86_64, x86,
73 aarch64, alpha, loongarch, mips64, riscv, s390 and sparc.
75 @menu
76 * Mathematical Constants::      Precise numeric values for often-used
77                                  constants.
78 * Trig Functions::              Sine, cosine, tangent, and friends.
79 * Inverse Trig Functions::      Arcsine, arccosine, etc.
80 * Exponents and Logarithms::    Also pow and sqrt.
81 * Hyperbolic Functions::        sinh, cosh, tanh, etc.
82 * Special Functions::           Bessel, gamma, erf.
83 * Errors in Math Functions::    Known Maximum Errors in Math Functions.
84 * Pseudo-Random Numbers::       Functions for generating pseudo-random
85                                  numbers.
86 * FP Function Optimizations::   Fast code or small code.
87 @end menu
89 @node Mathematical Constants
90 @section Predefined Mathematical Constants
91 @cindex constants
92 @cindex mathematical constants
94 The header @file{math.h} defines several useful mathematical constants.
95 All values are defined as preprocessor macros starting with @code{M_}.
96 The values provided are:
98 @vtable @code
99 @item M_E
100 The base of natural logarithms.
101 @item M_LOG2E
102 The logarithm to base @code{2} of @code{M_E}.
103 @item M_LOG10E
104 The logarithm to base @code{10} of @code{M_E}.
105 @item M_LN2
106 The natural logarithm of @code{2}.
107 @item M_LN10
108 The natural logarithm of @code{10}.
109 @item M_PI
110 Pi, the ratio of a circle's circumference to its diameter.
111 @item M_PI_2
112 Pi divided by two.
113 @item M_PI_4
114 Pi divided by four.
115 @item M_1_PI
116 The reciprocal of pi (1/pi)
117 @item M_2_PI
118 Two times the reciprocal of pi.
119 @item M_2_SQRTPI
120 Two times the reciprocal of the square root of pi.
121 @item M_SQRT2
122 The square root of two.
123 @item M_SQRT1_2
124 The reciprocal of the square root of two (also the square root of 1/2).
125 @end vtable
127 These constants come from the Unix98 standard and were also available in
128 4.4BSD; therefore they are only defined if
129 @code{_XOPEN_SOURCE=500}, or a more general feature select macro, is
130 defined.  The default set of features includes these constants.
131 @xref{Feature Test Macros}.
133 All values are of type @code{double}.  As an extension, @theglibc{}
134 also defines these constants with type @code{long double} and
135 @code{float}.  The @code{long double} macros have a lowercase @samp{l}
136 while the @code{float} macros have a lowercase @samp{f} appended to
137 their names: @code{M_El}, @code{M_PIl}, and so forth.  These are only
138 available if @code{_GNU_SOURCE} is defined.
140 Likewise, @theglibc{} also defines these constants with the types
141 @code{_Float@var{N}} and @code{_Float@var{N}x} for the machines that
142 have support for such types enabled (@pxref{Mathematics}) and if
143 @code{_GNU_SOURCE} is defined.  When available, the macros names are
144 appended with @samp{f@var{N}} or @samp{f@var{N}x}, such as @samp{f128}
145 for the type @code{_Float128}.
147 @vindex PI
148 @emph{Note:} Some programs use a constant named @code{PI} which has the
149 same value as @code{M_PI}.  This constant is not standard; it may have
150 appeared in some old AT&T headers, and is mentioned in Stroustrup's book
151 on C++.  It infringes on the user's name space, so @theglibc{}
152 does not define it.  Fixing programs written to expect it is simple:
153 replace @code{PI} with @code{M_PI} throughout, or put @samp{-DPI=M_PI}
154 on the compiler command line.
156 @node Trig Functions
157 @section Trigonometric Functions
158 @cindex trigonometric functions
160 These are the familiar @code{sin}, @code{cos}, and @code{tan} functions.
161 The arguments to all of these functions are in units of radians; recall
162 that pi radians equals 180 degrees.
164 @cindex pi (trigonometric constant)
165 The math library normally defines @code{M_PI} to a @code{double}
166 approximation of pi.  If strict ISO and/or POSIX compliance
167 are requested this constant is not defined, but you can easily define it
168 yourself:
170 @smallexample
171 #define M_PI 3.14159265358979323846264338327
172 @end smallexample
174 @noindent
175 You can also compute the value of pi with the expression @code{acos
176 (-1.0)}.
178 @deftypefun double sin (double @var{x})
179 @deftypefunx float sinf (float @var{x})
180 @deftypefunx {long double} sinl (long double @var{x})
181 @deftypefunx _FloatN sinfN (_Float@var{N} @var{x})
182 @deftypefunx _FloatNx sinfNx (_Float@var{N}x @var{x})
183 @standards{ISO, math.h}
184 @standardsx{sinfN, TS 18661-3:2015, math.h}
185 @standardsx{sinfNx, TS 18661-3:2015, math.h}
186 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
187 These functions return the sine of @var{x}, where @var{x} is given in
188 radians.  The return value is in the range @code{-1} to @code{1}.
189 @end deftypefun
191 @deftypefun double cos (double @var{x})
192 @deftypefunx float cosf (float @var{x})
193 @deftypefunx {long double} cosl (long double @var{x})
194 @deftypefunx _FloatN cosfN (_Float@var{N} @var{x})
195 @deftypefunx _FloatNx cosfNx (_Float@var{N}x @var{x})
196 @standards{ISO, math.h}
197 @standardsx{cosfN, TS 18661-3:2015, math.h}
198 @standardsx{cosfNx, TS 18661-3:2015, math.h}
199 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
200 These functions return the cosine of @var{x}, where @var{x} is given in
201 radians.  The return value is in the range @code{-1} to @code{1}.
202 @end deftypefun
204 @deftypefun double tan (double @var{x})
205 @deftypefunx float tanf (float @var{x})
206 @deftypefunx {long double} tanl (long double @var{x})
207 @deftypefunx _FloatN tanfN (_Float@var{N} @var{x})
208 @deftypefunx _FloatNx tanfNx (_Float@var{N}x @var{x})
209 @standards{ISO, math.h}
210 @standardsx{tanfN, TS 18661-3:2015, math.h}
211 @standardsx{tanfNx, TS 18661-3:2015, math.h}
212 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
213 These functions return the tangent of @var{x}, where @var{x} is given in
214 radians.
216 Mathematically, the tangent function has singularities at odd multiples
217 of pi/2.  If the argument @var{x} is too close to one of these
218 singularities, @code{tan} will signal overflow.
219 @end deftypefun
221 In many applications where @code{sin} and @code{cos} are used, the sine
222 and cosine of the same angle are needed at the same time.  It is more
223 efficient to compute them simultaneously, so the library provides a
224 function to do that.
226 @deftypefun void sincos (double @var{x}, double *@var{sinx}, double *@var{cosx})
227 @deftypefunx void sincosf (float @var{x}, float *@var{sinx}, float *@var{cosx})
228 @deftypefunx void sincosl (long double @var{x}, long double *@var{sinx}, long double *@var{cosx})
229 @deftypefunx _FloatN sincosfN (_Float@var{N} @var{x}, _Float@var{N} *@var{sinx}, _Float@var{N} *@var{cosx})
230 @deftypefunx _FloatNx sincosfNx (_Float@var{N}x @var{x}, _Float@var{N}x *@var{sinx}, _Float@var{N}x *@var{cosx})
231 @standards{GNU, math.h}
232 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
233 These functions return the sine of @var{x} in @code{*@var{sinx}} and the
234 cosine of @var{x} in @code{*@var{cosx}}, where @var{x} is given in
235 radians.  Both values, @code{*@var{sinx}} and @code{*@var{cosx}}, are in
236 the range of @code{-1} to @code{1}.
238 All these functions, including the @code{_Float@var{N}} and
239 @code{_Float@var{N}x} variants, are GNU extensions.  Portable programs
240 should be prepared to cope with their absence.
241 @end deftypefun
243 @cindex complex trigonometric functions
245 @w{ISO C99} defines variants of the trig functions which work on
246 complex numbers.  @Theglibc{} provides these functions, but they
247 are only useful if your compiler supports the new complex types defined
248 by the standard.
249 @c XXX Change this when gcc is fixed. -zw
250 (As of this writing GCC supports complex numbers, but there are bugs in
251 the implementation.)
253 @deftypefun {complex double} csin (complex double @var{z})
254 @deftypefunx {complex float} csinf (complex float @var{z})
255 @deftypefunx {complex long double} csinl (complex long double @var{z})
256 @deftypefunx {complex _FloatN} csinfN (complex _Float@var{N} @var{z})
257 @deftypefunx {complex _FloatNx} csinfNx (complex _Float@var{N}x @var{z})
258 @standards{ISO, complex.h}
259 @standardsx{csinfN, TS 18661-3:2015, complex.h}
260 @standardsx{csinfNx, TS 18661-3:2015, complex.h}
261 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
262 @c There are calls to nan* that could trigger @mtslocale if they didn't get
263 @c empty strings.
264 These functions return the complex sine of @var{z}.
265 The mathematical definition of the complex sine is
267 @ifnottex
268 @math{sin (z) = 1/(2*i) * (exp (z*i) - exp (-z*i))}.
269 @end ifnottex
270 @tex
271 $$\sin(z) = {1\over 2i} (e^{zi} - e^{-zi})$$
272 @end tex
273 @end deftypefun
275 @deftypefun {complex double} ccos (complex double @var{z})
276 @deftypefunx {complex float} ccosf (complex float @var{z})
277 @deftypefunx {complex long double} ccosl (complex long double @var{z})
278 @deftypefunx {complex _FloatN} ccosfN (complex _Float@var{N} @var{z})
279 @deftypefunx {complex _FloatNx} ccosfNx (complex _Float@var{N}x @var{z})
280 @standards{ISO, complex.h}
281 @standardsx{ccosfN, TS 18661-3:2015, complex.h}
282 @standardsx{ccosfNx, TS 18661-3:2015, complex.h}
283 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
284 These functions return the complex cosine of @var{z}.
285 The mathematical definition of the complex cosine is
287 @ifnottex
288 @math{cos (z) = 1/2 * (exp (z*i) + exp (-z*i))}
289 @end ifnottex
290 @tex
291 $$\cos(z) = {1\over 2} (e^{zi} + e^{-zi})$$
292 @end tex
293 @end deftypefun
295 @deftypefun {complex double} ctan (complex double @var{z})
296 @deftypefunx {complex float} ctanf (complex float @var{z})
297 @deftypefunx {complex long double} ctanl (complex long double @var{z})
298 @deftypefunx {complex _FloatN} ctanfN (complex _Float@var{N} @var{z})
299 @deftypefunx {complex _FloatNx} ctanfNx (complex _Float@var{N}x @var{z})
300 @standards{ISO, complex.h}
301 @standardsx{ctanfN, TS 18661-3:2015, complex.h}
302 @standardsx{ctanfNx, TS 18661-3:2015, complex.h}
303 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
304 These functions return the complex tangent of @var{z}.
305 The mathematical definition of the complex tangent is
307 @ifnottex
308 @math{tan (z) = -i * (exp (z*i) - exp (-z*i)) / (exp (z*i) + exp (-z*i))}
309 @end ifnottex
310 @tex
311 $$\tan(z) = -i \cdot {e^{zi} - e^{-zi}\over e^{zi} + e^{-zi}}$$
312 @end tex
314 @noindent
315 The complex tangent has poles at @math{pi/2 + 2n}, where @math{n} is an
316 integer.  @code{ctan} may signal overflow if @var{z} is too close to a
317 pole.
318 @end deftypefun
321 @node Inverse Trig Functions
322 @section Inverse Trigonometric Functions
323 @cindex inverse trigonometric functions
325 These are the usual arcsine, arccosine and arctangent functions,
326 which are the inverses of the sine, cosine and tangent functions
327 respectively.
329 @deftypefun double asin (double @var{x})
330 @deftypefunx float asinf (float @var{x})
331 @deftypefunx {long double} asinl (long double @var{x})
332 @deftypefunx _FloatN asinfN (_Float@var{N} @var{x})
333 @deftypefunx _FloatNx asinfNx (_Float@var{N}x @var{x})
334 @standards{ISO, math.h}
335 @standardsx{asinfN, TS 18661-3:2015, math.h}
336 @standardsx{asinfNx, TS 18661-3:2015, math.h}
337 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
338 These functions compute the arcsine of @var{x}---that is, the value whose
339 sine is @var{x}.  The value is in units of radians.  Mathematically,
340 there are infinitely many such values; the one actually returned is the
341 one between @code{-pi/2} and @code{pi/2} (inclusive).
343 The arcsine function is defined mathematically only
344 over the domain @code{-1} to @code{1}.  If @var{x} is outside the
345 domain, @code{asin} signals a domain error.
346 @end deftypefun
348 @deftypefun double acos (double @var{x})
349 @deftypefunx float acosf (float @var{x})
350 @deftypefunx {long double} acosl (long double @var{x})
351 @deftypefunx _FloatN acosfN (_Float@var{N} @var{x})
352 @deftypefunx _FloatNx acosfNx (_Float@var{N}x @var{x})
353 @standards{ISO, math.h}
354 @standardsx{acosfN, TS 18661-3:2015, math.h}
355 @standardsx{acosfNx, TS 18661-3:2015, math.h}
356 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
357 These functions compute the arccosine of @var{x}---that is, the value
358 whose cosine is @var{x}.  The value is in units of radians.
359 Mathematically, there are infinitely many such values; the one actually
360 returned is the one between @code{0} and @code{pi} (inclusive).
362 The arccosine function is defined mathematically only
363 over the domain @code{-1} to @code{1}.  If @var{x} is outside the
364 domain, @code{acos} signals a domain error.
365 @end deftypefun
367 @deftypefun double atan (double @var{x})
368 @deftypefunx float atanf (float @var{x})
369 @deftypefunx {long double} atanl (long double @var{x})
370 @deftypefunx _FloatN atanfN (_Float@var{N} @var{x})
371 @deftypefunx _FloatNx atanfNx (_Float@var{N}x @var{x})
372 @standards{ISO, math.h}
373 @standardsx{atanfN, TS 18661-3:2015, math.h}
374 @standardsx{atanfNx, TS 18661-3:2015, math.h}
375 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
376 These functions compute the arctangent of @var{x}---that is, the value
377 whose tangent is @var{x}.  The value is in units of radians.
378 Mathematically, there are infinitely many such values; the one actually
379 returned is the one between @code{-pi/2} and @code{pi/2} (inclusive).
380 @end deftypefun
382 @deftypefun double atan2 (double @var{y}, double @var{x})
383 @deftypefunx float atan2f (float @var{y}, float @var{x})
384 @deftypefunx {long double} atan2l (long double @var{y}, long double @var{x})
385 @deftypefunx _FloatN atan2fN (_Float@var{N} @var{y}, _Float@var{N} @var{x})
386 @deftypefunx _FloatNx atan2fNx (_Float@var{N}x @var{y}, _Float@var{N}x @var{x})
387 @standards{ISO, math.h}
388 @standardsx{atan2fN, TS 18661-3:2015, math.h}
389 @standardsx{atan2fNx, TS 18661-3:2015, math.h}
390 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
391 This function computes the arctangent of @var{y}/@var{x}, but the signs
392 of both arguments are used to determine the quadrant of the result, and
393 @var{x} is permitted to be zero.  The return value is given in radians
394 and is in the range @code{-pi} to @code{pi}, inclusive.
396 If @var{x} and @var{y} are coordinates of a point in the plane,
397 @code{atan2} returns the signed angle between the line from the origin
398 to that point and the x-axis.  Thus, @code{atan2} is useful for
399 converting Cartesian coordinates to polar coordinates.  (To compute the
400 radial coordinate, use @code{hypot}; see @ref{Exponents and
401 Logarithms}.)
403 @c This is experimentally true.  Should it be so? -zw
404 If both @var{x} and @var{y} are zero, @code{atan2} returns zero.
405 @end deftypefun
407 @cindex inverse complex trigonometric functions
408 @w{ISO C99} defines complex versions of the inverse trig functions.
410 @deftypefun {complex double} casin (complex double @var{z})
411 @deftypefunx {complex float} casinf (complex float @var{z})
412 @deftypefunx {complex long double} casinl (complex long double @var{z})
413 @deftypefunx {complex _FloatN} casinfN (complex _Float@var{N} @var{z})
414 @deftypefunx {complex _FloatNx} casinfNx (complex _Float@var{N}x @var{z})
415 @standards{ISO, complex.h}
416 @standardsx{casinfN, TS 18661-3:2015, complex.h}
417 @standardsx{casinfNx, TS 18661-3:2015, complex.h}
418 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
419 These functions compute the complex arcsine of @var{z}---that is, the
420 value whose sine is @var{z}.  The value returned is in radians.
422 Unlike the real-valued functions, @code{casin} is defined for all
423 values of @var{z}.
424 @end deftypefun
426 @deftypefun {complex double} cacos (complex double @var{z})
427 @deftypefunx {complex float} cacosf (complex float @var{z})
428 @deftypefunx {complex long double} cacosl (complex long double @var{z})
429 @deftypefunx {complex _FloatN} cacosfN (complex _Float@var{N} @var{z})
430 @deftypefunx {complex _FloatNx} cacosfNx (complex _Float@var{N}x @var{z})
431 @standards{ISO, complex.h}
432 @standardsx{cacosfN, TS 18661-3:2015, complex.h}
433 @standardsx{cacosfNx, TS 18661-3:2015, complex.h}
434 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
435 These functions compute the complex arccosine of @var{z}---that is, the
436 value whose cosine is @var{z}.  The value returned is in radians.
438 Unlike the real-valued functions, @code{cacos} is defined for all
439 values of @var{z}.
440 @end deftypefun
443 @deftypefun {complex double} catan (complex double @var{z})
444 @deftypefunx {complex float} catanf (complex float @var{z})
445 @deftypefunx {complex long double} catanl (complex long double @var{z})
446 @deftypefunx {complex _FloatN} catanfN (complex _Float@var{N} @var{z})
447 @deftypefunx {complex _FloatNx} catanfNx (complex _Float@var{N}x @var{z})
448 @standards{ISO, complex.h}
449 @standardsx{catanfN, TS 18661-3:2015, complex.h}
450 @standardsx{catanfNx, TS 18661-3:2015, complex.h}
451 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
452 These functions compute the complex arctangent of @var{z}---that is,
453 the value whose tangent is @var{z}.  The value is in units of radians.
454 @end deftypefun
457 @node Exponents and Logarithms
458 @section Exponentiation and Logarithms
459 @cindex exponentiation functions
460 @cindex power functions
461 @cindex logarithm functions
463 @deftypefun double exp (double @var{x})
464 @deftypefunx float expf (float @var{x})
465 @deftypefunx {long double} expl (long double @var{x})
466 @deftypefunx _FloatN expfN (_Float@var{N} @var{x})
467 @deftypefunx _FloatNx expfNx (_Float@var{N}x @var{x})
468 @standards{ISO, math.h}
469 @standardsx{expfN, TS 18661-3:2015, math.h}
470 @standardsx{expfNx, TS 18661-3:2015, math.h}
471 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
472 These functions compute @code{e} (the base of natural logarithms) raised
473 to the power @var{x}.
475 If the magnitude of the result is too large to be representable,
476 @code{exp} signals overflow.
477 @end deftypefun
479 @deftypefun double exp2 (double @var{x})
480 @deftypefunx float exp2f (float @var{x})
481 @deftypefunx {long double} exp2l (long double @var{x})
482 @deftypefunx _FloatN exp2fN (_Float@var{N} @var{x})
483 @deftypefunx _FloatNx exp2fNx (_Float@var{N}x @var{x})
484 @standards{ISO, math.h}
485 @standardsx{exp2fN, TS 18661-3:2015, math.h}
486 @standardsx{exp2fNx, TS 18661-3:2015, math.h}
487 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
488 These functions compute @code{2} raised to the power @var{x}.
489 Mathematically, @code{exp2 (x)} is the same as @code{exp (x * log (2))}.
490 @end deftypefun
492 @deftypefun double exp10 (double @var{x})
493 @deftypefunx float exp10f (float @var{x})
494 @deftypefunx {long double} exp10l (long double @var{x})
495 @deftypefunx _FloatN exp10fN (_Float@var{N} @var{x})
496 @deftypefunx _FloatNx exp10fNx (_Float@var{N}x @var{x})
497 @standards{ISO, math.h}
498 @standardsx{exp10fN, TS 18661-4:2015, math.h}
499 @standardsx{exp10fNx, TS 18661-4:2015, math.h}
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.
505 @end deftypefun
508 @deftypefun double log (double @var{x})
509 @deftypefunx float logf (float @var{x})
510 @deftypefunx {long double} logl (long double @var{x})
511 @deftypefunx _FloatN logfN (_Float@var{N} @var{x})
512 @deftypefunx _FloatNx logfNx (_Float@var{N}x @var{x})
513 @standards{ISO, math.h}
514 @standardsx{logfN, TS 18661-3:2015, math.h}
515 @standardsx{logfNx, TS 18661-3:2015, math.h}
516 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
517 These functions compute the natural logarithm of @var{x}.  @code{exp (log
518 (@var{x}))} equals @var{x}, exactly in mathematics and approximately in
521 If @var{x} is negative, @code{log} signals a domain error.  If @var{x}
522 is zero, it returns negative infinity; if @var{x} is too close to zero,
523 it may signal overflow.
524 @end deftypefun
526 @deftypefun double log10 (double @var{x})
527 @deftypefunx float log10f (float @var{x})
528 @deftypefunx {long double} log10l (long double @var{x})
529 @deftypefunx _FloatN log10fN (_Float@var{N} @var{x})
530 @deftypefunx _FloatNx log10fNx (_Float@var{N}x @var{x})
531 @standards{ISO, math.h}
532 @standardsx{log10fN, TS 18661-3:2015, math.h}
533 @standardsx{log10fNx, TS 18661-3:2015, math.h}
534 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
535 These functions return the base-10 logarithm of @var{x}.
536 @code{log10 (@var{x})} equals @code{log (@var{x}) / log (10)}.
538 @end deftypefun
540 @deftypefun double log2 (double @var{x})
541 @deftypefunx float log2f (float @var{x})
542 @deftypefunx {long double} log2l (long double @var{x})
543 @deftypefunx _FloatN log2fN (_Float@var{N} @var{x})
544 @deftypefunx _FloatNx log2fNx (_Float@var{N}x @var{x})
545 @standards{ISO, math.h}
546 @standardsx{log2fN, TS 18661-3:2015, math.h}
547 @standardsx{log2fNx, TS 18661-3:2015, math.h}
548 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
549 These functions return the base-2 logarithm of @var{x}.
550 @code{log2 (@var{x})} equals @code{log (@var{x}) / log (2)}.
551 @end deftypefun
553 @deftypefun double logb (double @var{x})
554 @deftypefunx float logbf (float @var{x})
555 @deftypefunx {long double} logbl (long double @var{x})
556 @deftypefunx _FloatN logbfN (_Float@var{N} @var{x})
557 @deftypefunx _FloatNx logbfNx (_Float@var{N}x @var{x})
558 @standards{ISO, math.h}
559 @standardsx{logbfN, TS 18661-3:2015, math.h}
560 @standardsx{logbfNx, TS 18661-3:2015, math.h}
561 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
562 These functions extract the exponent of @var{x} and return it as a
563 floating-point value.
564 If @code{FLT_RADIX} is two,
565 @code{logb (x)} is similar to @code{floor (log2 (fabs (x)))},
566 except that the latter may give an incorrect integer
567 due to intermediate rounding.
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}.
704 They cannot fail;
705 every representable real value
706 has a real cube root,
707 and rounding it to a representable value
708 never causes overflow nor underflow.
709 @end deftypefun
711 @deftypefun double hypot (double @var{x}, double @var{y})
712 @deftypefunx float hypotf (float @var{x}, float @var{y})
713 @deftypefunx {long double} hypotl (long double @var{x}, long double @var{y})
714 @deftypefunx _FloatN hypotfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
715 @deftypefunx _FloatNx hypotfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
716 @standards{ISO, math.h}
717 @standardsx{hypotfN, TS 18661-3:2015, math.h}
718 @standardsx{hypotfNx, TS 18661-3:2015, math.h}
719 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
720 These functions return @code{sqrt (@var{x}*@var{x} +
721 @var{y}*@var{y})}.  This is the length of the hypotenuse of a right
722 triangle with sides of length @var{x} and @var{y}, or the distance
723 of the point (@var{x}, @var{y}) from the origin.  Using this function
724 instead of the direct formula is wise, since the error is
725 much smaller.  See also the function @code{cabs} in @ref{Absolute Value}.
726 @end deftypefun
728 @deftypefun double expm1 (double @var{x})
729 @deftypefunx float expm1f (float @var{x})
730 @deftypefunx {long double} expm1l (long double @var{x})
731 @deftypefunx _FloatN expm1fN (_Float@var{N} @var{x})
732 @deftypefunx _FloatNx expm1fNx (_Float@var{N}x @var{x})
733 @standards{ISO, math.h}
734 @standardsx{expm1fN, TS 18661-3:2015, math.h}
735 @standardsx{expm1fNx, TS 18661-3:2015, math.h}
736 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
737 These functions return a value equivalent to @code{exp (@var{x}) - 1}.
738 They are computed in a way that is accurate even if @var{x} is
739 near zero---a case where @code{exp (@var{x}) - 1} would be inaccurate owing
740 to subtraction of two numbers that are nearly equal.
741 @end deftypefun
743 @deftypefun double exp2m1 (double @var{x})
744 @deftypefunx float exp2m1f (float @var{x})
745 @deftypefunx {long double} exp2m1l (long double @var{x})
746 @deftypefunx _FloatN exp2m1fN (_Float@var{N} @var{x})
747 @deftypefunx _FloatNx exp2m1fNx (_Float@var{N}x @var{x})
748 @standards{TS 18661-4:2015, math.h}
749 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
750 These functions return a value equivalent to @code{exp2 (@var{x}) - 1}.
751 They are computed in a way that is accurate even if @var{x} is
752 near zero---a case where @code{exp2 (@var{x}) - 1} would be inaccurate owing
753 to subtraction of two numbers that are nearly equal.
755 The @code{exp2m1} functions are from TS 18661-4:2015.
756 @end deftypefun
758 @deftypefun double exp10m1 (double @var{x})
759 @deftypefunx float exp10m1f (float @var{x})
760 @deftypefunx {long double} exp10m1l (long double @var{x})
761 @deftypefunx _FloatN exp10m1fN (_Float@var{N} @var{x})
762 @deftypefunx _FloatNx exp10m1fNx (_Float@var{N}x @var{x})
763 @standards{TS 18661-4:2015, math.h}
764 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
765 These functions return a value equivalent to @code{exp10 (@var{x}) - 1}.
766 They are computed in a way that is accurate even if @var{x} is
767 near zero---a case where @code{exp10 (@var{x}) - 1} would be inaccurate owing
768 to subtraction of two numbers that are nearly equal.
770 The @code{exp10m1} functions are from TS 18661-4:2015.
771 @end deftypefun
773 @deftypefun double log1p (double @var{x})
774 @deftypefunx float log1pf (float @var{x})
775 @deftypefunx {long double} log1pl (long double @var{x})
776 @deftypefunx _FloatN log1pfN (_Float@var{N} @var{x})
777 @deftypefunx _FloatNx log1pfNx (_Float@var{N}x @var{x})
778 @deftypefunx double logp1 (double @var{x})
779 @deftypefunx float logp1f (float @var{x})
780 @deftypefunx {long double} logp1l (long double @var{x})
781 @deftypefunx _FloatN logp1fN (_Float@var{N} @var{x})
782 @deftypefunx _FloatNx logp1fNx (_Float@var{N}x @var{x})
783 @standards{ISO, math.h}
784 @standardsx{log1pfN, TS 18661-3:2015, math.h}
785 @standardsx{log1pfNx, TS 18661-3:2015, math.h}
786 @standardsx{logp1, TS 18661-4:2015, math.h}
787 @standardsx{logp1f, TS 18661-4:2015, math.h}
788 @standardsx{logp1l, TS 18661-4:2015, math.h}
789 @standardsx{logp1fN, TS 18661-4:2015, math.h}
790 @standardsx{logp1fNx, TS 18661-4:2015, math.h}
791 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
792 These functions return a value equivalent to @w{@code{log (1 + @var{x})}}.
793 They are computed in a way that is accurate even if @var{x} is
794 near zero.
796 The @code{logp1} names for these functions are from TS 18661-4:2015.
797 @end deftypefun
799 @deftypefun double log2p1 (double @var{x})
800 @deftypefunx float log2p1f (float @var{x})
801 @deftypefunx {long double} log2p1l (long double @var{x})
802 @deftypefunx _FloatN log2p1fN (_Float@var{N} @var{x})
803 @deftypefunx _FloatNx log2p1fNx (_Float@var{N}x @var{x})
804 @standards{TS 18661-4:2015, math.h}
805 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
806 These functions return a value equivalent to @w{@code{log2 (1 + @var{x})}}.
807 They are computed in a way that is accurate even if @var{x} is
808 near zero.
810 The @code{log2p1} functions are from TS 18661-4:2015.
811 @end deftypefun
813 @deftypefun double log10p1 (double @var{x})
814 @deftypefunx float log10p1f (float @var{x})
815 @deftypefunx {long double} log10p1l (long double @var{x})
816 @deftypefunx _FloatN log10p1fN (_Float@var{N} @var{x})
817 @deftypefunx _FloatNx log10p1fNx (_Float@var{N}x @var{x})
818 @standards{TS 18661-4:2015, math.h}
819 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
820 These functions return a value equivalent to @w{@code{log10 (1 + @var{x})}}.
821 They are computed in a way that is accurate even if @var{x} is
822 near zero.
824 The @code{log10p1} functions are from TS 18661-4:2015.
825 @end deftypefun
827 @cindex complex exponentiation functions
828 @cindex complex logarithm functions
830 @w{ISO C99} defines complex variants of some of the exponentiation and
831 logarithm functions.
833 @deftypefun {complex double} cexp (complex double @var{z})
834 @deftypefunx {complex float} cexpf (complex float @var{z})
835 @deftypefunx {complex long double} cexpl (complex long double @var{z})
836 @deftypefunx {complex _FloatN} cexpfN (complex _Float@var{N} @var{z})
837 @deftypefunx {complex _FloatNx} cexpfNx (complex _Float@var{N}x @var{z})
838 @standards{ISO, complex.h}
839 @standardsx{cexpfN, TS 18661-3:2015, complex.h}
840 @standardsx{cexpfNx, TS 18661-3:2015, complex.h}
841 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
842 These functions return @code{e} (the base of natural
843 logarithms) raised to the power of @var{z}.
844 Mathematically, this corresponds to the value
846 @ifnottex
847 @math{exp (z) = exp (creal (z)) * (cos (cimag (z)) + I * sin (cimag (z)))}
848 @end ifnottex
849 @tex
850 $$\exp(z) = e^z = e^{{\rm Re}\,z} (\cos ({\rm Im}\,z) + i \sin ({\rm Im}\,z))$$
851 @end tex
852 @end deftypefun
854 @deftypefun {complex double} clog (complex double @var{z})
855 @deftypefunx {complex float} clogf (complex float @var{z})
856 @deftypefunx {complex long double} clogl (complex long double @var{z})
857 @deftypefunx {complex _FloatN} clogfN (complex _Float@var{N} @var{z})
858 @deftypefunx {complex _FloatNx} clogfNx (complex _Float@var{N}x @var{z})
859 @standards{ISO, complex.h}
860 @standardsx{clogfN, TS 18661-3:2015, complex.h}
861 @standardsx{clogfNx, TS 18661-3:2015, complex.h}
862 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
863 These functions return the natural logarithm of @var{z}.
864 Mathematically, this corresponds to the value
866 @ifnottex
867 @math{log (z) = log (cabs (z)) + I * carg (z)}
868 @end ifnottex
869 @tex
870 $$\log(z) = \log |z| + i \arg z$$
871 @end tex
873 @noindent
874 @code{clog} has a pole at 0, and will signal overflow if @var{z} equals
875 or is very close to 0.  It is well-defined for all other values of
876 @var{z}.
877 @end deftypefun
880 @deftypefun {complex double} clog10 (complex double @var{z})
881 @deftypefunx {complex float} clog10f (complex float @var{z})
882 @deftypefunx {complex long double} clog10l (complex long double @var{z})
883 @deftypefunx {complex _FloatN} clog10fN (complex _Float@var{N} @var{z})
884 @deftypefunx {complex _FloatNx} clog10fNx (complex _Float@var{N}x @var{z})
885 @standards{GNU, complex.h}
886 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
887 These functions return the base 10 logarithm of the complex value
888 @var{z}.  Mathematically, this corresponds to the value
890 @ifnottex
891 @math{log10 (z) = log10 (cabs (z)) + I * carg (z) / log (10)}
892 @end ifnottex
893 @tex
894 $$\log_{10}(z) = \log_{10}|z| + i \arg z / \log (10)$$
895 @end tex
897 All these functions, including the @code{_Float@var{N}} and
898 @code{_Float@var{N}x} variants, are GNU extensions.
899 @end deftypefun
901 @deftypefun {complex double} csqrt (complex double @var{z})
902 @deftypefunx {complex float} csqrtf (complex float @var{z})
903 @deftypefunx {complex long double} csqrtl (complex long double @var{z})
904 @deftypefunx {complex _FloatN} csqrtfN (_Float@var{N} @var{z})
905 @deftypefunx {complex _FloatNx} csqrtfNx (complex _Float@var{N}x @var{z})
906 @standards{ISO, complex.h}
907 @standardsx{csqrtfN, TS 18661-3:2015, complex.h}
908 @standardsx{csqrtfNx, TS 18661-3:2015, complex.h}
909 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
910 These functions return the complex square root of the argument @var{z}.  Unlike
911 the real-valued functions, they are defined for all values of @var{z}.
912 @end deftypefun
914 @deftypefun {complex double} cpow (complex double @var{base}, complex double @var{power})
915 @deftypefunx {complex float} cpowf (complex float @var{base}, complex float @var{power})
916 @deftypefunx {complex long double} cpowl (complex long double @var{base}, complex long double @var{power})
917 @deftypefunx {complex _FloatN} cpowfN (complex _Float@var{N} @var{base}, complex _Float@var{N} @var{power})
918 @deftypefunx {complex _FloatNx} cpowfNx (complex _Float@var{N}x @var{base}, complex _Float@var{N}x @var{power})
919 @standards{ISO, complex.h}
920 @standardsx{cpowfN, TS 18661-3:2015, complex.h}
921 @standardsx{cpowfNx, TS 18661-3:2015, complex.h}
922 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
923 These functions return @var{base} raised to the power of
924 @var{power}.  This is equivalent to @w{@code{cexp (y * clog (x))}}
925 @end deftypefun
927 @node Hyperbolic Functions
928 @section Hyperbolic Functions
929 @cindex hyperbolic functions
931 The functions in this section are related to the exponential functions;
932 see @ref{Exponents and Logarithms}.
934 @deftypefun double sinh (double @var{x})
935 @deftypefunx float sinhf (float @var{x})
936 @deftypefunx {long double} sinhl (long double @var{x})
937 @deftypefunx _FloatN sinhfN (_Float@var{N} @var{x})
938 @deftypefunx _FloatNx sinhfNx (_Float@var{N}x @var{x})
939 @standards{ISO, math.h}
940 @standardsx{sinhfN, TS 18661-3:2015, math.h}
941 @standardsx{sinhfNx, TS 18661-3:2015, math.h}
942 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
943 These functions return the hyperbolic sine of @var{x}, defined
944 mathematically as @w{@code{(exp (@var{x}) - exp (-@var{x})) / 2}}.  They
945 may signal overflow if @var{x} is too large.
946 @end deftypefun
948 @deftypefun double cosh (double @var{x})
949 @deftypefunx float coshf (float @var{x})
950 @deftypefunx {long double} coshl (long double @var{x})
951 @deftypefunx _FloatN coshfN (_Float@var{N} @var{x})
952 @deftypefunx _FloatNx coshfNx (_Float@var{N}x @var{x})
953 @standards{ISO, math.h}
954 @standardsx{coshfN, TS 18661-3:2015, math.h}
955 @standardsx{coshfNx, TS 18661-3:2015, math.h}
956 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
957 These functions return the hyperbolic cosine of @var{x},
958 defined mathematically as @w{@code{(exp (@var{x}) + exp (-@var{x})) / 2}}.
959 They may signal overflow if @var{x} is too large.
960 @end deftypefun
962 @deftypefun double tanh (double @var{x})
963 @deftypefunx float tanhf (float @var{x})
964 @deftypefunx {long double} tanhl (long double @var{x})
965 @deftypefunx _FloatN tanhfN (_Float@var{N} @var{x})
966 @deftypefunx _FloatNx tanhfNx (_Float@var{N}x @var{x})
967 @standards{ISO, math.h}
968 @standardsx{tanhfN, TS 18661-3:2015, math.h}
969 @standardsx{tanhfNx, TS 18661-3:2015, math.h}
970 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
971 These functions return the hyperbolic tangent of @var{x},
972 defined mathematically as @w{@code{sinh (@var{x}) / cosh (@var{x})}}.
973 They may signal overflow if @var{x} is too large.
974 @end deftypefun
976 @cindex hyperbolic functions
978 There are counterparts for the hyperbolic functions which take
979 complex arguments.
981 @deftypefun {complex double} csinh (complex double @var{z})
982 @deftypefunx {complex float} csinhf (complex float @var{z})
983 @deftypefunx {complex long double} csinhl (complex long double @var{z})
984 @deftypefunx {complex _FloatN} csinhfN (complex _Float@var{N} @var{z})
985 @deftypefunx {complex _FloatNx} csinhfNx (complex _Float@var{N}x @var{z})
986 @standards{ISO, complex.h}
987 @standardsx{csinhfN, TS 18661-3:2015, complex.h}
988 @standardsx{csinhfNx, TS 18661-3:2015, complex.h}
989 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
990 These functions return the complex hyperbolic sine of @var{z}, defined
991 mathematically as @w{@code{(exp (@var{z}) - exp (-@var{z})) / 2}}.
992 @end deftypefun
994 @deftypefun {complex double} ccosh (complex double @var{z})
995 @deftypefunx {complex float} ccoshf (complex float @var{z})
996 @deftypefunx {complex long double} ccoshl (complex long double @var{z})
997 @deftypefunx {complex _FloatN} ccoshfN (complex _Float@var{N} @var{z})
998 @deftypefunx {complex _FloatNx} ccoshfNx (complex _Float@var{N}x @var{z})
999 @standards{ISO, complex.h}
1000 @standardsx{ccoshfN, TS 18661-3:2015, complex.h}
1001 @standardsx{ccoshfNx, TS 18661-3:2015, complex.h}
1002 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1003 These functions return the complex hyperbolic cosine of @var{z}, defined
1004 mathematically as @w{@code{(exp (@var{z}) + exp (-@var{z})) / 2}}.
1005 @end deftypefun
1007 @deftypefun {complex double} ctanh (complex double @var{z})
1008 @deftypefunx {complex float} ctanhf (complex float @var{z})
1009 @deftypefunx {complex long double} ctanhl (complex long double @var{z})
1010 @deftypefunx {complex _FloatN} ctanhfN (complex _Float@var{N} @var{z})
1011 @deftypefunx {complex _FloatNx} ctanhfNx (complex _Float@var{N}x @var{z})
1012 @standards{ISO, complex.h}
1013 @standardsx{ctanhfN, TS 18661-3:2015, complex.h}
1014 @standardsx{ctanhfNx, TS 18661-3:2015, complex.h}
1015 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1016 These functions return the complex hyperbolic tangent of @var{z},
1017 defined mathematically as @w{@code{csinh (@var{z}) / ccosh (@var{z})}}.
1018 @end deftypefun
1021 @cindex inverse hyperbolic functions
1023 @deftypefun double asinh (double @var{x})
1024 @deftypefunx float asinhf (float @var{x})
1025 @deftypefunx {long double} asinhl (long double @var{x})
1026 @deftypefunx _FloatN asinhfN (_Float@var{N} @var{x})
1027 @deftypefunx _FloatNx asinhfNx (_Float@var{N}x @var{x})
1028 @standards{ISO, math.h}
1029 @standardsx{asinhfN, TS 18661-3:2015, math.h}
1030 @standardsx{asinhfNx, TS 18661-3:2015, math.h}
1031 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1032 These functions return the inverse hyperbolic sine of @var{x}---the
1033 value whose hyperbolic sine is @var{x}.
1034 @end deftypefun
1036 @deftypefun double acosh (double @var{x})
1037 @deftypefunx float acoshf (float @var{x})
1038 @deftypefunx {long double} acoshl (long double @var{x})
1039 @deftypefunx _FloatN acoshfN (_Float@var{N} @var{x})
1040 @deftypefunx _FloatNx acoshfNx (_Float@var{N}x @var{x})
1041 @standards{ISO, math.h}
1042 @standardsx{acoshfN, TS 18661-3:2015, math.h}
1043 @standardsx{acoshfNx, TS 18661-3:2015, math.h}
1044 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1045 These functions return the inverse hyperbolic cosine of @var{x}---the
1046 value whose hyperbolic cosine is @var{x}.  If @var{x} is less than
1047 @code{1}, @code{acosh} signals a domain error.
1048 @end deftypefun
1050 @deftypefun double atanh (double @var{x})
1051 @deftypefunx float atanhf (float @var{x})
1052 @deftypefunx {long double} atanhl (long double @var{x})
1053 @deftypefunx _FloatN atanhfN (_Float@var{N} @var{x})
1054 @deftypefunx _FloatNx atanhfNx (_Float@var{N}x @var{x})
1055 @standards{ISO, math.h}
1056 @standardsx{atanhfN, TS 18661-3:2015, math.h}
1057 @standardsx{atanhfNx, TS 18661-3:2015, math.h}
1058 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1059 These functions return the inverse hyperbolic tangent of @var{x}---the
1060 value whose hyperbolic tangent is @var{x}.  If the absolute value of
1061 @var{x} is greater than @code{1}, @code{atanh} signals a domain error;
1062 if it is equal to 1, @code{atanh} returns infinity.
1063 @end deftypefun
1065 @cindex inverse complex hyperbolic functions
1067 @deftypefun {complex double} casinh (complex double @var{z})
1068 @deftypefunx {complex float} casinhf (complex float @var{z})
1069 @deftypefunx {complex long double} casinhl (complex long double @var{z})
1070 @deftypefunx {complex _FloatN} casinhfN (complex _Float@var{N} @var{z})
1071 @deftypefunx {complex _FloatNx} casinhfNx (complex _Float@var{N}x @var{z})
1072 @standards{ISO, complex.h}
1073 @standardsx{casinhfN, TS 18661-3:2015, complex.h}
1074 @standardsx{casinhfNx, TS 18661-3:2015, complex.h}
1075 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1076 These functions return the inverse complex hyperbolic sine of
1077 @var{z}---the value whose complex hyperbolic sine is @var{z}.
1078 @end deftypefun
1080 @deftypefun {complex double} cacosh (complex double @var{z})
1081 @deftypefunx {complex float} cacoshf (complex float @var{z})
1082 @deftypefunx {complex long double} cacoshl (complex long double @var{z})
1083 @deftypefunx {complex _FloatN} cacoshfN (complex _Float@var{N} @var{z})
1084 @deftypefunx {complex _FloatNx} cacoshfNx (complex _Float@var{N}x @var{z})
1085 @standards{ISO, complex.h}
1086 @standardsx{cacoshfN, TS 18661-3:2015, complex.h}
1087 @standardsx{cacoshfNx, TS 18661-3:2015, complex.h}
1088 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1089 These functions return the inverse complex hyperbolic cosine of
1090 @var{z}---the value whose complex hyperbolic cosine is @var{z}.  Unlike
1091 the real-valued functions, there are no restrictions on the value of @var{z}.
1092 @end deftypefun
1094 @deftypefun {complex double} catanh (complex double @var{z})
1095 @deftypefunx {complex float} catanhf (complex float @var{z})
1096 @deftypefunx {complex long double} catanhl (complex long double @var{z})
1097 @deftypefunx {complex _FloatN} catanhfN (complex _Float@var{N} @var{z})
1098 @deftypefunx {complex _FloatNx} catanhfNx (complex _Float@var{N}x @var{z})
1099 @standards{ISO, complex.h}
1100 @standardsx{catanhfN, TS 18661-3:2015, complex.h}
1101 @standardsx{catanhfNx, TS 18661-3:2015, complex.h}
1102 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1103 These functions return the inverse complex hyperbolic tangent of
1104 @var{z}---the value whose complex hyperbolic tangent is @var{z}.  Unlike
1105 the real-valued functions, there are no restrictions on the value of
1106 @var{z}.
1107 @end deftypefun
1109 @node Special Functions
1110 @section Special Functions
1111 @cindex special functions
1112 @cindex Bessel functions
1113 @cindex gamma function
1115 These are some more exotic mathematical functions which are sometimes
1116 useful.  Currently they only have real-valued versions.
1118 @deftypefun double erf (double @var{x})
1119 @deftypefunx float erff (float @var{x})
1120 @deftypefunx {long double} erfl (long double @var{x})
1121 @deftypefunx _FloatN erffN (_Float@var{N} @var{x})
1122 @deftypefunx _FloatNx erffNx (_Float@var{N}x @var{x})
1123 @standards{SVID, math.h}
1124 @standardsx{erffN, TS 18661-3:2015, math.h}
1125 @standardsx{erffNx, TS 18661-3:2015, math.h}
1126 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1127 @code{erf} returns the error function of @var{x}.  The error
1128 function is defined as
1129 @tex
1130 $$\hbox{erf}(x) = {2\over\sqrt{\pi}}\cdot\int_0^x e^{-t^2} \hbox{d}t$$
1131 @end tex
1132 @ifnottex
1133 @smallexample
1134 erf (x) = 2/sqrt(pi) * integral from 0 to x of exp(-t^2) dt
1135 @end smallexample
1136 @end ifnottex
1137 @end deftypefun
1139 @deftypefun double erfc (double @var{x})
1140 @deftypefunx float erfcf (float @var{x})
1141 @deftypefunx {long double} erfcl (long double @var{x})
1142 @deftypefunx _FloatN erfcfN (_Float@var{N} @var{x})
1143 @deftypefunx _FloatNx erfcfNx (_Float@var{N}x @var{x})
1144 @standards{SVID, math.h}
1145 @standardsx{erfcfN, TS 18661-3:2015, math.h}
1146 @standardsx{erfcfNx, TS 18661-3:2015, math.h}
1147 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1148 @code{erfc} returns @code{1.0 - erf(@var{x})}, but computed in a
1149 fashion that avoids round-off error when @var{x} is large.
1150 @end deftypefun
1152 @deftypefun double lgamma (double @var{x})
1153 @deftypefunx float lgammaf (float @var{x})
1154 @deftypefunx {long double} lgammal (long double @var{x})
1155 @deftypefunx _FloatN lgammafN (_Float@var{N} @var{x})
1156 @deftypefunx _FloatNx lgammafNx (_Float@var{N}x @var{x})
1157 @standards{SVID, math.h}
1158 @standardsx{lgammafN, TS 18661-3:2015, math.h}
1159 @standardsx{lgammafNx, TS 18661-3:2015, math.h}
1160 @safety{@prelim{}@mtunsafe{@mtasurace{:signgam}}@asunsafe{}@acsafe{}}
1161 @code{lgamma} returns the natural logarithm of the absolute value of
1162 the gamma function of @var{x}.  The gamma function is defined as
1163 @tex
1164 $$\Gamma(x) = \int_0^\infty t^{x-1} e^{-t} \hbox{d}t$$
1165 @end tex
1166 @ifnottex
1167 @smallexample
1168 gamma (x) = integral from 0 to @infinity{} of t^(x-1) e^-t dt
1169 @end smallexample
1170 @end ifnottex
1172 @vindex signgam
1173 The sign of the gamma function is stored in the global variable
1174 @var{signgam}, which is declared in @file{math.h}.  It is @code{1} if
1175 the intermediate result was positive or zero, or @code{-1} if it was
1176 negative.
1178 To compute the real gamma function you can use the @code{tgamma}
1179 function or you can compute the values as follows:
1180 @smallexample
1181 lgam = lgamma(x);
1182 gam  = signgam*exp(lgam);
1183 @end smallexample
1185 The gamma function has singularities at the non-positive integers.
1186 @code{lgamma} will raise the zero divide exception if evaluated at a
1187 singularity.
1188 @end deftypefun
1190 @deftypefun double lgamma_r (double @var{x}, int *@var{signp})
1191 @deftypefunx float lgammaf_r (float @var{x}, int *@var{signp})
1192 @deftypefunx {long double} lgammal_r (long double @var{x}, int *@var{signp})
1193 @deftypefunx _FloatN lgammafN_r (_Float@var{N} @var{x}, int *@var{signp})
1194 @deftypefunx _FloatNx lgammafNx_r (_Float@var{N}x @var{x}, int *@var{signp})
1195 @standards{XPG, math.h}
1196 @standardsx{lgammafN_r, GNU, math.h}
1197 @standardsx{lgammafNx_r, GNU, math.h}
1198 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1199 @code{lgamma_r} is just like @code{lgamma}, but it stores the sign of
1200 the intermediate result in the variable pointed to by @var{signp}
1201 instead of in the @var{signgam} global.  This means it is reentrant.
1203 The @code{lgammaf@var{N}_r} and @code{lgammaf@var{N}x_r} functions are
1204 GNU extensions.
1205 @end deftypefun
1207 @deftypefun double gamma (double @var{x})
1208 @deftypefunx float gammaf (float @var{x})
1209 @deftypefunx {long double} gammal (long double @var{x})
1210 @standards{SVID, math.h}
1211 @safety{@prelim{}@mtunsafe{@mtasurace{:signgam}}@asunsafe{}@acsafe{}}
1212 These functions exist for compatibility reasons.  They are equivalent to
1213 @code{lgamma} etc.  It is better to use @code{lgamma} since for one the
1214 name reflects better the actual computation, and moreover @code{lgamma} is
1215 standardized in @w{ISO C99} while @code{gamma} is not.
1216 @end deftypefun
1218 @deftypefun double tgamma (double @var{x})
1219 @deftypefunx float tgammaf (float @var{x})
1220 @deftypefunx {long double} tgammal (long double @var{x})
1221 @deftypefunx _FloatN tgammafN (_Float@var{N} @var{x})
1222 @deftypefunx _FloatNx tgammafNx (_Float@var{N}x @var{x})
1223 @standardsx{tgamma, XPG, math.h}
1224 @standardsx{tgamma, ISO, math.h}
1225 @standardsx{tgammaf, XPG, math.h}
1226 @standardsx{tgammaf, ISO, math.h}
1227 @standardsx{tgammal, XPG, math.h}
1228 @standardsx{tgammal, ISO, math.h}
1229 @standardsx{tgammafN, TS 18661-3:2015, math.h}
1230 @standardsx{tgammafNx, TS 18661-3:2015, math.h}
1231 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1232 @code{tgamma} applies the gamma function to @var{x}.  The gamma
1233 function is defined as
1234 @tex
1235 $$\Gamma(x) = \int_0^\infty t^{x-1} e^{-t} \hbox{d}t$$
1236 @end tex
1237 @ifnottex
1238 @smallexample
1239 gamma (x) = integral from 0 to @infinity{} of t^(x-1) e^-t dt
1240 @end smallexample
1241 @end ifnottex
1243 This function was introduced in @w{ISO C99}.  The @code{_Float@var{N}}
1244 and @code{_Float@var{N}x} variants were introduced in @w{ISO/IEC TS
1245 18661-3}.
1246 @end deftypefun
1248 @deftypefun double j0 (double @var{x})
1249 @deftypefunx float j0f (float @var{x})
1250 @deftypefunx {long double} j0l (long double @var{x})
1251 @deftypefunx _FloatN j0fN (_Float@var{N} @var{x})
1252 @deftypefunx _FloatNx j0fNx (_Float@var{N}x @var{x})
1253 @standards{SVID, math.h}
1254 @standardsx{j0fN, GNU, math.h}
1255 @standardsx{j0fNx, GNU, math.h}
1256 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1257 @code{j0} returns the Bessel function of the first kind of order 0 of
1258 @var{x}.  It may signal underflow if @var{x} is too large.
1260 The @code{_Float@var{N}} and @code{_Float@var{N}x} variants are GNU
1261 extensions.
1262 @end deftypefun
1264 @deftypefun double j1 (double @var{x})
1265 @deftypefunx float j1f (float @var{x})
1266 @deftypefunx {long double} j1l (long double @var{x})
1267 @deftypefunx _FloatN j1fN (_Float@var{N} @var{x})
1268 @deftypefunx _FloatNx j1fNx (_Float@var{N}x @var{x})
1269 @standards{SVID, math.h}
1270 @standardsx{j1fN, GNU, math.h}
1271 @standardsx{j1fNx, GNU, math.h}
1272 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1273 @code{j1} returns the Bessel function of the first kind of order 1 of
1274 @var{x}.  It may signal underflow if @var{x} is too large.
1276 The @code{_Float@var{N}} and @code{_Float@var{N}x} variants are GNU
1277 extensions.
1278 @end deftypefun
1280 @deftypefun double jn (int @var{n}, double @var{x})
1281 @deftypefunx float jnf (int @var{n}, float @var{x})
1282 @deftypefunx {long double} jnl (int @var{n}, long double @var{x})
1283 @deftypefunx _FloatN jnfN (int @var{n}, _Float@var{N} @var{x})
1284 @deftypefunx _FloatNx jnfNx (int @var{n}, _Float@var{N}x @var{x})
1285 @standards{SVID, math.h}
1286 @standardsx{jnfN, GNU, math.h}
1287 @standardsx{jnfNx, GNU, math.h}
1288 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1289 @code{jn} returns the Bessel function of the first kind of order
1290 @var{n} of @var{x}.  It may signal underflow if @var{x} is too large.
1292 The @code{_Float@var{N}} and @code{_Float@var{N}x} variants are GNU
1293 extensions.
1294 @end deftypefun
1296 @deftypefun double y0 (double @var{x})
1297 @deftypefunx float y0f (float @var{x})
1298 @deftypefunx {long double} y0l (long double @var{x})
1299 @deftypefunx _FloatN y0fN (_Float@var{N} @var{x})
1300 @deftypefunx _FloatNx y0fNx (_Float@var{N}x @var{x})
1301 @standards{SVID, math.h}
1302 @standardsx{y0fN, GNU, math.h}
1303 @standardsx{y0fNx, GNU, math.h}
1304 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1305 @code{y0} returns the Bessel function of the second kind of order 0 of
1306 @var{x}.  It may signal underflow if @var{x} is too large.  If @var{x}
1307 is negative, @code{y0} signals a domain error; if it is zero,
1308 @code{y0} signals overflow and returns @math{-@infinity}.
1310 The @code{_Float@var{N}} and @code{_Float@var{N}x} variants are GNU
1311 extensions.
1312 @end deftypefun
1314 @deftypefun double y1 (double @var{x})
1315 @deftypefunx float y1f (float @var{x})
1316 @deftypefunx {long double} y1l (long double @var{x})
1317 @deftypefunx _FloatN y1fN (_Float@var{N} @var{x})
1318 @deftypefunx _FloatNx y1fNx (_Float@var{N}x @var{x})
1319 @standards{SVID, math.h}
1320 @standardsx{y1fN, GNU, math.h}
1321 @standardsx{y1fNx, GNU, math.h}
1322 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1323 @code{y1} returns the Bessel function of the second kind of order 1 of
1324 @var{x}.  It may signal underflow if @var{x} is too large.  If @var{x}
1325 is negative, @code{y1} signals a domain error; if it is zero,
1326 @code{y1} signals overflow and returns @math{-@infinity}.
1328 The @code{_Float@var{N}} and @code{_Float@var{N}x} variants are GNU
1329 extensions.
1330 @end deftypefun
1332 @deftypefun double yn (int @var{n}, double @var{x})
1333 @deftypefunx float ynf (int @var{n}, float @var{x})
1334 @deftypefunx {long double} ynl (int @var{n}, long double @var{x})
1335 @deftypefunx _FloatN ynfN (int @var{n}, _Float@var{N} @var{x})
1336 @deftypefunx _FloatNx ynfNx (int @var{n}, _Float@var{N}x @var{x})
1337 @standards{SVID, math.h}
1338 @standardsx{ynfN, GNU, math.h}
1339 @standardsx{ynfNx, GNU, math.h}
1340 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1341 @code{yn} returns the Bessel function of the second kind of order @var{n} of
1342 @var{x}.  It may signal underflow if @var{x} is too large.  If @var{x}
1343 is negative, @code{yn} signals a domain error; if it is zero,
1344 @code{yn} signals overflow and returns @math{-@infinity}.
1346 The @code{_Float@var{N}} and @code{_Float@var{N}x} variants are GNU
1347 extensions.
1348 @end deftypefun
1350 @node Errors in Math Functions
1351 @section Known Maximum Errors in Math Functions
1352 @cindex math errors
1353 @cindex ulps
1355 This section lists the known errors of the functions in the math
1356 library.  Errors are measured in ``units of the last place''.  This is a
1357 measure for the relative error.  For a number @math{z} with the
1358 representation @math{d.d@dots{}d@mul{}2^e} (we assume IEEE
1359 floating-point numbers with base 2) the ULP is represented by
1361 @tex
1362 $${|d.d\dots d - (z/2^e)|}\over {2^{p-1}}$$
1363 @end tex
1364 @ifnottex
1365 @smallexample
1366 |d.d...d - (z / 2^e)| / 2^(p - 1)
1367 @end smallexample
1368 @end ifnottex
1370 @noindent
1371 where @math{p} is the number of bits in the mantissa of the
1372 floating-point number representation.  Ideally the error for all
1373 functions is always less than 0.5ulps in round-to-nearest mode.  Using
1374 rounding bits this is also
1375 possible and normally implemented for the basic operations.  Except
1376 for certain functions such as @code{sqrt}, @code{fma} and @code{rint}
1377 whose results are fully specified by reference to corresponding IEEE
1378 754 floating-point operations, and conversions between strings and
1379 floating point, @theglibc{} does not aim for correctly rounded results
1380 for functions in the math library, and does not aim for correctness in
1381 whether ``inexact'' exceptions are raised.  Instead, the goals for
1382 accuracy of functions without fully specified results are as follows;
1383 some functions have bugs meaning they do not meet these goals in all
1384 cases.  In the future, @theglibc{} may provide some other correctly
1385 rounding functions under the names such as @code{crsin} proposed for
1386 an extension to ISO C.
1388 @itemize @bullet
1390 @item
1391 Each function with a floating-point result behaves as if it computes
1392 an infinite-precision result that is within a few ulp (in both real
1393 and complex parts, for functions with complex results) of the
1394 mathematically correct value of the function (interpreted together
1395 with ISO C or POSIX semantics for the function in question) at the
1396 exact value passed as the input.  Exceptions are raised appropriately
1397 for this value and in accordance with IEEE 754 / ISO C / POSIX
1398 semantics, and it is then rounded according to the current rounding
1399 direction to the result that is returned to the user.  @code{errno}
1400 may also be set (@pxref{Math Error Reporting}).  (The ``inexact''
1401 exception may be raised, or not raised, even if this is inconsistent
1402 with the infinite-precision value.)
1404 @item
1405 For the IBM @code{long double} format, as used on PowerPC GNU/Linux,
1406 the accuracy goal is weaker for input values not exactly representable
1407 in 106 bits of precision; it is as if the input value is some value
1408 within 0.5ulp of the value actually passed, where ``ulp'' is
1409 interpreted in terms of a fixed-precision 106-bit mantissa, but not
1410 necessarily the exact value actually passed with discontiguous
1411 mantissa bits.
1413 @item
1414 For the IBM @code{long double} format, functions whose results are
1415 fully specified by reference to corresponding IEEE 754 floating-point
1416 operations have the same accuracy goals as other functions, but with
1417 the error bound being the same as that for division (3ulp).
1418 Furthermore, ``inexact'' and ``underflow'' exceptions may be raised
1419 for all functions for any inputs, even where such exceptions are
1420 inconsistent with the returned value, since the underlying
1421 floating-point arithmetic has that property.
1423 @item
1424 Functions behave as if the infinite-precision result computed is zero,
1425 infinity or NaN if and only if that is the mathematically correct
1426 infinite-precision result.  They behave as if the infinite-precision
1427 result computed always has the same sign as the mathematically correct
1428 result.
1430 @item
1431 If the mathematical result is more than a few ulp above the overflow
1432 threshold for the current rounding direction, the value returned is
1433 the appropriate overflow value for the current rounding direction,
1434 with the overflow exception raised.
1436 @item
1437 If the mathematical result has magnitude well below half the least
1438 subnormal magnitude, the returned value is either zero or the least
1439 subnormal (in each case, with the correct sign), according to the
1440 current rounding direction and with the underflow exception raised.
1442 @item
1443 Where the mathematical result underflows (before rounding) and is not
1444 exactly representable as a floating-point value, the function does not
1445 behave as if the computed infinite-precision result is an exact value
1446 in the subnormal range.  This means that the underflow exception is
1447 raised other than possibly for cases where the mathematical result is
1448 very close to the underflow threshold and the function behaves as if
1449 it computes an infinite-precision result that does not underflow.  (So
1450 there may be spurious underflow exceptions in cases where the
1451 underflowing result is exact, but not missing underflow exceptions in
1452 cases where it is inexact.)
1454 @item
1455 @Theglibc{} does not aim for functions to satisfy other properties of
1456 the underlying mathematical function, such as monotonicity, where not
1457 implied by the above goals.
1459 @item
1460 All the above applies to both real and complex parts, for complex
1461 functions.
1463 @end itemize
1465 Therefore many of the functions in the math library have errors.  The
1466 table lists the maximum error for each function which is exposed by one
1467 of the existing tests in the test suite.  The table tries to cover as much
1468 as possible and list the actual maximum error (or at least a ballpark
1469 figure) but this is often not achieved due to the large search space.
1471 The table lists the ULP values for different architectures.  Different
1472 architectures have different results since their hardware support for
1473 floating-point operations varies and also the existing hardware support
1474 is different.  Only the round-to-nearest rounding mode is covered by
1475 this table.  Functions not listed do not have known errors.  Vector
1476 versions of functions in the x86_64 libmvec library have a maximum error
1477 of 4 ulps.
1479 @page
1480 @c This multitable does not fit on a single page
1481 @include libm-err.texi
1483 @node Pseudo-Random Numbers
1484 @section Pseudo-Random Numbers
1485 @cindex random numbers
1486 @cindex pseudo-random numbers
1487 @cindex seed (for random numbers)
1489 This section describes the GNU facilities for generating a series of
1490 pseudo-random numbers.  The numbers generated are not truly random;
1491 typically, they form a sequence that repeats periodically, with a period
1492 so large that you can ignore it for ordinary purposes.  The random
1493 number generator works by remembering a @dfn{seed} value which it uses
1494 to compute the next random number and also to compute a new seed.
1496 Although the generated numbers look unpredictable within one run of a
1497 program, the sequence of numbers is @emph{exactly the same} from one run
1498 to the next.  This is because the initial seed is always the same.  This
1499 is convenient when you are debugging a program, but it is unhelpful if
1500 you want the program to behave unpredictably.  If you want a different
1501 pseudo-random series each time your program runs, you must specify a
1502 different seed each time.  For ordinary purposes, basing the seed on the
1503 current time works well.  For random numbers in cryptography,
1504 @pxref{Unpredictable Bytes}.
1506 You can obtain repeatable sequences of numbers on a particular machine type
1507 by specifying the same initial seed value for the random number
1508 generator.  There is no standard meaning for a particular seed value;
1509 the same seed, used in different C libraries or on different CPU types,
1510 will give you different random numbers.
1512 @Theglibc{} supports the standard @w{ISO C} random number functions
1513 plus two other sets derived from BSD and SVID.  The BSD and @w{ISO C}
1514 functions provide identical, somewhat limited functionality.  If only a
1515 small number of random bits are required, we recommend you use the
1516 @w{ISO C} interface, @code{rand} and @code{srand}.  The SVID functions
1517 provide a more flexible interface, which allows better random number
1518 generator algorithms, provides more random bits (up to 48) per call, and
1519 can provide random floating-point numbers.  These functions are required
1520 by the XPG standard and therefore will be present in all modern Unix
1521 systems.
1523 @menu
1524 * ISO Random::                  @code{rand} and friends.
1525 * BSD Random::                  @code{random} and friends.
1526 * SVID Random::                 @code{drand48} and friends.
1527 * High Quality Random::         @code{arc4random} and friends.
1528 @end menu
1530 @node ISO Random
1531 @subsection ISO C Random Number Functions
1533 This section describes the random number functions that are part of
1534 the @w{ISO C} standard.
1536 To use these facilities, you should include the header file
1537 @file{stdlib.h} in your program.
1538 @pindex stdlib.h
1540 @deftypevr Macro int RAND_MAX
1541 @standards{ISO, stdlib.h}
1542 The value of this macro is an integer constant representing the largest
1543 value the @code{rand} function can return.  In @theglibc{}, it is
1544 @code{2147483647}, which is the largest signed integer representable in
1545 32 bits.  In other libraries, it may be as low as @code{32767}.
1546 @end deftypevr
1548 @deftypefun int rand (void)
1549 @standards{ISO, stdlib.h}
1550 @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
1551 @c Just calls random.
1552 The @code{rand} function returns the next pseudo-random number in the
1553 series.  The value ranges from @code{0} to @code{RAND_MAX}.
1554 @end deftypefun
1556 @deftypefun void srand (unsigned int @var{seed})
1557 @standards{ISO, stdlib.h}
1558 @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
1559 @c Alias to srandom.
1560 This function establishes @var{seed} as the seed for a new series of
1561 pseudo-random numbers.  If you call @code{rand} before a seed has been
1562 established with @code{srand}, it uses the value @code{1} as a default
1563 seed.
1565 To produce a different pseudo-random series each time your program is
1566 run, do @code{srand (time (0))}.
1567 @end deftypefun
1569 POSIX.1 extended the C standard functions to support reproducible random
1570 numbers in multi-threaded programs.  However, the extension is badly
1571 designed and unsuitable for serious work.
1573 @deftypefun int rand_r (unsigned int *@var{seed})
1574 @standards{POSIX.1, stdlib.h}
1575 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1576 This function returns a random number in the range 0 to @code{RAND_MAX}
1577 just as @code{rand} does.  However, all its state is stored in the
1578 @var{seed} argument.  This means the RNG's state can only have as many
1579 bits as the type @code{unsigned int} has.  This is far too few to
1580 provide a good RNG.
1582 If your program requires a reentrant RNG, we recommend you use the
1583 reentrant GNU extensions to the SVID random number generator.  The
1584 POSIX.1 interface should only be used when the GNU extensions are not
1585 available.
1586 @end deftypefun
1589 @node BSD Random
1590 @subsection BSD Random Number Functions
1592 This section describes a set of random number generation functions that
1593 are derived from BSD.  There is no advantage to using these functions
1594 with @theglibc{}; we support them for BSD compatibility only.
1596 The prototypes for these functions are in @file{stdlib.h}.
1597 @pindex stdlib.h
1599 @deftypefun {long int} random (void)
1600 @standards{BSD, stdlib.h}
1601 @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
1602 @c Takes a lock and calls random_r with an automatic variable and the
1603 @c global state, while holding a lock.
1604 This function returns the next pseudo-random number in the sequence.
1605 The value returned ranges from @code{0} to @code{2147483647}.
1607 @strong{NB:} Temporarily this function was defined to return a
1608 @code{int32_t} value to indicate that the return value always contains
1609 32 bits even if @code{long int} is wider.  The standard demands it
1610 differently.  Users must always be aware of the 32-bit limitation,
1611 though.
1612 @end deftypefun
1614 @deftypefun void srandom (unsigned int @var{seed})
1615 @standards{BSD, stdlib.h}
1616 @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
1617 @c Takes a lock and calls srandom_r with an automatic variable and a
1618 @c static buffer.  There's no MT-safety issue because the static buffer
1619 @c is internally protected by a lock, although other threads may modify
1620 @c the set state before it is used.
1621 The @code{srandom} function sets the state of the random number
1622 generator based on the integer @var{seed}.  If you supply a @var{seed} value
1623 of @code{1}, this will cause @code{random} to reproduce the default set
1624 of random numbers.
1626 To produce a different set of pseudo-random numbers each time your
1627 program runs, do @code{srandom (time (0))}.
1628 @end deftypefun
1630 @deftypefun {char *} initstate (unsigned int @var{seed}, char *@var{state}, size_t @var{size})
1631 @standards{BSD, stdlib.h}
1632 @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
1633 The @code{initstate} function is used to initialize the random number
1634 generator state.  The argument @var{state} is an array of @var{size}
1635 bytes, used to hold the state information.  It is initialized based on
1636 @var{seed}.  The size must be between 8 and 256 bytes, and should be a
1637 power of two.  The bigger the @var{state} array, the better.
1639 The return value is the previous value of the state information array.
1640 You can use this value later as an argument to @code{setstate} to
1641 restore that state.
1642 @end deftypefun
1644 @deftypefun {char *} setstate (char *@var{state})
1645 @standards{BSD, stdlib.h}
1646 @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
1647 The @code{setstate} function restores the random number state
1648 information @var{state}.  The argument must have been the result of
1649 a previous call to @var{initstate} or @var{setstate}.
1651 The return value is the previous value of the state information array.
1652 You can use this value later as an argument to @code{setstate} to
1653 restore that state.
1655 If the function fails the return value is @code{NULL}.
1656 @end deftypefun
1658 The four functions described so far in this section all work on a state
1659 which is shared by all threads.  The state is not directly accessible to
1660 the user and can only be modified by these functions.  This makes it
1661 hard to deal with situations where each thread should have its own
1662 pseudo-random number generator.
1664 @Theglibc{} contains four additional functions which contain the
1665 state as an explicit parameter and therefore make it possible to handle
1666 thread-local PRNGs.  Besides this there is no difference.  In fact, the
1667 four functions already discussed are implemented internally using the
1668 following interfaces.
1670 The @file{stdlib.h} header contains a definition of the following type:
1672 @deftp {Data Type} {struct random_data}
1673 @standards{GNU, stdlib.h}
1675 Objects of type @code{struct random_data} contain the information
1676 necessary to represent the state of the PRNG.  Although a complete
1677 definition of the type is present the type should be treated as opaque.
1678 @end deftp
1680 The functions modifying the state follow exactly the already described
1681 functions.
1683 @deftypefun int random_r (struct random_data *restrict @var{buf}, int32_t *restrict @var{result})
1684 @standards{GNU, stdlib.h}
1685 @safety{@prelim{}@mtsafe{@mtsrace{:buf}}@assafe{}@acunsafe{@acucorrupt{}}}
1686 The @code{random_r} function behaves exactly like the @code{random}
1687 function except that it uses and modifies the state in the object
1688 pointed to by the first parameter instead of the global state.
1689 @end deftypefun
1691 @deftypefun int srandom_r (unsigned int @var{seed}, struct random_data *@var{buf})
1692 @standards{GNU, stdlib.h}
1693 @safety{@prelim{}@mtsafe{@mtsrace{:buf}}@assafe{}@acunsafe{@acucorrupt{}}}
1694 The @code{srandom_r} function behaves exactly like the @code{srandom}
1695 function except that it uses and modifies the state in the object
1696 pointed to by the second parameter instead of the global state.
1697 @end deftypefun
1699 @deftypefun int initstate_r (unsigned int @var{seed}, char *restrict @var{statebuf}, size_t @var{statelen}, struct random_data *restrict @var{buf})
1700 @standards{GNU, stdlib.h}
1701 @safety{@prelim{}@mtsafe{@mtsrace{:buf}}@assafe{}@acunsafe{@acucorrupt{}}}
1702 The @code{initstate_r} function behaves exactly like the @code{initstate}
1703 function except that it uses and modifies the state in the object
1704 pointed to by the fourth parameter instead of the global state.
1705 @end deftypefun
1707 @deftypefun int setstate_r (char *restrict @var{statebuf}, struct random_data *restrict @var{buf})
1708 @standards{GNU, stdlib.h}
1709 @safety{@prelim{}@mtsafe{@mtsrace{:buf}}@assafe{}@acunsafe{@acucorrupt{}}}
1710 The @code{setstate_r} function behaves exactly like the @code{setstate}
1711 function except that it uses and modifies the state in the object
1712 pointed to by the first parameter instead of the global state.
1713 @end deftypefun
1715 @node SVID Random
1716 @subsection SVID Random Number Function
1718 The C library on SVID systems contains yet another kind of random number
1719 generator functions.  They use a state of 48 bits of data.  The user can
1720 choose among a collection of functions which return the random bits
1721 in different forms.
1723 Generally there are two kinds of function.  The first uses a state of
1724 the random number generator which is shared among several functions and
1725 by all threads of the process.  The second requires the user to handle
1726 the state.
1728 All functions have in common that they use the same congruential
1729 formula with the same constants.  The formula is
1731 @smallexample
1732 Y = (a * X + c) mod m
1733 @end smallexample
1735 @noindent
1736 where @var{X} is the state of the generator at the beginning and
1737 @var{Y} the state at the end.  @code{a} and @code{c} are constants
1738 determining the way the generator works.  By default they are
1740 @smallexample
1741 a = 0x5DEECE66D = 25214903917
1742 c = 0xb = 11
1743 @end smallexample
1745 @noindent
1746 but they can also be changed by the user.  @code{m} is of course 2^48
1747 since the state consists of a 48-bit array.
1749 The prototypes for these functions are in @file{stdlib.h}.
1750 @pindex stdlib.h
1753 @deftypefun double drand48 (void)
1754 @standards{SVID, stdlib.h}
1755 @safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
1756 @c Uses of the static state buffer are not guarded by a lock (thus
1757 @c @mtasurace:drand48), so they may be found or left at a
1758 @c partially-updated state in case of calls from within signal handlers
1759 @c or cancellation.  None of this will break safety rules or invoke
1760 @c undefined behavior, but it may affect randomness.
1761 This function returns a @code{double} value in the range of @code{0.0}
1762 to @code{1.0} (exclusive).  The random bits are determined by the global
1763 state of the random number generator in the C library.
1765 Since the @code{double} type according to @w{IEEE 754} has a 52-bit
1766 mantissa this means 4 bits are not initialized by the random number
1767 generator.  These are (of course) chosen to be the least significant
1768 bits and they are initialized to @code{0}.
1769 @end deftypefun
1771 @deftypefun double erand48 (unsigned short int @var{xsubi}[3])
1772 @standards{SVID, stdlib.h}
1773 @safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
1774 @c The static buffer is just initialized with default parameters, which
1775 @c are later read to advance the state held in xsubi.
1776 This function returns a @code{double} value in the range of @code{0.0}
1777 to @code{1.0} (exclusive), similarly to @code{drand48}.  The argument is
1778 an array describing the state of the random number generator.
1780 This function can be called subsequently since it updates the array to
1781 guarantee random numbers.  The array should have been initialized before
1782 initial use to obtain reproducible results.
1783 @end deftypefun
1785 @deftypefun {long int} lrand48 (void)
1786 @standards{SVID, stdlib.h}
1787 @safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
1788 The @code{lrand48} function returns an integer value in the range of
1789 @code{0} to @code{2^31} (exclusive).  Even if the size of the @code{long
1790 int} type can take more than 32 bits, no higher numbers are returned.
1791 The random bits are determined by the global state of the random number
1792 generator in the C library.
1793 @end deftypefun
1795 @deftypefun {long int} nrand48 (unsigned short int @var{xsubi}[3])
1796 @standards{SVID, stdlib.h}
1797 @safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
1798 This function is similar to the @code{lrand48} function in that it
1799 returns a number in the range of @code{0} to @code{2^31} (exclusive) but
1800 the state of the random number generator used to produce the random bits
1801 is determined by the array provided as the parameter to the function.
1803 The numbers in the array are updated afterwards so that subsequent calls
1804 to this function yield different results (as is expected of a random
1805 number generator).  The array should have been initialized before the
1806 first call to obtain reproducible results.
1807 @end deftypefun
1809 @deftypefun {long int} mrand48 (void)
1810 @standards{SVID, stdlib.h}
1811 @safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
1812 The @code{mrand48} function is similar to @code{lrand48}.  The only
1813 difference is that the numbers returned are in the range @code{-2^31} to
1814 @code{2^31} (exclusive).
1815 @end deftypefun
1817 @deftypefun {long int} jrand48 (unsigned short int @var{xsubi}[3])
1818 @standards{SVID, stdlib.h}
1819 @safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
1820 The @code{jrand48} function is similar to @code{nrand48}.  The only
1821 difference is that the numbers returned are in the range @code{-2^31} to
1822 @code{2^31} (exclusive).  For the @code{xsubi} parameter the same
1823 requirements are necessary.
1824 @end deftypefun
1826 The internal state of the random number generator can be initialized in
1827 several ways.  The methods differ in the completeness of the
1828 information provided.
1830 @deftypefun void srand48 (long int @var{seedval})
1831 @standards{SVID, stdlib.h}
1832 @safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
1833 The @code{srand48} function sets the most significant 32 bits of the
1834 internal state of the random number generator to the least
1835 significant 32 bits of the @var{seedval} parameter.  The lower 16 bits
1836 are initialized to the value @code{0x330E}.  Even if the @code{long
1837 int} type contains more than 32 bits only the lower 32 bits are used.
1839 Owing to this limitation, initialization of the state of this
1840 function is not very useful.  But it makes it easy to use a construct
1841 like @code{srand48 (time (0))}.
1843 A side-effect of this function is that the values @code{a} and @code{c}
1844 from the internal state, which are used in the congruential formula,
1845 are reset to the default values given above.  This is of importance once
1846 the user has called the @code{lcong48} function (see below).
1847 @end deftypefun
1849 @deftypefun {unsigned short int *} seed48 (unsigned short int @var{seed16v}[3])
1850 @standards{SVID, stdlib.h}
1851 @safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
1852 The @code{seed48} function initializes all 48 bits of the state of the
1853 internal random number generator from the contents of the parameter
1854 @var{seed16v}.  Here the lower 16 bits of the first element of
1855 @var{seed16v} initialize the least significant 16 bits of the internal
1856 state, the lower 16 bits of @code{@var{seed16v}[1]} initialize the mid-order
1857 16 bits of the state and the 16 lower bits of @code{@var{seed16v}[2]}
1858 initialize the most significant 16 bits of the state.
1860 Unlike @code{srand48} this function lets the user initialize all 48 bits
1861 of the state.
1863 The value returned by @code{seed48} is a pointer to an array containing
1864 the values of the internal state before the change.  This might be
1865 useful to restart the random number generator at a certain state.
1866 Otherwise the value can simply be ignored.
1868 As for @code{srand48}, the values @code{a} and @code{c} from the
1869 congruential formula are reset to the default values.
1870 @end deftypefun
1872 There is one more function to initialize the random number generator
1873 which enables you to specify even more information by allowing you to
1874 change the parameters in the congruential formula.
1876 @deftypefun void lcong48 (unsigned short int @var{param}[7])
1877 @standards{SVID, stdlib.h}
1878 @safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
1879 The @code{lcong48} function allows the user to change the complete state
1880 of the random number generator.  Unlike @code{srand48} and
1881 @code{seed48}, this function also changes the constants in the
1882 congruential formula.
1884 From the seven elements in the array @var{param} the least significant
1885 16 bits of the entries @code{@var{param}[0]} to @code{@var{param}[2]}
1886 determine the initial state, the least significant 16 bits of
1887 @code{@var{param}[3]} to @code{@var{param}[5]} determine the 48 bit
1888 constant @code{a} and @code{@var{param}[6]} determines the 16-bit value
1889 @code{c}.
1890 @end deftypefun
1892 All the above functions have in common that they use the global
1893 parameters for the congruential formula.  In multi-threaded programs it
1894 might sometimes be useful to have different parameters in different
1895 threads.  For this reason all the above functions have a counterpart
1896 which works on a description of the random number generator in the
1897 user-supplied buffer instead of the global state.
1899 Please note that it is no problem if several threads use the global
1900 state if all threads use the functions which take a pointer to an array
1901 containing the state.  The random numbers are computed following the
1902 same loop but if the state in the array is different all threads will
1903 obtain an individual random number generator.
1905 The user-supplied buffer must be of type @code{struct drand48_data}.
1906 This type should be regarded as opaque and not manipulated directly.
1908 @deftypefun int drand48_r (struct drand48_data *@var{buffer}, double *@var{result})
1909 @standards{GNU, stdlib.h}
1910 @safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
1911 This function is equivalent to the @code{drand48} function with the
1912 difference that it does not modify the global random number generator
1913 parameters but instead the parameters in the buffer supplied through the
1914 pointer @var{buffer}.  The random number is returned in the variable
1915 pointed to by @var{result}.
1917 The return value of the function indicates whether the call succeeded.
1918 If the value is less than @code{0} an error occurred and @code{errno} is
1919 set to indicate the problem.
1921 This function is a GNU extension and should not be used in portable
1922 programs.
1923 @end deftypefun
1925 @deftypefun int erand48_r (unsigned short int @var{xsubi}[3], struct drand48_data *@var{buffer}, double *@var{result})
1926 @standards{GNU, stdlib.h}
1927 @safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
1928 The @code{erand48_r} function works like @code{erand48}, but in addition
1929 it takes an argument @var{buffer} which describes the random number
1930 generator.  The state of the random number generator is taken from the
1931 @code{xsubi} array, the parameters for the congruential formula from the
1932 global random number generator data.  The random number is returned in
1933 the variable pointed to by @var{result}.
1935 The return value is non-negative if the call succeeded.
1937 This function is a GNU extension and should not be used in portable
1938 programs.
1939 @end deftypefun
1941 @deftypefun int lrand48_r (struct drand48_data *@var{buffer}, long int *@var{result})
1942 @standards{GNU, stdlib.h}
1943 @safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
1944 This function is similar to @code{lrand48}, but in addition it takes a
1945 pointer to a buffer describing the state of the random number generator
1946 just like @code{drand48}.
1948 If the return value of the function is non-negative the variable pointed
1949 to by @var{result} contains the result.  Otherwise an error occurred.
1951 This function is a GNU extension and should not be used in portable
1952 programs.
1953 @end deftypefun
1955 @deftypefun int nrand48_r (unsigned short int @var{xsubi}[3], struct drand48_data *@var{buffer}, long int *@var{result})
1956 @standards{GNU, stdlib.h}
1957 @safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
1958 The @code{nrand48_r} function works like @code{nrand48} in that it
1959 produces a random number in the range @code{0} to @code{2^31}.  But instead
1960 of using the global parameters for the congruential formula it uses the
1961 information from the buffer pointed to by @var{buffer}.  The state is
1962 described by the values in @var{xsubi}.
1964 If the return value is non-negative the variable pointed to by
1965 @var{result} contains the result.
1967 This function is a GNU extension and should not be used in portable
1968 programs.
1969 @end deftypefun
1971 @deftypefun int mrand48_r (struct drand48_data *@var{buffer}, long int *@var{result})
1972 @standards{GNU, stdlib.h}
1973 @safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
1974 This function is similar to @code{mrand48} but like the other reentrant
1975 functions it uses the random number generator described by the value in
1976 the buffer pointed to by @var{buffer}.
1978 If the return value is non-negative the variable pointed to by
1979 @var{result} contains the result.
1981 This function is a GNU extension and should not be used in portable
1982 programs.
1983 @end deftypefun
1985 @deftypefun int jrand48_r (unsigned short int @var{xsubi}[3], struct drand48_data *@var{buffer}, long int *@var{result})
1986 @standards{GNU, stdlib.h}
1987 @safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
1988 The @code{jrand48_r} function is similar to @code{jrand48}.  Like the
1989 other reentrant functions of this function family it uses the
1990 congruential formula parameters from the buffer pointed to by
1991 @var{buffer}.
1993 If the return value is non-negative the variable pointed to by
1994 @var{result} contains the result.
1996 This function is a GNU extension and should not be used in portable
1997 programs.
1998 @end deftypefun
2000 Before any of the above functions are used the buffer of type
2001 @code{struct drand48_data} should be initialized.  The easiest way to do
2002 this is to fill the whole buffer with null bytes, e.g. by
2004 @smallexample
2005 memset (buffer, '\0', sizeof (struct drand48_data));
2006 @end smallexample
2008 @noindent
2009 Using any of the reentrant functions of this family now will
2010 automatically initialize the random number generator to the default
2011 values for the state and the parameters of the congruential formula.
2013 The other possibility is to use any of the functions which explicitly
2014 initialize the buffer.  Though it might be obvious how to initialize the
2015 buffer from looking at the parameter to the function, it is highly
2016 recommended to use these functions since the result might not always be
2017 what you expect.
2019 @deftypefun int srand48_r (long int @var{seedval}, struct drand48_data *@var{buffer})
2020 @standards{GNU, stdlib.h}
2021 @safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
2022 The description of the random number generator represented by the
2023 information in @var{buffer} is initialized similarly to what the function
2024 @code{srand48} does.  The state is initialized from the parameter
2025 @var{seedval} and the parameters for the congruential formula are
2026 initialized to their default values.
2028 If the return value is non-negative the function call succeeded.
2030 This function is a GNU extension and should not be used in portable
2031 programs.
2032 @end deftypefun
2034 @deftypefun int seed48_r (unsigned short int @var{seed16v}[3], struct drand48_data *@var{buffer})
2035 @standards{GNU, stdlib.h}
2036 @safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
2037 This function is similar to @code{srand48_r} but like @code{seed48} it
2038 initializes all 48 bits of the state from the parameter @var{seed16v}.
2040 If the return value is non-negative the function call succeeded.  It
2041 does not return a pointer to the previous state of the random number
2042 generator like the @code{seed48} function does.  If the user wants to
2043 preserve the state for a later re-run s/he can copy the whole buffer
2044 pointed to by @var{buffer}.
2046 This function is a GNU extension and should not be used in portable
2047 programs.
2048 @end deftypefun
2050 @deftypefun int lcong48_r (unsigned short int @var{param}[7], struct drand48_data *@var{buffer})
2051 @standards{GNU, stdlib.h}
2052 @safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
2053 This function initializes all aspects of the random number generator
2054 described in @var{buffer} with the data in @var{param}.  Here it is
2055 especially true that the function does more than just copying the
2056 contents of @var{param} and @var{buffer}.  More work is required and
2057 therefore it is important to use this function rather than initializing
2058 the random number generator directly.
2060 If the return value is non-negative the function call succeeded.
2062 This function is a GNU extension and should not be used in portable
2063 programs.
2064 @end deftypefun
2066 @node High Quality Random
2067 @subsection High Quality Random Number Functions
2069 This section describes the random number functions provided as a GNU
2070 extension, based on OpenBSD interfaces.
2072 @Theglibc{} uses kernel entropy obtained either through @code{getrandom}
2073 or by reading @file{/dev/urandom} to seed.
2075 These functions provide higher random quality than ISO, BSD, and SVID
2076 functions, and may be used in cryptographic contexts.
2078 The prototypes for these functions are in @file{stdlib.h}.
2079 @pindex stdlib.h
2081 @deftypefun uint32_t arc4random (void)
2082 @standards{BSD, stdlib.h}
2083 @safety{@mtsafe{}@asunsafe{@asucorrupt{}}@acsafe{}}
2084 This function returns a single 32-bit value in the range of @code{0} to
2085 @code{2^32−1} (inclusive), which is twice the range of @code{rand} and
2086 @code{random}.
2087 @end deftypefun
2089 @deftypefun void arc4random_buf (void *@var{buffer}, size_t @var{length})
2090 @standards{BSD, stdlib.h}
2091 @safety{@mtsafe{}@asunsafe{@asucorrupt{}}@acsafe{}}
2092 This function fills the region @var{buffer} of length @var{length} bytes
2093 with random data.
2094 @end deftypefun
2096 @deftypefun uint32_t arc4random_uniform (uint32_t @var{upper_bound})
2097 @standards{BSD, stdlib.h}
2098 @safety{@mtsafe{}@asunsafe{@asucorrupt{}}@acsafe{}}
2099 This function returns a single 32-bit value, uniformly distributed but
2100 less than the @var{upper_bound}.  It avoids the @w{modulo bias} when the
2101 upper bound is not a power of two.
2102 @end deftypefun
2104 @node FP Function Optimizations
2105 @section Is Fast Code or Small Code preferred?
2106 @cindex Optimization
2108 If an application uses many floating point functions it is often the case
2109 that the cost of the function calls themselves is not negligible.
2110 Modern processors can often execute the operations themselves
2111 very fast, but the function call disrupts the instruction pipeline.
2113 For this reason @theglibc{} provides optimizations for many of the
2114 frequently-used math functions.  When GNU CC is used and the user
2115 activates the optimizer, several new inline functions and macros are
2116 defined.  These new functions and macros have the same names as the
2117 library functions and so are used instead of the latter.  In the case of
2118 inline functions the compiler will decide whether it is reasonable to
2119 use them, and this decision is usually correct.
2121 This means that no calls to the library functions may be necessary, and
2122 can increase the speed of generated code significantly.  The drawback is
2123 that code size will increase, and the increase is not always negligible.
2125 There are two kinds of inline functions: those that give the same result
2126 as the library functions and others that might not set @code{errno} and
2127 might have a reduced precision and/or argument range in comparison with
2128 the library functions.  The latter inline functions are only available
2129 if the flag @code{-ffast-math} is given to GNU CC.
2131 Not all hardware implements the entire @w{IEEE 754} standard, and even
2132 if it does there may be a substantial performance penalty for using some
2133 of its features.  For example, enabling traps on some processors forces
2134 the FPU to run un-pipelined, which can more than double calculation time.
2135 @c ***Add explanation of -lieee, -mieee.