1 @c We need some definitions here.
30 @node Mathematics, Arithmetic, Syslog, Top
31 @c %MENU% Math functions, useful constants, random numbers
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
42 All mathematical functions which take a floating-point argument
43 have three variants, one each for @code{double}, @code{float}, and
44 @code{long double} arguments. The @code{double} versions are mostly
45 defined in @w{ISO C89}. The @code{float} and @code{long double}
46 versions are from the numeric extensions to C included in @w{ISO C99}.
48 Which of the three versions of a function should be used depends on the
49 situation. For most calculations, the @code{float} functions are the
50 fastest. On the other hand, the @code{long double} functions have the
51 highest precision. @code{double} is somewhere in between. It is
52 usually wise to pick the narrowest type that can accommodate your data.
53 Not all machines have a distinct @code{long double} type; it may be the
54 same as @code{double}.
56 On some machines, @theglibc{} also provides @code{_Float@var{N}} and
57 @code{_Float@var{N}x} types. These types are defined in @w{ISO/IEC TS
58 18661-3}, which extends @w{ISO C} and defines floating-point types that
59 are not machine-dependent. When such a type, such as @code{_Float128},
60 is supported by @theglibc{}, extra variants for most of the mathematical
61 functions provided for @code{double}, @code{float}, and @code{long
62 double} are also provided for the supported type. Throughout this
63 manual, the @code{_Float@var{N}} and @code{_Float@var{N}x} variants of
64 these functions are described along with the @code{double},
65 @code{float}, and @code{long double} variants and they come from
66 @w{ISO/IEC TS 18661-3}, unless explicitly stated otherwise.
68 Currently, support for @code{_Float@var{N}} or @code{_Float@var{N}x}
69 types is only provided for @code{_Float128} on powerpc64le (PowerPC
70 64-bits little-endian), x86_64, x86 and ia64.
73 * Mathematical Constants:: Precise numeric values for often-used
75 * Trig Functions:: Sine, cosine, tangent, and friends.
76 * Inverse Trig Functions:: Arcsine, arccosine, etc.
77 * Exponents and Logarithms:: Also pow and sqrt.
78 * Hyperbolic Functions:: sinh, cosh, tanh, etc.
79 * Special Functions:: Bessel, gamma, erf.
80 * Errors in Math Functions:: Known Maximum Errors in Math Functions.
81 * Pseudo-Random Numbers:: Functions for generating pseudo-random
83 * FP Function Optimizations:: Fast code or small code.
86 @node Mathematical Constants
87 @section Predefined Mathematical Constants
89 @cindex mathematical constants
91 The header @file{math.h} defines several useful mathematical constants.
92 All values are defined as preprocessor macros starting with @code{M_}.
93 The values provided are:
97 The base of natural logarithms.
99 The logarithm to base @code{2} of @code{M_E}.
101 The logarithm to base @code{10} of @code{M_E}.
103 The natural logarithm of @code{2}.
105 The natural logarithm of @code{10}.
107 Pi, the ratio of a circle's circumference to its diameter.
113 The reciprocal of pi (1/pi)
115 Two times the reciprocal of pi.
117 Two times the reciprocal of the square root of pi.
119 The square root of two.
121 The reciprocal of the square root of two (also the square root of 1/2).
124 These constants come from the Unix98 standard and were also available in
125 4.4BSD; therefore they are only defined if
126 @code{_XOPEN_SOURCE=500}, or a more general feature select macro, is
127 defined. The default set of features includes these constants.
128 @xref{Feature Test Macros}.
130 All values are of type @code{double}. As an extension, @theglibc{}
131 also defines these constants with type @code{long double}. The
132 @code{long double} macros have a lowercase @samp{l} appended to their
133 names: @code{M_El}, @code{M_PIl}, and so forth. These are only
134 available if @code{_GNU_SOURCE} is defined.
136 Likewise, @theglibc{} also defines these constants with the types
137 @code{_Float@var{N}} and @code{_Float@var{N}x} for the machines that
138 have support for such types enabled (@pxref{Mathematics}) and if
139 @code{_GNU_SOURCE} is defined. When available, the macros names are
140 appended with @samp{f@var{N}} or @samp{f@var{N}x}, such as @samp{f128}
141 for the type @code{_Float128}.
144 @emph{Note:} Some programs use a constant named @code{PI} which has the
145 same value as @code{M_PI}. This constant is not standard; it may have
146 appeared in some old AT&T headers, and is mentioned in Stroustrup's book
147 on C++. It infringes on the user's name space, so @theglibc{}
148 does not define it. Fixing programs written to expect it is simple:
149 replace @code{PI} with @code{M_PI} throughout, or put @samp{-DPI=M_PI}
150 on the compiler command line.
153 @section Trigonometric Functions
154 @cindex trigonometric functions
156 These are the familiar @code{sin}, @code{cos}, and @code{tan} functions.
157 The arguments to all of these functions are in units of radians; recall
158 that pi radians equals 180 degrees.
160 @cindex pi (trigonometric constant)
161 The math library normally defines @code{M_PI} to a @code{double}
162 approximation of pi. If strict ISO and/or POSIX compliance
163 are requested this constant is not defined, but you can easily define it
167 #define M_PI 3.14159265358979323846264338327
171 You can also compute the value of pi with the expression @code{acos
174 @deftypefun double sin (double @var{x})
175 @deftypefunx float sinf (float @var{x})
176 @deftypefunx {long double} sinl (long double @var{x})
177 @deftypefunx _FloatN sinfN (_Float@var{N} @var{x})
178 @deftypefunx _FloatNx sinfNx (_Float@var{N}x @var{x})
179 @standards{ISO, math.h}
180 @standardsx{sinfN, TS 18661-3:2015, math.h}
181 @standardsx{sinfNx, TS 18661-3:2015, math.h}
182 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
183 These functions return the sine of @var{x}, where @var{x} is given in
184 radians. The return value is in the range @code{-1} to @code{1}.
187 @deftypefun double cos (double @var{x})
188 @deftypefunx float cosf (float @var{x})
189 @deftypefunx {long double} cosl (long double @var{x})
190 @deftypefunx _FloatN cosfN (_Float@var{N} @var{x})
191 @deftypefunx _FloatNx cosfNx (_Float@var{N}x @var{x})
192 @standards{ISO, math.h}
193 @standardsx{cosfN, TS 18661-3:2015, math.h}
194 @standardsx{cosfNx, TS 18661-3:2015, math.h}
195 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
196 These functions return the cosine of @var{x}, where @var{x} is given in
197 radians. The return value is in the range @code{-1} to @code{1}.
200 @deftypefun double tan (double @var{x})
201 @deftypefunx float tanf (float @var{x})
202 @deftypefunx {long double} tanl (long double @var{x})
203 @deftypefunx _FloatN tanfN (_Float@var{N} @var{x})
204 @deftypefunx _FloatNx tanfNx (_Float@var{N}x @var{x})
205 @standards{ISO, math.h}
206 @standardsx{tanfN, TS 18661-3:2015, math.h}
207 @standardsx{tanfNx, TS 18661-3:2015, math.h}
208 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
209 These functions return the tangent of @var{x}, where @var{x} is given in
212 Mathematically, the tangent function has singularities at odd multiples
213 of pi/2. If the argument @var{x} is too close to one of these
214 singularities, @code{tan} will signal overflow.
217 In many applications where @code{sin} and @code{cos} are used, the sine
218 and cosine of the same angle are needed at the same time. It is more
219 efficient to compute them simultaneously, so the library provides a
222 @deftypefun void sincos (double @var{x}, double *@var{sinx}, double *@var{cosx})
223 @deftypefunx void sincosf (float @var{x}, float *@var{sinx}, float *@var{cosx})
224 @deftypefunx void sincosl (long double @var{x}, long double *@var{sinx}, long double *@var{cosx})
225 @deftypefunx _FloatN sincosfN (_Float@var{N} @var{x}, _Float@var{N} *@var{sinx}, _Float@var{N} *@var{cosx})
226 @deftypefunx _FloatNx sincosfNx (_Float@var{N}x @var{x}, _Float@var{N}x *@var{sinx}, _Float@var{N}x *@var{cosx})
227 @standards{GNU, math.h}
228 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
229 These functions return the sine of @var{x} in @code{*@var{sinx}} and the
230 cosine of @var{x} in @code{*@var{cosx}}, where @var{x} is given in
231 radians. Both values, @code{*@var{sinx}} and @code{*@var{cosx}}, are in
232 the range of @code{-1} to @code{1}.
234 All these functions, including the @code{_Float@var{N}} and
235 @code{_Float@var{N}x} variants, are GNU extensions. Portable programs
236 should be prepared to cope with their absence.
239 @cindex complex trigonometric functions
241 @w{ISO C99} defines variants of the trig functions which work on
242 complex numbers. @Theglibc{} provides these functions, but they
243 are only useful if your compiler supports the new complex types defined
245 @c XXX Change this when gcc is fixed. -zw
246 (As of this writing GCC supports complex numbers, but there are bugs in
249 @deftypefun {complex double} csin (complex double @var{z})
250 @deftypefunx {complex float} csinf (complex float @var{z})
251 @deftypefunx {complex long double} csinl (complex long double @var{z})
252 @deftypefunx {complex _FloatN} csinfN (complex _Float@var{N} @var{z})
253 @deftypefunx {complex _FloatNx} csinfNx (complex _Float@var{N}x @var{z})
254 @standards{ISO, complex.h}
255 @standardsx{csinfN, TS 18661-3:2015, complex.h}
256 @standardsx{csinfNx, TS 18661-3:2015, complex.h}
257 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
258 @c There are calls to nan* that could trigger @mtslocale if they didn't get
260 These functions return the complex sine of @var{z}.
261 The mathematical definition of the complex sine is
264 @math{sin (z) = 1/(2*i) * (exp (z*i) - exp (-z*i))}.
267 $$\sin(z) = {1\over 2i} (e^{zi} - e^{-zi})$$
271 @deftypefun {complex double} ccos (complex double @var{z})
272 @deftypefunx {complex float} ccosf (complex float @var{z})
273 @deftypefunx {complex long double} ccosl (complex long double @var{z})
274 @deftypefunx {complex _FloatN} ccosfN (complex _Float@var{N} @var{z})
275 @deftypefunx {complex _FloatNx} ccosfNx (complex _Float@var{N}x @var{z})
276 @standards{ISO, complex.h}
277 @standardsx{ccosfN, TS 18661-3:2015, complex.h}
278 @standardsx{ccosfNx, TS 18661-3:2015, complex.h}
279 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
280 These functions return the complex cosine of @var{z}.
281 The mathematical definition of the complex cosine is
284 @math{cos (z) = 1/2 * (exp (z*i) + exp (-z*i))}
287 $$\cos(z) = {1\over 2} (e^{zi} + e^{-zi})$$
291 @deftypefun {complex double} ctan (complex double @var{z})
292 @deftypefunx {complex float} ctanf (complex float @var{z})
293 @deftypefunx {complex long double} ctanl (complex long double @var{z})
294 @deftypefunx {complex _FloatN} ctanfN (complex _Float@var{N} @var{z})
295 @deftypefunx {complex _FloatNx} ctanfNx (complex _Float@var{N}x @var{z})
296 @standards{ISO, complex.h}
297 @standardsx{ctanfN, TS 18661-3:2015, complex.h}
298 @standardsx{ctanfNx, TS 18661-3:2015, complex.h}
299 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
300 These functions return the complex tangent of @var{z}.
301 The mathematical definition of the complex tangent is
304 @math{tan (z) = -i * (exp (z*i) - exp (-z*i)) / (exp (z*i) + exp (-z*i))}
307 $$\tan(z) = -i \cdot {e^{zi} - e^{-zi}\over e^{zi} + e^{-zi}}$$
311 The complex tangent has poles at @math{pi/2 + 2n}, where @math{n} is an
312 integer. @code{ctan} may signal overflow if @var{z} is too close to a
317 @node Inverse Trig Functions
318 @section Inverse Trigonometric Functions
319 @cindex inverse trigonometric functions
321 These are the usual arcsine, arccosine and arctangent functions,
322 which are the inverses of the sine, cosine and tangent functions
325 @deftypefun double asin (double @var{x})
326 @deftypefunx float asinf (float @var{x})
327 @deftypefunx {long double} asinl (long double @var{x})
328 @deftypefunx _FloatN asinfN (_Float@var{N} @var{x})
329 @deftypefunx _FloatNx asinfNx (_Float@var{N}x @var{x})
330 @standards{ISO, math.h}
331 @standardsx{asinfN, TS 18661-3:2015, math.h}
332 @standardsx{asinfNx, TS 18661-3:2015, math.h}
333 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
334 These functions compute the arcsine of @var{x}---that is, the value whose
335 sine is @var{x}. The value is in units of radians. Mathematically,
336 there are infinitely many such values; the one actually returned is the
337 one between @code{-pi/2} and @code{pi/2} (inclusive).
339 The arcsine function is defined mathematically only
340 over the domain @code{-1} to @code{1}. If @var{x} is outside the
341 domain, @code{asin} signals a domain error.
344 @deftypefun double acos (double @var{x})
345 @deftypefunx float acosf (float @var{x})
346 @deftypefunx {long double} acosl (long double @var{x})
347 @deftypefunx _FloatN acosfN (_Float@var{N} @var{x})
348 @deftypefunx _FloatNx acosfNx (_Float@var{N}x @var{x})
349 @standards{ISO, math.h}
350 @standardsx{acosfN, TS 18661-3:2015, math.h}
351 @standardsx{acosfNx, TS 18661-3:2015, math.h}
352 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
353 These functions compute the arccosine of @var{x}---that is, the value
354 whose cosine is @var{x}. The value is in units of radians.
355 Mathematically, there are infinitely many such values; the one actually
356 returned is the one between @code{0} and @code{pi} (inclusive).
358 The arccosine function is defined mathematically only
359 over the domain @code{-1} to @code{1}. If @var{x} is outside the
360 domain, @code{acos} signals a domain error.
363 @deftypefun double atan (double @var{x})
364 @deftypefunx float atanf (float @var{x})
365 @deftypefunx {long double} atanl (long double @var{x})
366 @deftypefunx _FloatN atanfN (_Float@var{N} @var{x})
367 @deftypefunx _FloatNx atanfNx (_Float@var{N}x @var{x})
368 @standards{ISO, math.h}
369 @standardsx{atanfN, TS 18661-3:2015, math.h}
370 @standardsx{atanfNx, TS 18661-3:2015, math.h}
371 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
372 These functions compute the arctangent of @var{x}---that is, the value
373 whose tangent is @var{x}. The value is in units of radians.
374 Mathematically, there are infinitely many such values; the one actually
375 returned is the one between @code{-pi/2} and @code{pi/2} (inclusive).
378 @deftypefun double atan2 (double @var{y}, double @var{x})
379 @deftypefunx float atan2f (float @var{y}, float @var{x})
380 @deftypefunx {long double} atan2l (long double @var{y}, long double @var{x})
381 @deftypefunx _FloatN atan2fN (_Float@var{N} @var{y}, _Float@var{N} @var{x})
382 @deftypefunx _FloatNx atan2fNx (_Float@var{N}x @var{y}, _Float@var{N}x @var{x})
383 @standards{ISO, math.h}
384 @standardsx{atan2fN, TS 18661-3:2015, math.h}
385 @standardsx{atan2fNx, TS 18661-3:2015, math.h}
386 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
387 This function computes the arctangent of @var{y}/@var{x}, but the signs
388 of both arguments are used to determine the quadrant of the result, and
389 @var{x} is permitted to be zero. The return value is given in radians
390 and is in the range @code{-pi} to @code{pi}, inclusive.
392 If @var{x} and @var{y} are coordinates of a point in the plane,
393 @code{atan2} returns the signed angle between the line from the origin
394 to that point and the x-axis. Thus, @code{atan2} is useful for
395 converting Cartesian coordinates to polar coordinates. (To compute the
396 radial coordinate, use @code{hypot}; see @ref{Exponents and
399 @c This is experimentally true. Should it be so? -zw
400 If both @var{x} and @var{y} are zero, @code{atan2} returns zero.
403 @cindex inverse complex trigonometric functions
404 @w{ISO C99} defines complex versions of the inverse trig functions.
406 @deftypefun {complex double} casin (complex double @var{z})
407 @deftypefunx {complex float} casinf (complex float @var{z})
408 @deftypefunx {complex long double} casinl (complex long double @var{z})
409 @deftypefunx {complex _FloatN} casinfN (complex _Float@var{N} @var{z})
410 @deftypefunx {complex _FloatNx} casinfNx (complex _Float@var{N}x @var{z})
411 @standards{ISO, complex.h}
412 @standardsx{casinfN, TS 18661-3:2015, complex.h}
413 @standardsx{casinfNx, TS 18661-3:2015, complex.h}
414 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
415 These functions compute the complex arcsine of @var{z}---that is, the
416 value whose sine is @var{z}. The value returned is in radians.
418 Unlike the real-valued functions, @code{casin} is defined for all
422 @deftypefun {complex double} cacos (complex double @var{z})
423 @deftypefunx {complex float} cacosf (complex float @var{z})
424 @deftypefunx {complex long double} cacosl (complex long double @var{z})
425 @deftypefunx {complex _FloatN} cacosfN (complex _Float@var{N} @var{z})
426 @deftypefunx {complex _FloatNx} cacosfNx (complex _Float@var{N}x @var{z})
427 @standards{ISO, complex.h}
428 @standardsx{cacosfN, TS 18661-3:2015, complex.h}
429 @standardsx{cacosfNx, TS 18661-3:2015, complex.h}
430 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
431 These functions compute the complex arccosine of @var{z}---that is, the
432 value whose cosine is @var{z}. The value returned is in radians.
434 Unlike the real-valued functions, @code{cacos} is defined for all
439 @deftypefun {complex double} catan (complex double @var{z})
440 @deftypefunx {complex float} catanf (complex float @var{z})
441 @deftypefunx {complex long double} catanl (complex long double @var{z})
442 @deftypefunx {complex _FloatN} catanfN (complex _Float@var{N} @var{z})
443 @deftypefunx {complex _FloatNx} catanfNx (complex _Float@var{N}x @var{z})
444 @standards{ISO, complex.h}
445 @standardsx{catanfN, TS 18661-3:2015, complex.h}
446 @standardsx{catanfNx, TS 18661-3:2015, complex.h}
447 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
448 These functions compute the complex arctangent of @var{z}---that is,
449 the value whose tangent is @var{z}. The value is in units of radians.
453 @node Exponents and Logarithms
454 @section Exponentiation and Logarithms
455 @cindex exponentiation functions
456 @cindex power functions
457 @cindex logarithm functions
459 @deftypefun double exp (double @var{x})
460 @deftypefunx float expf (float @var{x})
461 @deftypefunx {long double} expl (long double @var{x})
462 @deftypefunx _FloatN expfN (_Float@var{N} @var{x})
463 @deftypefunx _FloatNx expfNx (_Float@var{N}x @var{x})
464 @standards{ISO, math.h}
465 @standardsx{expfN, TS 18661-3:2015, math.h}
466 @standardsx{expfNx, TS 18661-3:2015, math.h}
467 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
468 These functions compute @code{e} (the base of natural logarithms) raised
469 to the power @var{x}.
471 If the magnitude of the result is too large to be representable,
472 @code{exp} signals overflow.
475 @deftypefun double exp2 (double @var{x})
476 @deftypefunx float exp2f (float @var{x})
477 @deftypefunx {long double} exp2l (long double @var{x})
478 @deftypefunx _FloatN exp2fN (_Float@var{N} @var{x})
479 @deftypefunx _FloatNx exp2fNx (_Float@var{N}x @var{x})
480 @standards{ISO, math.h}
481 @standardsx{exp2fN, TS 18661-3:2015, math.h}
482 @standardsx{exp2fNx, TS 18661-3:2015, math.h}
483 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
484 These functions compute @code{2} raised to the power @var{x}.
485 Mathematically, @code{exp2 (x)} is the same as @code{exp (x * log (2))}.
488 @deftypefun double exp10 (double @var{x})
489 @deftypefunx float exp10f (float @var{x})
490 @deftypefunx {long double} exp10l (long double @var{x})
491 @deftypefunx _FloatN exp10fN (_Float@var{N} @var{x})
492 @deftypefunx _FloatNx exp10fNx (_Float@var{N}x @var{x})
493 @standards{ISO, math.h}
494 @standardsx{exp10fN, TS 18661-4:2015, math.h}
495 @standardsx{exp10fNx, TS 18661-4:2015, math.h}
496 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
497 These functions compute @code{10} raised to the power @var{x}.
498 Mathematically, @code{exp10 (x)} is the same as @code{exp (x * log (10))}.
500 The @code{exp10} functions are from TS 18661-4:2015.
504 @deftypefun double log (double @var{x})
505 @deftypefunx float logf (float @var{x})
506 @deftypefunx {long double} logl (long double @var{x})
507 @deftypefunx _FloatN logfN (_Float@var{N} @var{x})
508 @deftypefunx _FloatNx logfNx (_Float@var{N}x @var{x})
509 @standards{ISO, math.h}
510 @standardsx{logfN, TS 18661-3:2015, math.h}
511 @standardsx{logfNx, TS 18661-3:2015, math.h}
512 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
513 These functions compute the natural logarithm of @var{x}. @code{exp (log
514 (@var{x}))} equals @var{x}, exactly in mathematics and approximately in
517 If @var{x} is negative, @code{log} signals a domain error. If @var{x}
518 is zero, it returns negative infinity; if @var{x} is too close to zero,
519 it may signal overflow.
522 @deftypefun double log10 (double @var{x})
523 @deftypefunx float log10f (float @var{x})
524 @deftypefunx {long double} log10l (long double @var{x})
525 @deftypefunx _FloatN log10fN (_Float@var{N} @var{x})
526 @deftypefunx _FloatNx log10fNx (_Float@var{N}x @var{x})
527 @standards{ISO, math.h}
528 @standardsx{log10fN, TS 18661-3:2015, math.h}
529 @standardsx{log10fNx, TS 18661-3:2015, math.h}
530 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
531 These functions return the base-10 logarithm of @var{x}.
532 @code{log10 (@var{x})} equals @code{log (@var{x}) / log (10)}.
536 @deftypefun double log2 (double @var{x})
537 @deftypefunx float log2f (float @var{x})
538 @deftypefunx {long double} log2l (long double @var{x})
539 @deftypefunx _FloatN log2fN (_Float@var{N} @var{x})
540 @deftypefunx _FloatNx log2fNx (_Float@var{N}x @var{x})
541 @standards{ISO, math.h}
542 @standardsx{log2fN, TS 18661-3:2015, math.h}
543 @standardsx{log2fNx, TS 18661-3:2015, math.h}
544 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
545 These functions return the base-2 logarithm of @var{x}.
546 @code{log2 (@var{x})} equals @code{log (@var{x}) / log (2)}.
549 @deftypefun double logb (double @var{x})
550 @deftypefunx float logbf (float @var{x})
551 @deftypefunx {long double} logbl (long double @var{x})
552 @deftypefunx _FloatN logbfN (_Float@var{N} @var{x})
553 @deftypefunx _FloatNx logbfNx (_Float@var{N}x @var{x})
554 @standards{ISO, math.h}
555 @standardsx{logbfN, TS 18661-3:2015, math.h}
556 @standardsx{logbfNx, TS 18661-3:2015, math.h}
557 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
558 These functions extract the exponent of @var{x} and return it as a
559 floating-point value. If @code{FLT_RADIX} is two, @code{logb} is equal
560 to @code{floor (log2 (x))}, except it's probably faster.
562 If @var{x} is de-normalized, @code{logb} returns the exponent @var{x}
563 would have if it were normalized. If @var{x} is infinity (positive or
564 negative), @code{logb} returns @math{@infinity{}}. If @var{x} is zero,
565 @code{logb} returns @math{@infinity{}}. It does not signal.
568 @deftypefun int ilogb (double @var{x})
569 @deftypefunx int ilogbf (float @var{x})
570 @deftypefunx int ilogbl (long double @var{x})
571 @deftypefunx int ilogbfN (_Float@var{N} @var{x})
572 @deftypefunx int ilogbfNx (_Float@var{N}x @var{x})
573 @deftypefunx {long int} llogb (double @var{x})
574 @deftypefunx {long int} llogbf (float @var{x})
575 @deftypefunx {long int} llogbl (long double @var{x})
576 @deftypefunx {long int} llogbfN (_Float@var{N} @var{x})
577 @deftypefunx {long int} llogbfNx (_Float@var{N}x @var{x})
578 @standards{ISO, math.h}
579 @standardsx{ilogbfN, TS 18661-3:2015, math.h}
580 @standardsx{ilogbfNx, TS 18661-3:2015, math.h}
581 @standardsx{llogbfN, TS 18661-3:2015, math.h}
582 @standardsx{llogbfNx, TS 18661-3:2015, math.h}
583 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
584 These functions are equivalent to the corresponding @code{logb}
585 functions except that they return signed integer values. The
586 @code{ilogb}, @code{ilogbf}, and @code{ilogbl} functions are from ISO
587 C99; the @code{llogb}, @code{llogbf}, @code{llogbl} functions are from
588 TS 18661-1:2014; the @code{ilogbfN}, @code{ilogbfNx}, @code{llogbfN},
589 and @code{llogbfNx} functions are from TS 18661-3:2015.
593 Since integers cannot represent infinity and NaN, @code{ilogb} instead
594 returns an integer that can't be the exponent of a normal floating-point
595 number. @file{math.h} defines constants so you can check for this.
597 @deftypevr Macro int FP_ILOGB0
598 @standards{ISO, math.h}
599 @code{ilogb} returns this value if its argument is @code{0}. The
600 numeric value is either @code{INT_MIN} or @code{-INT_MAX}.
602 This macro is defined in @w{ISO C99}.
605 @deftypevr Macro {long int} FP_LLOGB0
606 @standards{ISO, math.h}
607 @code{llogb} returns this value if its argument is @code{0}. The
608 numeric value is either @code{LONG_MIN} or @code{-LONG_MAX}.
610 This macro is defined in TS 18661-1:2014.
613 @deftypevr Macro int FP_ILOGBNAN
614 @standards{ISO, math.h}
615 @code{ilogb} returns this value if its argument is @code{NaN}. The
616 numeric value is either @code{INT_MIN} or @code{INT_MAX}.
618 This macro is defined in @w{ISO C99}.
621 @deftypevr Macro {long int} FP_LLOGBNAN
622 @standards{ISO, math.h}
623 @code{llogb} returns this value if its argument is @code{NaN}. The
624 numeric value is either @code{LONG_MIN} or @code{LONG_MAX}.
626 This macro is defined in TS 18661-1:2014.
629 These values are system specific. They might even be the same. The
630 proper way to test the result of @code{ilogb} is as follows:
634 if (i == FP_ILOGB0 || i == FP_ILOGBNAN)
638 /* @r{Handle NaN.} */
642 /* @r{Handle 0.0.} */
646 /* @r{Some other value with large exponent,}
652 @deftypefun double pow (double @var{base}, double @var{power})
653 @deftypefunx float powf (float @var{base}, float @var{power})
654 @deftypefunx {long double} powl (long double @var{base}, long double @var{power})
655 @deftypefunx _FloatN powfN (_Float@var{N} @var{base}, _Float@var{N} @var{power})
656 @deftypefunx _FloatNx powfNx (_Float@var{N}x @var{base}, _Float@var{N}x @var{power})
657 @standards{ISO, math.h}
658 @standardsx{powfN, TS 18661-3:2015, math.h}
659 @standardsx{powfNx, TS 18661-3:2015, math.h}
660 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
661 These are general exponentiation functions, returning @var{base} raised
664 Mathematically, @code{pow} would return a complex number when @var{base}
665 is negative and @var{power} is not an integral value. @code{pow} can't
666 do that, so instead it signals a domain error. @code{pow} may also
667 underflow or overflow the destination type.
670 @cindex square root function
671 @deftypefun double sqrt (double @var{x})
672 @deftypefunx float sqrtf (float @var{x})
673 @deftypefunx {long double} sqrtl (long double @var{x})
674 @deftypefunx _FloatN sqrtfN (_Float@var{N} @var{x})
675 @deftypefunx _FloatNx sqrtfNx (_Float@var{N}x @var{x})
676 @standards{ISO, math.h}
677 @standardsx{sqrtfN, TS 18661-3:2015, math.h}
678 @standardsx{sqrtfNx, TS 18661-3:2015, math.h}
679 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
680 These functions return the nonnegative square root of @var{x}.
682 If @var{x} is negative, @code{sqrt} signals a domain error.
683 Mathematically, it should return a complex number.
686 @cindex cube root function
687 @deftypefun double cbrt (double @var{x})
688 @deftypefunx float cbrtf (float @var{x})
689 @deftypefunx {long double} cbrtl (long double @var{x})
690 @deftypefunx _FloatN cbrtfN (_Float@var{N} @var{x})
691 @deftypefunx _FloatNx cbrtfNx (_Float@var{N}x @var{x})
692 @standards{BSD, math.h}
693 @standardsx{cbrtfN, TS 18661-3:2015, math.h}
694 @standardsx{cbrtfNx, TS 18661-3:2015, math.h}
695 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
696 These functions return the cube root of @var{x}. They cannot
697 fail; every representable real value has a representable real cube root.
700 @deftypefun double hypot (double @var{x}, double @var{y})
701 @deftypefunx float hypotf (float @var{x}, float @var{y})
702 @deftypefunx {long double} hypotl (long double @var{x}, long double @var{y})
703 @deftypefunx _FloatN hypotfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
704 @deftypefunx _FloatNx hypotfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
705 @standards{ISO, math.h}
706 @standardsx{hypotfN, TS 18661-3:2015, math.h}
707 @standardsx{hypotfNx, TS 18661-3:2015, math.h}
708 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
709 These functions return @code{sqrt (@var{x}*@var{x} +
710 @var{y}*@var{y})}. This is the length of the hypotenuse of a right
711 triangle with sides of length @var{x} and @var{y}, or the distance
712 of the point (@var{x}, @var{y}) from the origin. Using this function
713 instead of the direct formula is wise, since the error is
714 much smaller. See also the function @code{cabs} in @ref{Absolute Value}.
717 @deftypefun double expm1 (double @var{x})
718 @deftypefunx float expm1f (float @var{x})
719 @deftypefunx {long double} expm1l (long double @var{x})
720 @deftypefunx _FloatN expm1fN (_Float@var{N} @var{x})
721 @deftypefunx _FloatNx expm1fNx (_Float@var{N}x @var{x})
722 @standards{ISO, math.h}
723 @standardsx{expm1fN, TS 18661-3:2015, math.h}
724 @standardsx{expm1fNx, TS 18661-3:2015, math.h}
725 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
726 These functions return a value equivalent to @code{exp (@var{x}) - 1}.
727 They are computed in a way that is accurate even if @var{x} is
728 near zero---a case where @code{exp (@var{x}) - 1} would be inaccurate owing
729 to subtraction of two numbers that are nearly equal.
732 @deftypefun double log1p (double @var{x})
733 @deftypefunx float log1pf (float @var{x})
734 @deftypefunx {long double} log1pl (long double @var{x})
735 @deftypefunx _FloatN log1pfN (_Float@var{N} @var{x})
736 @deftypefunx _FloatNx log1pfNx (_Float@var{N}x @var{x})
737 @standards{ISO, math.h}
738 @standardsx{log1pfN, TS 18661-3:2015, math.h}
739 @standardsx{log1pfNx, TS 18661-3:2015, math.h}
740 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
741 These functions return a value equivalent to @w{@code{log (1 + @var{x})}}.
742 They are computed in a way that is accurate even if @var{x} is
746 @cindex complex exponentiation functions
747 @cindex complex logarithm functions
749 @w{ISO C99} defines complex variants of some of the exponentiation and
752 @deftypefun {complex double} cexp (complex double @var{z})
753 @deftypefunx {complex float} cexpf (complex float @var{z})
754 @deftypefunx {complex long double} cexpl (complex long double @var{z})
755 @deftypefunx {complex _FloatN} cexpfN (complex _Float@var{N} @var{z})
756 @deftypefunx {complex _FloatNx} cexpfNx (complex _Float@var{N}x @var{z})
757 @standards{ISO, complex.h}
758 @standardsx{cexpfN, TS 18661-3:2015, complex.h}
759 @standardsx{cexpfNx, TS 18661-3:2015, complex.h}
760 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
761 These functions return @code{e} (the base of natural
762 logarithms) raised to the power of @var{z}.
763 Mathematically, this corresponds to the value
766 @math{exp (z) = exp (creal (z)) * (cos (cimag (z)) + I * sin (cimag (z)))}
769 $$\exp(z) = e^z = e^{{\rm Re}\,z} (\cos ({\rm Im}\,z) + i \sin ({\rm Im}\,z))$$
773 @deftypefun {complex double} clog (complex double @var{z})
774 @deftypefunx {complex float} clogf (complex float @var{z})
775 @deftypefunx {complex long double} clogl (complex long double @var{z})
776 @deftypefunx {complex _FloatN} clogfN (complex _Float@var{N} @var{z})
777 @deftypefunx {complex _FloatNx} clogfNx (complex _Float@var{N}x @var{z})
778 @standards{ISO, complex.h}
779 @standardsx{clogfN, TS 18661-3:2015, complex.h}
780 @standardsx{clogfNx, TS 18661-3:2015, complex.h}
781 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
782 These functions return the natural logarithm of @var{z}.
783 Mathematically, this corresponds to the value
786 @math{log (z) = log (cabs (z)) + I * carg (z)}
789 $$\log(z) = \log |z| + i \arg z$$
793 @code{clog} has a pole at 0, and will signal overflow if @var{z} equals
794 or is very close to 0. It is well-defined for all other values of
799 @deftypefun {complex double} clog10 (complex double @var{z})
800 @deftypefunx {complex float} clog10f (complex float @var{z})
801 @deftypefunx {complex long double} clog10l (complex long double @var{z})
802 @deftypefunx {complex _FloatN} clog10fN (complex _Float@var{N} @var{z})
803 @deftypefunx {complex _FloatNx} clog10fNx (complex _Float@var{N}x @var{z})
804 @standards{GNU, complex.h}
805 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
806 These functions return the base 10 logarithm of the complex value
807 @var{z}. Mathematically, this corresponds to the value
810 @math{log10 (z) = log10 (cabs (z)) + I * carg (z) / log (10)}
813 $$\log_{10}(z) = \log_{10}|z| + i \arg z / \log (10)$$
816 All these functions, including the @code{_Float@var{N}} and
817 @code{_Float@var{N}x} variants, are GNU extensions.
820 @deftypefun {complex double} csqrt (complex double @var{z})
821 @deftypefunx {complex float} csqrtf (complex float @var{z})
822 @deftypefunx {complex long double} csqrtl (complex long double @var{z})
823 @deftypefunx {complex _FloatN} csqrtfN (_Float@var{N} @var{z})
824 @deftypefunx {complex _FloatNx} csqrtfNx (complex _Float@var{N}x @var{z})
825 @standards{ISO, complex.h}
826 @standardsx{csqrtfN, TS 18661-3:2015, complex.h}
827 @standardsx{csqrtfNx, TS 18661-3:2015, complex.h}
828 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
829 These functions return the complex square root of the argument @var{z}. Unlike
830 the real-valued functions, they are defined for all values of @var{z}.
833 @deftypefun {complex double} cpow (complex double @var{base}, complex double @var{power})
834 @deftypefunx {complex float} cpowf (complex float @var{base}, complex float @var{power})
835 @deftypefunx {complex long double} cpowl (complex long double @var{base}, complex long double @var{power})
836 @deftypefunx {complex _FloatN} cpowfN (complex _Float@var{N} @var{base}, complex _Float@var{N} @var{power})
837 @deftypefunx {complex _FloatNx} cpowfNx (complex _Float@var{N}x @var{base}, complex _Float@var{N}x @var{power})
838 @standards{ISO, complex.h}
839 @standardsx{cpowfN, TS 18661-3:2015, complex.h}
840 @standardsx{cpowfNx, TS 18661-3:2015, complex.h}
841 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
842 These functions return @var{base} raised to the power of
843 @var{power}. This is equivalent to @w{@code{cexp (y * clog (x))}}
846 @node Hyperbolic Functions
847 @section Hyperbolic Functions
848 @cindex hyperbolic functions
850 The functions in this section are related to the exponential functions;
851 see @ref{Exponents and Logarithms}.
853 @deftypefun double sinh (double @var{x})
854 @deftypefunx float sinhf (float @var{x})
855 @deftypefunx {long double} sinhl (long double @var{x})
856 @deftypefunx _FloatN sinhfN (_Float@var{N} @var{x})
857 @deftypefunx _FloatNx sinhfNx (_Float@var{N}x @var{x})
858 @standards{ISO, math.h}
859 @standardsx{sinhfN, TS 18661-3:2015, math.h}
860 @standardsx{sinhfNx, TS 18661-3:2015, math.h}
861 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
862 These functions return the hyperbolic sine of @var{x}, defined
863 mathematically as @w{@code{(exp (@var{x}) - exp (-@var{x})) / 2}}. They
864 may signal overflow if @var{x} is too large.
867 @deftypefun double cosh (double @var{x})
868 @deftypefunx float coshf (float @var{x})
869 @deftypefunx {long double} coshl (long double @var{x})
870 @deftypefunx _FloatN coshfN (_Float@var{N} @var{x})
871 @deftypefunx _FloatNx coshfNx (_Float@var{N}x @var{x})
872 @standards{ISO, math.h}
873 @standardsx{coshfN, TS 18661-3:2015, math.h}
874 @standardsx{coshfNx, TS 18661-3:2015, math.h}
875 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
876 These functions return the hyperbolic cosine of @var{x},
877 defined mathematically as @w{@code{(exp (@var{x}) + exp (-@var{x})) / 2}}.
878 They may signal overflow if @var{x} is too large.
881 @deftypefun double tanh (double @var{x})
882 @deftypefunx float tanhf (float @var{x})
883 @deftypefunx {long double} tanhl (long double @var{x})
884 @deftypefunx _FloatN tanhfN (_Float@var{N} @var{x})
885 @deftypefunx _FloatNx tanhfNx (_Float@var{N}x @var{x})
886 @standards{ISO, math.h}
887 @standardsx{tanhfN, TS 18661-3:2015, math.h}
888 @standardsx{tanhfNx, TS 18661-3:2015, math.h}
889 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
890 These functions return the hyperbolic tangent of @var{x},
891 defined mathematically as @w{@code{sinh (@var{x}) / cosh (@var{x})}}.
892 They may signal overflow if @var{x} is too large.
895 @cindex hyperbolic functions
897 There are counterparts for the hyperbolic functions which take
900 @deftypefun {complex double} csinh (complex double @var{z})
901 @deftypefunx {complex float} csinhf (complex float @var{z})
902 @deftypefunx {complex long double} csinhl (complex long double @var{z})
903 @deftypefunx {complex _FloatN} csinhfN (complex _Float@var{N} @var{z})
904 @deftypefunx {complex _FloatNx} csinhfNx (complex _Float@var{N}x @var{z})
905 @standards{ISO, complex.h}
906 @standardsx{csinhfN, TS 18661-3:2015, complex.h}
907 @standardsx{csinhfNx, TS 18661-3:2015, complex.h}
908 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
909 These functions return the complex hyperbolic sine of @var{z}, defined
910 mathematically as @w{@code{(exp (@var{z}) - exp (-@var{z})) / 2}}.
913 @deftypefun {complex double} ccosh (complex double @var{z})
914 @deftypefunx {complex float} ccoshf (complex float @var{z})
915 @deftypefunx {complex long double} ccoshl (complex long double @var{z})
916 @deftypefunx {complex _FloatN} ccoshfN (complex _Float@var{N} @var{z})
917 @deftypefunx {complex _FloatNx} ccoshfNx (complex _Float@var{N}x @var{z})
918 @standards{ISO, complex.h}
919 @standardsx{ccoshfN, TS 18661-3:2015, complex.h}
920 @standardsx{ccoshfNx, TS 18661-3:2015, complex.h}
921 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
922 These functions return the complex hyperbolic cosine of @var{z}, defined
923 mathematically as @w{@code{(exp (@var{z}) + exp (-@var{z})) / 2}}.
926 @deftypefun {complex double} ctanh (complex double @var{z})
927 @deftypefunx {complex float} ctanhf (complex float @var{z})
928 @deftypefunx {complex long double} ctanhl (complex long double @var{z})
929 @deftypefunx {complex _FloatN} ctanhfN (complex _Float@var{N} @var{z})
930 @deftypefunx {complex _FloatNx} ctanhfNx (complex _Float@var{N}x @var{z})
931 @standards{ISO, complex.h}
932 @standardsx{ctanhfN, TS 18661-3:2015, complex.h}
933 @standardsx{ctanhfNx, TS 18661-3:2015, complex.h}
934 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
935 These functions return the complex hyperbolic tangent of @var{z},
936 defined mathematically as @w{@code{csinh (@var{z}) / ccosh (@var{z})}}.
940 @cindex inverse hyperbolic functions
942 @deftypefun double asinh (double @var{x})
943 @deftypefunx float asinhf (float @var{x})
944 @deftypefunx {long double} asinhl (long double @var{x})
945 @deftypefunx _FloatN asinhfN (_Float@var{N} @var{x})
946 @deftypefunx _FloatNx asinhfNx (_Float@var{N}x @var{x})
947 @standards{ISO, math.h}
948 @standardsx{asinhfN, TS 18661-3:2015, math.h}
949 @standardsx{asinhfNx, TS 18661-3:2015, math.h}
950 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
951 These functions return the inverse hyperbolic sine of @var{x}---the
952 value whose hyperbolic sine is @var{x}.
955 @deftypefun double acosh (double @var{x})
956 @deftypefunx float acoshf (float @var{x})
957 @deftypefunx {long double} acoshl (long double @var{x})
958 @deftypefunx _FloatN acoshfN (_Float@var{N} @var{x})
959 @deftypefunx _FloatNx acoshfNx (_Float@var{N}x @var{x})
960 @standards{ISO, math.h}
961 @standardsx{acoshfN, TS 18661-3:2015, math.h}
962 @standardsx{acoshfNx, TS 18661-3:2015, math.h}
963 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
964 These functions return the inverse hyperbolic cosine of @var{x}---the
965 value whose hyperbolic cosine is @var{x}. If @var{x} is less than
966 @code{1}, @code{acosh} signals a domain error.
969 @deftypefun double atanh (double @var{x})
970 @deftypefunx float atanhf (float @var{x})
971 @deftypefunx {long double} atanhl (long double @var{x})
972 @deftypefunx _FloatN atanhfN (_Float@var{N} @var{x})
973 @deftypefunx _FloatNx atanhfNx (_Float@var{N}x @var{x})
974 @standards{ISO, math.h}
975 @standardsx{atanhfN, TS 18661-3:2015, math.h}
976 @standardsx{atanhfNx, TS 18661-3:2015, math.h}
977 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
978 These functions return the inverse hyperbolic tangent of @var{x}---the
979 value whose hyperbolic tangent is @var{x}. If the absolute value of
980 @var{x} is greater than @code{1}, @code{atanh} signals a domain error;
981 if it is equal to 1, @code{atanh} returns infinity.
984 @cindex inverse complex hyperbolic functions
986 @deftypefun {complex double} casinh (complex double @var{z})
987 @deftypefunx {complex float} casinhf (complex float @var{z})
988 @deftypefunx {complex long double} casinhl (complex long double @var{z})
989 @deftypefunx {complex _FloatN} casinhfN (complex _Float@var{N} @var{z})
990 @deftypefunx {complex _FloatNx} casinhfNx (complex _Float@var{N}x @var{z})
991 @standards{ISO, complex.h}
992 @standardsx{casinhfN, TS 18661-3:2015, complex.h}
993 @standardsx{casinhfNx, TS 18661-3:2015, complex.h}
994 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
995 These functions return the inverse complex hyperbolic sine of
996 @var{z}---the value whose complex hyperbolic sine is @var{z}.
999 @deftypefun {complex double} cacosh (complex double @var{z})
1000 @deftypefunx {complex float} cacoshf (complex float @var{z})
1001 @deftypefunx {complex long double} cacoshl (complex long double @var{z})
1002 @deftypefunx {complex _FloatN} cacoshfN (complex _Float@var{N} @var{z})
1003 @deftypefunx {complex _FloatNx} cacoshfNx (complex _Float@var{N}x @var{z})
1004 @standards{ISO, complex.h}
1005 @standardsx{cacoshfN, TS 18661-3:2015, complex.h}
1006 @standardsx{cacoshfNx, TS 18661-3:2015, complex.h}
1007 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1008 These functions return the inverse complex hyperbolic cosine of
1009 @var{z}---the value whose complex hyperbolic cosine is @var{z}. Unlike
1010 the real-valued functions, there are no restrictions on the value of @var{z}.
1013 @deftypefun {complex double} catanh (complex double @var{z})
1014 @deftypefunx {complex float} catanhf (complex float @var{z})
1015 @deftypefunx {complex long double} catanhl (complex long double @var{z})
1016 @deftypefunx {complex _FloatN} catanhfN (complex _Float@var{N} @var{z})
1017 @deftypefunx {complex _FloatNx} catanhfNx (complex _Float@var{N}x @var{z})
1018 @standards{ISO, complex.h}
1019 @standardsx{catanhfN, TS 18661-3:2015, complex.h}
1020 @standardsx{catanhfNx, TS 18661-3:2015, complex.h}
1021 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1022 These functions return the inverse complex hyperbolic tangent of
1023 @var{z}---the value whose complex hyperbolic tangent is @var{z}. Unlike
1024 the real-valued functions, there are no restrictions on the value of
1028 @node Special Functions
1029 @section Special Functions
1030 @cindex special functions
1031 @cindex Bessel functions
1032 @cindex gamma function
1034 These are some more exotic mathematical functions which are sometimes
1035 useful. Currently they only have real-valued versions.
1037 @deftypefun double erf (double @var{x})
1038 @deftypefunx float erff (float @var{x})
1039 @deftypefunx {long double} erfl (long double @var{x})
1040 @deftypefunx _FloatN erffN (_Float@var{N} @var{x})
1041 @deftypefunx _FloatNx erffNx (_Float@var{N}x @var{x})
1042 @standards{SVID, math.h}
1043 @standardsx{erffN, TS 18661-3:2015, math.h}
1044 @standardsx{erffNx, TS 18661-3:2015, math.h}
1045 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1046 @code{erf} returns the error function of @var{x}. The error
1047 function is defined as
1049 $$\hbox{erf}(x) = {2\over\sqrt{\pi}}\cdot\int_0^x e^{-t^2} \hbox{d}t$$
1053 erf (x) = 2/sqrt(pi) * integral from 0 to x of exp(-t^2) dt
1058 @deftypefun double erfc (double @var{x})
1059 @deftypefunx float erfcf (float @var{x})
1060 @deftypefunx {long double} erfcl (long double @var{x})
1061 @deftypefunx _FloatN erfcfN (_Float@var{N} @var{x})
1062 @deftypefunx _FloatNx erfcfNx (_Float@var{N}x @var{x})
1063 @standards{SVID, math.h}
1064 @standardsx{erfcfN, TS 18661-3:2015, math.h}
1065 @standardsx{erfcfNx, TS 18661-3:2015, math.h}
1066 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1067 @code{erfc} returns @code{1.0 - erf(@var{x})}, but computed in a
1068 fashion that avoids round-off error when @var{x} is large.
1071 @deftypefun double lgamma (double @var{x})
1072 @deftypefunx float lgammaf (float @var{x})
1073 @deftypefunx {long double} lgammal (long double @var{x})
1074 @deftypefunx _FloatN lgammafN (_Float@var{N} @var{x})
1075 @deftypefunx _FloatNx lgammafNx (_Float@var{N}x @var{x})
1076 @standards{SVID, math.h}
1077 @standardsx{lgammafN, TS 18661-3:2015, math.h}
1078 @standardsx{lgammafNx, TS 18661-3:2015, math.h}
1079 @safety{@prelim{}@mtunsafe{@mtasurace{:signgam}}@asunsafe{}@acsafe{}}
1080 @code{lgamma} returns the natural logarithm of the absolute value of
1081 the gamma function of @var{x}. The gamma function is defined as
1083 $$\Gamma(x) = \int_0^\infty t^{x-1} e^{-t} \hbox{d}t$$
1087 gamma (x) = integral from 0 to @infinity{} of t^(x-1) e^-t dt
1092 The sign of the gamma function is stored in the global variable
1093 @var{signgam}, which is declared in @file{math.h}. It is @code{1} if
1094 the intermediate result was positive or zero, or @code{-1} if it was
1097 To compute the real gamma function you can use the @code{tgamma}
1098 function or you can compute the values as follows:
1101 gam = signgam*exp(lgam);
1104 The gamma function has singularities at the non-positive integers.
1105 @code{lgamma} will raise the zero divide exception if evaluated at a
1109 @deftypefun double lgamma_r (double @var{x}, int *@var{signp})
1110 @deftypefunx float lgammaf_r (float @var{x}, int *@var{signp})
1111 @deftypefunx {long double} lgammal_r (long double @var{x}, int *@var{signp})
1112 @deftypefunx _FloatN lgammafN_r (_Float@var{N} @var{x}, int *@var{signp})
1113 @deftypefunx _FloatNx lgammafNx_r (_Float@var{N}x @var{x}, int *@var{signp})
1114 @standards{XPG, math.h}
1115 @standardsx{lgammafN_r, GNU, math.h}
1116 @standardsx{lgammafNx_r, GNU, math.h}
1117 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1118 @code{lgamma_r} is just like @code{lgamma}, but it stores the sign of
1119 the intermediate result in the variable pointed to by @var{signp}
1120 instead of in the @var{signgam} global. This means it is reentrant.
1122 The @code{lgammaf@var{N}_r} and @code{lgammaf@var{N}x_r} functions are
1126 @deftypefun double gamma (double @var{x})
1127 @deftypefunx float gammaf (float @var{x})
1128 @deftypefunx {long double} gammal (long double @var{x})
1129 @standards{SVID, math.h}
1130 @safety{@prelim{}@mtunsafe{@mtasurace{:signgam}}@asunsafe{}@acsafe{}}
1131 These functions exist for compatibility reasons. They are equivalent to
1132 @code{lgamma} etc. It is better to use @code{lgamma} since for one the
1133 name reflects better the actual computation, and moreover @code{lgamma} is
1134 standardized in @w{ISO C99} while @code{gamma} is not.
1137 @deftypefun double tgamma (double @var{x})
1138 @deftypefunx float tgammaf (float @var{x})
1139 @deftypefunx {long double} tgammal (long double @var{x})
1140 @deftypefunx _FloatN tgammafN (_Float@var{N} @var{x})
1141 @deftypefunx _FloatNx tgammafNx (_Float@var{N}x @var{x})
1142 @standardsx{tgamma, XPG, math.h}
1143 @standardsx{tgamma, ISO, math.h}
1144 @standardsx{tgammaf, XPG, math.h}
1145 @standardsx{tgammaf, ISO, math.h}
1146 @standardsx{tgammal, XPG, math.h}
1147 @standardsx{tgammal, ISO, math.h}
1148 @standardsx{tgammafN, TS 18661-3:2015, math.h}
1149 @standardsx{tgammafNx, TS 18661-3:2015, math.h}
1150 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1151 @code{tgamma} applies the gamma function to @var{x}. The gamma
1152 function is defined as
1154 $$\Gamma(x) = \int_0^\infty t^{x-1} e^{-t} \hbox{d}t$$
1158 gamma (x) = integral from 0 to @infinity{} of t^(x-1) e^-t dt
1162 This function was introduced in @w{ISO C99}. The @code{_Float@var{N}}
1163 and @code{_Float@var{N}x} variants were introduced in @w{ISO/IEC TS
1167 @deftypefun double j0 (double @var{x})
1168 @deftypefunx float j0f (float @var{x})
1169 @deftypefunx {long double} j0l (long double @var{x})
1170 @deftypefunx _FloatN j0fN (_Float@var{N} @var{x})
1171 @deftypefunx _FloatNx j0fNx (_Float@var{N}x @var{x})
1172 @standards{SVID, math.h}
1173 @standardsx{j0fN, GNU, math.h}
1174 @standardsx{j0fNx, GNU, math.h}
1175 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1176 @code{j0} returns the Bessel function of the first kind of order 0 of
1177 @var{x}. It may signal underflow if @var{x} is too large.
1179 The @code{_Float@var{N}} and @code{_Float@var{N}x} variants are GNU
1183 @deftypefun double j1 (double @var{x})
1184 @deftypefunx float j1f (float @var{x})
1185 @deftypefunx {long double} j1l (long double @var{x})
1186 @deftypefunx _FloatN j1fN (_Float@var{N} @var{x})
1187 @deftypefunx _FloatNx j1fNx (_Float@var{N}x @var{x})
1188 @standards{SVID, math.h}
1189 @standardsx{j1fN, GNU, math.h}
1190 @standardsx{j1fNx, GNU, math.h}
1191 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1192 @code{j1} returns the Bessel function of the first kind of order 1 of
1193 @var{x}. It may signal underflow if @var{x} is too large.
1195 The @code{_Float@var{N}} and @code{_Float@var{N}x} variants are GNU
1199 @deftypefun double jn (int @var{n}, double @var{x})
1200 @deftypefunx float jnf (int @var{n}, float @var{x})
1201 @deftypefunx {long double} jnl (int @var{n}, long double @var{x})
1202 @deftypefunx _FloatN jnfN (int @var{n}, _Float@var{N} @var{x})
1203 @deftypefunx _FloatNx jnfNx (int @var{n}, _Float@var{N}x @var{x})
1204 @standards{SVID, math.h}
1205 @standardsx{jnfN, GNU, math.h}
1206 @standardsx{jnfNx, GNU, math.h}
1207 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1208 @code{jn} returns the Bessel function of the first kind of order
1209 @var{n} of @var{x}. It may signal underflow if @var{x} is too large.
1211 The @code{_Float@var{N}} and @code{_Float@var{N}x} variants are GNU
1215 @deftypefun double y0 (double @var{x})
1216 @deftypefunx float y0f (float @var{x})
1217 @deftypefunx {long double} y0l (long double @var{x})
1218 @deftypefunx _FloatN y0fN (_Float@var{N} @var{x})
1219 @deftypefunx _FloatNx y0fNx (_Float@var{N}x @var{x})
1220 @standards{SVID, math.h}
1221 @standardsx{y0fN, GNU, math.h}
1222 @standardsx{y0fNx, GNU, math.h}
1223 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1224 @code{y0} returns the Bessel function of the second kind of order 0 of
1225 @var{x}. It may signal underflow if @var{x} is too large. If @var{x}
1226 is negative, @code{y0} signals a domain error; if it is zero,
1227 @code{y0} signals overflow and returns @math{-@infinity}.
1229 The @code{_Float@var{N}} and @code{_Float@var{N}x} variants are GNU
1233 @deftypefun double y1 (double @var{x})
1234 @deftypefunx float y1f (float @var{x})
1235 @deftypefunx {long double} y1l (long double @var{x})
1236 @deftypefunx _FloatN y1fN (_Float@var{N} @var{x})
1237 @deftypefunx _FloatNx y1fNx (_Float@var{N}x @var{x})
1238 @standards{SVID, math.h}
1239 @standardsx{y1fN, GNU, math.h}
1240 @standardsx{y1fNx, GNU, math.h}
1241 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1242 @code{y1} returns the Bessel function of the second kind of order 1 of
1243 @var{x}. It may signal underflow if @var{x} is too large. If @var{x}
1244 is negative, @code{y1} signals a domain error; if it is zero,
1245 @code{y1} signals overflow and returns @math{-@infinity}.
1247 The @code{_Float@var{N}} and @code{_Float@var{N}x} variants are GNU
1251 @deftypefun double yn (int @var{n}, double @var{x})
1252 @deftypefunx float ynf (int @var{n}, float @var{x})
1253 @deftypefunx {long double} ynl (int @var{n}, long double @var{x})
1254 @deftypefunx _FloatN ynfN (int @var{n}, _Float@var{N} @var{x})
1255 @deftypefunx _FloatNx ynfNx (int @var{n}, _Float@var{N}x @var{x})
1256 @standards{SVID, math.h}
1257 @standardsx{ynfN, GNU, math.h}
1258 @standardsx{ynfNx, GNU, math.h}
1259 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1260 @code{yn} returns the Bessel function of the second kind of order @var{n} of
1261 @var{x}. It may signal underflow if @var{x} is too large. If @var{x}
1262 is negative, @code{yn} signals a domain error; if it is zero,
1263 @code{yn} signals overflow and returns @math{-@infinity}.
1265 The @code{_Float@var{N}} and @code{_Float@var{N}x} variants are GNU
1269 @node Errors in Math Functions
1270 @section Known Maximum Errors in Math Functions
1274 This section lists the known errors of the functions in the math
1275 library. Errors are measured in ``units of the last place''. This is a
1276 measure for the relative error. For a number @math{z} with the
1277 representation @math{d.d@dots{}d@mul{}2^e} (we assume IEEE
1278 floating-point numbers with base 2) the ULP is represented by
1281 $${|d.d\dots d - (z/2^e)|}\over {2^{p-1}}$$
1285 |d.d...d - (z / 2^e)| / 2^(p - 1)
1290 where @math{p} is the number of bits in the mantissa of the
1291 floating-point number representation. Ideally the error for all
1292 functions is always less than 0.5ulps in round-to-nearest mode. Using
1293 rounding bits this is also
1294 possible and normally implemented for the basic operations. Except
1295 for certain functions such as @code{sqrt}, @code{fma} and @code{rint}
1296 whose results are fully specified by reference to corresponding IEEE
1297 754 floating-point operations, and conversions between strings and
1298 floating point, @theglibc{} does not aim for correctly rounded results
1299 for functions in the math library, and does not aim for correctness in
1300 whether ``inexact'' exceptions are raised. Instead, the goals for
1301 accuracy of functions without fully specified results are as follows;
1302 some functions have bugs meaning they do not meet these goals in all
1303 cases. In the future, @theglibc{} may provide some other correctly
1304 rounding functions under the names such as @code{crsin} proposed for
1305 an extension to ISO C.
1310 Each function with a floating-point result behaves as if it computes
1311 an infinite-precision result that is within a few ulp (in both real
1312 and complex parts, for functions with complex results) of the
1313 mathematically correct value of the function (interpreted together
1314 with ISO C or POSIX semantics for the function in question) at the
1315 exact value passed as the input. Exceptions are raised appropriately
1316 for this value and in accordance with IEEE 754 / ISO C / POSIX
1317 semantics, and it is then rounded according to the current rounding
1318 direction to the result that is returned to the user. @code{errno}
1319 may also be set (@pxref{Math Error Reporting}). (The ``inexact''
1320 exception may be raised, or not raised, even if this is inconsistent
1321 with the infinite-precision value.)
1324 For the IBM @code{long double} format, as used on PowerPC GNU/Linux,
1325 the accuracy goal is weaker for input values not exactly representable
1326 in 106 bits of precision; it is as if the input value is some value
1327 within 0.5ulp of the value actually passed, where ``ulp'' is
1328 interpreted in terms of a fixed-precision 106-bit mantissa, but not
1329 necessarily the exact value actually passed with discontiguous
1333 For the IBM @code{long double} format, functions whose results are
1334 fully specified by reference to corresponding IEEE 754 floating-point
1335 operations have the same accuracy goals as other functions, but with
1336 the error bound being the same as that for division (3ulp).
1337 Furthermore, ``inexact'' and ``underflow'' exceptions may be raised
1338 for all functions for any inputs, even where such exceptions are
1339 inconsistent with the returned value, since the underlying
1340 floating-point arithmetic has that property.
1343 Functions behave as if the infinite-precision result computed is zero,
1344 infinity or NaN if and only if that is the mathematically correct
1345 infinite-precision result. They behave as if the infinite-precision
1346 result computed always has the same sign as the mathematically correct
1350 If the mathematical result is more than a few ulp above the overflow
1351 threshold for the current rounding direction, the value returned is
1352 the appropriate overflow value for the current rounding direction,
1353 with the overflow exception raised.
1356 If the mathematical result has magnitude well below half the least
1357 subnormal magnitude, the returned value is either zero or the least
1358 subnormal (in each case, with the correct sign), according to the
1359 current rounding direction and with the underflow exception raised.
1362 Where the mathematical result underflows (before rounding) and is not
1363 exactly representable as a floating-point value, the function does not
1364 behave as if the computed infinite-precision result is an exact value
1365 in the subnormal range. This means that the underflow exception is
1366 raised other than possibly for cases where the mathematical result is
1367 very close to the underflow threshold and the function behaves as if
1368 it computes an infinite-precision result that does not underflow. (So
1369 there may be spurious underflow exceptions in cases where the
1370 underflowing result is exact, but not missing underflow exceptions in
1371 cases where it is inexact.)
1374 @Theglibc{} does not aim for functions to satisfy other properties of
1375 the underlying mathematical function, such as monotonicity, where not
1376 implied by the above goals.
1379 All the above applies to both real and complex parts, for complex
1384 Therefore many of the functions in the math library have errors. The
1385 table lists the maximum error for each function which is exposed by one
1386 of the existing tests in the test suite. The table tries to cover as much
1387 as possible and list the actual maximum error (or at least a ballpark
1388 figure) but this is often not achieved due to the large search space.
1390 The table lists the ULP values for different architectures. Different
1391 architectures have different results since their hardware support for
1392 floating-point operations varies and also the existing hardware support
1393 is different. Only the round-to-nearest rounding mode is covered by
1394 this table, and vector versions of functions are not covered.
1395 Functions not listed do not have known errors.
1398 @c This multitable does not fit on a single page
1399 @include libm-err.texi
1401 @node Pseudo-Random Numbers
1402 @section Pseudo-Random Numbers
1403 @cindex random numbers
1404 @cindex pseudo-random numbers
1405 @cindex seed (for random numbers)
1407 This section describes the GNU facilities for generating a series of
1408 pseudo-random numbers. The numbers generated are not truly random;
1409 typically, they form a sequence that repeats periodically, with a period
1410 so large that you can ignore it for ordinary purposes. The random
1411 number generator works by remembering a @dfn{seed} value which it uses
1412 to compute the next random number and also to compute a new seed.
1414 Although the generated numbers look unpredictable within one run of a
1415 program, the sequence of numbers is @emph{exactly the same} from one run
1416 to the next. This is because the initial seed is always the same. This
1417 is convenient when you are debugging a program, but it is unhelpful if
1418 you want the program to behave unpredictably. If you want a different
1419 pseudo-random series each time your program runs, you must specify a
1420 different seed each time. For ordinary purposes, basing the seed on the
1421 current time works well. For random numbers in cryptography,
1422 @pxref{Unpredictable Bytes}.
1424 You can obtain repeatable sequences of numbers on a particular machine type
1425 by specifying the same initial seed value for the random number
1426 generator. There is no standard meaning for a particular seed value;
1427 the same seed, used in different C libraries or on different CPU types,
1428 will give you different random numbers.
1430 @Theglibc{} supports the standard @w{ISO C} random number functions
1431 plus two other sets derived from BSD and SVID. The BSD and @w{ISO C}
1432 functions provide identical, somewhat limited functionality. If only a
1433 small number of random bits are required, we recommend you use the
1434 @w{ISO C} interface, @code{rand} and @code{srand}. The SVID functions
1435 provide a more flexible interface, which allows better random number
1436 generator algorithms, provides more random bits (up to 48) per call, and
1437 can provide random floating-point numbers. These functions are required
1438 by the XPG standard and therefore will be present in all modern Unix
1442 * ISO Random:: @code{rand} and friends.
1443 * BSD Random:: @code{random} and friends.
1444 * SVID Random:: @code{drand48} and friends.
1448 @subsection ISO C Random Number Functions
1450 This section describes the random number functions that are part of
1451 the @w{ISO C} standard.
1453 To use these facilities, you should include the header file
1454 @file{stdlib.h} in your program.
1457 @deftypevr Macro int RAND_MAX
1458 @standards{ISO, stdlib.h}
1459 The value of this macro is an integer constant representing the largest
1460 value the @code{rand} function can return. In @theglibc{}, it is
1461 @code{2147483647}, which is the largest signed integer representable in
1462 32 bits. In other libraries, it may be as low as @code{32767}.
1465 @deftypefun int rand (void)
1466 @standards{ISO, stdlib.h}
1467 @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
1468 @c Just calls random.
1469 The @code{rand} function returns the next pseudo-random number in the
1470 series. The value ranges from @code{0} to @code{RAND_MAX}.
1473 @deftypefun void srand (unsigned int @var{seed})
1474 @standards{ISO, stdlib.h}
1475 @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
1476 @c Alias to srandom.
1477 This function establishes @var{seed} as the seed for a new series of
1478 pseudo-random numbers. If you call @code{rand} before a seed has been
1479 established with @code{srand}, it uses the value @code{1} as a default
1482 To produce a different pseudo-random series each time your program is
1483 run, do @code{srand (time (0))}.
1486 POSIX.1 extended the C standard functions to support reproducible random
1487 numbers in multi-threaded programs. However, the extension is badly
1488 designed and unsuitable for serious work.
1490 @deftypefun int rand_r (unsigned int *@var{seed})
1491 @standards{POSIX.1, stdlib.h}
1492 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1493 This function returns a random number in the range 0 to @code{RAND_MAX}
1494 just as @code{rand} does. However, all its state is stored in the
1495 @var{seed} argument. This means the RNG's state can only have as many
1496 bits as the type @code{unsigned int} has. This is far too few to
1499 If your program requires a reentrant RNG, we recommend you use the
1500 reentrant GNU extensions to the SVID random number generator. The
1501 POSIX.1 interface should only be used when the GNU extensions are not
1507 @subsection BSD Random Number Functions
1509 This section describes a set of random number generation functions that
1510 are derived from BSD. There is no advantage to using these functions
1511 with @theglibc{}; we support them for BSD compatibility only.
1513 The prototypes for these functions are in @file{stdlib.h}.
1516 @deftypefun {long int} random (void)
1517 @standards{BSD, stdlib.h}
1518 @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
1519 @c Takes a lock and calls random_r with an automatic variable and the
1520 @c global state, while holding a lock.
1521 This function returns the next pseudo-random number in the sequence.
1522 The value returned ranges from @code{0} to @code{2147483647}.
1524 @strong{NB:} Temporarily this function was defined to return a
1525 @code{int32_t} value to indicate that the return value always contains
1526 32 bits even if @code{long int} is wider. The standard demands it
1527 differently. Users must always be aware of the 32-bit limitation,
1531 @deftypefun void srandom (unsigned int @var{seed})
1532 @standards{BSD, stdlib.h}
1533 @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
1534 @c Takes a lock and calls srandom_r with an automatic variable and a
1535 @c static buffer. There's no MT-safety issue because the static buffer
1536 @c is internally protected by a lock, although other threads may modify
1537 @c the set state before it is used.
1538 The @code{srandom} function sets the state of the random number
1539 generator based on the integer @var{seed}. If you supply a @var{seed} value
1540 of @code{1}, this will cause @code{random} to reproduce the default set
1543 To produce a different set of pseudo-random numbers each time your
1544 program runs, do @code{srandom (time (0))}.
1547 @deftypefun {char *} initstate (unsigned int @var{seed}, char *@var{state}, size_t @var{size})
1548 @standards{BSD, stdlib.h}
1549 @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
1550 The @code{initstate} function is used to initialize the random number
1551 generator state. The argument @var{state} is an array of @var{size}
1552 bytes, used to hold the state information. It is initialized based on
1553 @var{seed}. The size must be between 8 and 256 bytes, and should be a
1554 power of two. The bigger the @var{state} array, the better.
1556 The return value is the previous value of the state information array.
1557 You can use this value later as an argument to @code{setstate} to
1561 @deftypefun {char *} setstate (char *@var{state})
1562 @standards{BSD, stdlib.h}
1563 @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
1564 The @code{setstate} function restores the random number state
1565 information @var{state}. The argument must have been the result of
1566 a previous call to @var{initstate} or @var{setstate}.
1568 The return value is the previous value of the state information array.
1569 You can use this value later as an argument to @code{setstate} to
1572 If the function fails the return value is @code{NULL}.
1575 The four functions described so far in this section all work on a state
1576 which is shared by all threads. The state is not directly accessible to
1577 the user and can only be modified by these functions. This makes it
1578 hard to deal with situations where each thread should have its own
1579 pseudo-random number generator.
1581 @Theglibc{} contains four additional functions which contain the
1582 state as an explicit parameter and therefore make it possible to handle
1583 thread-local PRNGs. Besides this there is no difference. In fact, the
1584 four functions already discussed are implemented internally using the
1585 following interfaces.
1587 The @file{stdlib.h} header contains a definition of the following type:
1589 @deftp {Data Type} {struct random_data}
1590 @standards{GNU, stdlib.h}
1592 Objects of type @code{struct random_data} contain the information
1593 necessary to represent the state of the PRNG. Although a complete
1594 definition of the type is present the type should be treated as opaque.
1597 The functions modifying the state follow exactly the already described
1600 @deftypefun int random_r (struct random_data *restrict @var{buf}, int32_t *restrict @var{result})
1601 @standards{GNU, stdlib.h}
1602 @safety{@prelim{}@mtsafe{@mtsrace{:buf}}@assafe{}@acunsafe{@acucorrupt{}}}
1603 The @code{random_r} function behaves exactly like the @code{random}
1604 function except that it uses and modifies the state in the object
1605 pointed to by the first parameter instead of the global state.
1608 @deftypefun int srandom_r (unsigned int @var{seed}, struct random_data *@var{buf})
1609 @standards{GNU, stdlib.h}
1610 @safety{@prelim{}@mtsafe{@mtsrace{:buf}}@assafe{}@acunsafe{@acucorrupt{}}}
1611 The @code{srandom_r} function behaves exactly like the @code{srandom}
1612 function except that it uses and modifies the state in the object
1613 pointed to by the second parameter instead of the global state.
1616 @deftypefun int initstate_r (unsigned int @var{seed}, char *restrict @var{statebuf}, size_t @var{statelen}, struct random_data *restrict @var{buf})
1617 @standards{GNU, stdlib.h}
1618 @safety{@prelim{}@mtsafe{@mtsrace{:buf}}@assafe{}@acunsafe{@acucorrupt{}}}
1619 The @code{initstate_r} function behaves exactly like the @code{initstate}
1620 function except that it uses and modifies the state in the object
1621 pointed to by the fourth parameter instead of the global state.
1624 @deftypefun int setstate_r (char *restrict @var{statebuf}, struct random_data *restrict @var{buf})
1625 @standards{GNU, stdlib.h}
1626 @safety{@prelim{}@mtsafe{@mtsrace{:buf}}@assafe{}@acunsafe{@acucorrupt{}}}
1627 The @code{setstate_r} function behaves exactly like the @code{setstate}
1628 function except that it uses and modifies the state in the object
1629 pointed to by the first parameter instead of the global state.
1633 @subsection SVID Random Number Function
1635 The C library on SVID systems contains yet another kind of random number
1636 generator functions. They use a state of 48 bits of data. The user can
1637 choose among a collection of functions which return the random bits
1640 Generally there are two kinds of function. The first uses a state of
1641 the random number generator which is shared among several functions and
1642 by all threads of the process. The second requires the user to handle
1645 All functions have in common that they use the same congruential
1646 formula with the same constants. The formula is
1649 Y = (a * X + c) mod m
1653 where @var{X} is the state of the generator at the beginning and
1654 @var{Y} the state at the end. @code{a} and @code{c} are constants
1655 determining the way the generator works. By default they are
1658 a = 0x5DEECE66D = 25214903917
1663 but they can also be changed by the user. @code{m} is of course 2^48
1664 since the state consists of a 48-bit array.
1666 The prototypes for these functions are in @file{stdlib.h}.
1670 @deftypefun double drand48 (void)
1671 @standards{SVID, stdlib.h}
1672 @safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
1673 @c Uses of the static state buffer are not guarded by a lock (thus
1674 @c @mtasurace:drand48), so they may be found or left at a
1675 @c partially-updated state in case of calls from within signal handlers
1676 @c or cancellation. None of this will break safety rules or invoke
1677 @c undefined behavior, but it may affect randomness.
1678 This function returns a @code{double} value in the range of @code{0.0}
1679 to @code{1.0} (exclusive). The random bits are determined by the global
1680 state of the random number generator in the C library.
1682 Since the @code{double} type according to @w{IEEE 754} has a 52-bit
1683 mantissa this means 4 bits are not initialized by the random number
1684 generator. These are (of course) chosen to be the least significant
1685 bits and they are initialized to @code{0}.
1688 @deftypefun double erand48 (unsigned short int @var{xsubi}[3])
1689 @standards{SVID, stdlib.h}
1690 @safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
1691 @c The static buffer is just initialized with default parameters, which
1692 @c are later read to advance the state held in xsubi.
1693 This function returns a @code{double} value in the range of @code{0.0}
1694 to @code{1.0} (exclusive), similarly to @code{drand48}. The argument is
1695 an array describing the state of the random number generator.
1697 This function can be called subsequently since it updates the array to
1698 guarantee random numbers. The array should have been initialized before
1699 initial use to obtain reproducible results.
1702 @deftypefun {long int} lrand48 (void)
1703 @standards{SVID, stdlib.h}
1704 @safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
1705 The @code{lrand48} function returns an integer value in the range of
1706 @code{0} to @code{2^31} (exclusive). Even if the size of the @code{long
1707 int} type can take more than 32 bits, no higher numbers are returned.
1708 The random bits are determined by the global state of the random number
1709 generator in the C library.
1712 @deftypefun {long int} nrand48 (unsigned short int @var{xsubi}[3])
1713 @standards{SVID, stdlib.h}
1714 @safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
1715 This function is similar to the @code{lrand48} function in that it
1716 returns a number in the range of @code{0} to @code{2^31} (exclusive) but
1717 the state of the random number generator used to produce the random bits
1718 is determined by the array provided as the parameter to the function.
1720 The numbers in the array are updated afterwards so that subsequent calls
1721 to this function yield different results (as is expected of a random
1722 number generator). The array should have been initialized before the
1723 first call to obtain reproducible results.
1726 @deftypefun {long int} mrand48 (void)
1727 @standards{SVID, stdlib.h}
1728 @safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
1729 The @code{mrand48} function is similar to @code{lrand48}. The only
1730 difference is that the numbers returned are in the range @code{-2^31} to
1731 @code{2^31} (exclusive).
1734 @deftypefun {long int} jrand48 (unsigned short int @var{xsubi}[3])
1735 @standards{SVID, stdlib.h}
1736 @safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
1737 The @code{jrand48} function is similar to @code{nrand48}. The only
1738 difference is that the numbers returned are in the range @code{-2^31} to
1739 @code{2^31} (exclusive). For the @code{xsubi} parameter the same
1740 requirements are necessary.
1743 The internal state of the random number generator can be initialized in
1744 several ways. The methods differ in the completeness of the
1745 information provided.
1747 @deftypefun void srand48 (long int @var{seedval})
1748 @standards{SVID, stdlib.h}
1749 @safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
1750 The @code{srand48} function sets the most significant 32 bits of the
1751 internal state of the random number generator to the least
1752 significant 32 bits of the @var{seedval} parameter. The lower 16 bits
1753 are initialized to the value @code{0x330E}. Even if the @code{long
1754 int} type contains more than 32 bits only the lower 32 bits are used.
1756 Owing to this limitation, initialization of the state of this
1757 function is not very useful. But it makes it easy to use a construct
1758 like @code{srand48 (time (0))}.
1760 A side-effect of this function is that the values @code{a} and @code{c}
1761 from the internal state, which are used in the congruential formula,
1762 are reset to the default values given above. This is of importance once
1763 the user has called the @code{lcong48} function (see below).
1766 @deftypefun {unsigned short int *} seed48 (unsigned short int @var{seed16v}[3])
1767 @standards{SVID, stdlib.h}
1768 @safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
1769 The @code{seed48} function initializes all 48 bits of the state of the
1770 internal random number generator from the contents of the parameter
1771 @var{seed16v}. Here the lower 16 bits of the first element of
1772 @var{seed16v} initialize the least significant 16 bits of the internal
1773 state, the lower 16 bits of @code{@var{seed16v}[1]} initialize the mid-order
1774 16 bits of the state and the 16 lower bits of @code{@var{seed16v}[2]}
1775 initialize the most significant 16 bits of the state.
1777 Unlike @code{srand48} this function lets the user initialize all 48 bits
1780 The value returned by @code{seed48} is a pointer to an array containing
1781 the values of the internal state before the change. This might be
1782 useful to restart the random number generator at a certain state.
1783 Otherwise the value can simply be ignored.
1785 As for @code{srand48}, the values @code{a} and @code{c} from the
1786 congruential formula are reset to the default values.
1789 There is one more function to initialize the random number generator
1790 which enables you to specify even more information by allowing you to
1791 change the parameters in the congruential formula.
1793 @deftypefun void lcong48 (unsigned short int @var{param}[7])
1794 @standards{SVID, stdlib.h}
1795 @safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
1796 The @code{lcong48} function allows the user to change the complete state
1797 of the random number generator. Unlike @code{srand48} and
1798 @code{seed48}, this function also changes the constants in the
1799 congruential formula.
1801 From the seven elements in the array @var{param} the least significant
1802 16 bits of the entries @code{@var{param}[0]} to @code{@var{param}[2]}
1803 determine the initial state, the least significant 16 bits of
1804 @code{@var{param}[3]} to @code{@var{param}[5]} determine the 48 bit
1805 constant @code{a} and @code{@var{param}[6]} determines the 16-bit value
1809 All the above functions have in common that they use the global
1810 parameters for the congruential formula. In multi-threaded programs it
1811 might sometimes be useful to have different parameters in different
1812 threads. For this reason all the above functions have a counterpart
1813 which works on a description of the random number generator in the
1814 user-supplied buffer instead of the global state.
1816 Please note that it is no problem if several threads use the global
1817 state if all threads use the functions which take a pointer to an array
1818 containing the state. The random numbers are computed following the
1819 same loop but if the state in the array is different all threads will
1820 obtain an individual random number generator.
1822 The user-supplied buffer must be of type @code{struct drand48_data}.
1823 This type should be regarded as opaque and not manipulated directly.
1825 @deftypefun int drand48_r (struct drand48_data *@var{buffer}, double *@var{result})
1826 @standards{GNU, stdlib.h}
1827 @safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
1828 This function is equivalent to the @code{drand48} function with the
1829 difference that it does not modify the global random number generator
1830 parameters but instead the parameters in the buffer supplied through the
1831 pointer @var{buffer}. The random number is returned in the variable
1832 pointed to by @var{result}.
1834 The return value of the function indicates whether the call succeeded.
1835 If the value is less than @code{0} an error occurred and @var{errno} is
1836 set to indicate the problem.
1838 This function is a GNU extension and should not be used in portable
1842 @deftypefun int erand48_r (unsigned short int @var{xsubi}[3], struct drand48_data *@var{buffer}, double *@var{result})
1843 @standards{GNU, stdlib.h}
1844 @safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
1845 The @code{erand48_r} function works like @code{erand48}, but in addition
1846 it takes an argument @var{buffer} which describes the random number
1847 generator. The state of the random number generator is taken from the
1848 @code{xsubi} array, the parameters for the congruential formula from the
1849 global random number generator data. The random number is returned in
1850 the variable pointed to by @var{result}.
1852 The return value is non-negative if the call succeeded.
1854 This function is a GNU extension and should not be used in portable
1858 @deftypefun int lrand48_r (struct drand48_data *@var{buffer}, long int *@var{result})
1859 @standards{GNU, stdlib.h}
1860 @safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
1861 This function is similar to @code{lrand48}, but in addition it takes a
1862 pointer to a buffer describing the state of the random number generator
1863 just like @code{drand48}.
1865 If the return value of the function is non-negative the variable pointed
1866 to by @var{result} contains the result. Otherwise an error occurred.
1868 This function is a GNU extension and should not be used in portable
1872 @deftypefun int nrand48_r (unsigned short int @var{xsubi}[3], struct drand48_data *@var{buffer}, long int *@var{result})
1873 @standards{GNU, stdlib.h}
1874 @safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
1875 The @code{nrand48_r} function works like @code{nrand48} in that it
1876 produces a random number in the range @code{0} to @code{2^31}. But instead
1877 of using the global parameters for the congruential formula it uses the
1878 information from the buffer pointed to by @var{buffer}. The state is
1879 described by the values in @var{xsubi}.
1881 If the return value is non-negative the variable pointed to by
1882 @var{result} contains the result.
1884 This function is a GNU extension and should not be used in portable
1888 @deftypefun int mrand48_r (struct drand48_data *@var{buffer}, long int *@var{result})
1889 @standards{GNU, stdlib.h}
1890 @safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
1891 This function is similar to @code{mrand48} but like the other reentrant
1892 functions it uses the random number generator described by the value in
1893 the buffer pointed to by @var{buffer}.
1895 If the return value is non-negative the variable pointed to by
1896 @var{result} contains the result.
1898 This function is a GNU extension and should not be used in portable
1902 @deftypefun int jrand48_r (unsigned short int @var{xsubi}[3], struct drand48_data *@var{buffer}, long int *@var{result})
1903 @standards{GNU, stdlib.h}
1904 @safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
1905 The @code{jrand48_r} function is similar to @code{jrand48}. Like the
1906 other reentrant functions of this function family it uses the
1907 congruential formula parameters from the buffer pointed to by
1910 If the return value is non-negative the variable pointed to by
1911 @var{result} contains the result.
1913 This function is a GNU extension and should not be used in portable
1917 Before any of the above functions are used the buffer of type
1918 @code{struct drand48_data} should be initialized. The easiest way to do
1919 this is to fill the whole buffer with null bytes, e.g. by
1922 memset (buffer, '\0', sizeof (struct drand48_data));
1926 Using any of the reentrant functions of this family now will
1927 automatically initialize the random number generator to the default
1928 values for the state and the parameters of the congruential formula.
1930 The other possibility is to use any of the functions which explicitly
1931 initialize the buffer. Though it might be obvious how to initialize the
1932 buffer from looking at the parameter to the function, it is highly
1933 recommended to use these functions since the result might not always be
1936 @deftypefun int srand48_r (long int @var{seedval}, struct drand48_data *@var{buffer})
1937 @standards{GNU, stdlib.h}
1938 @safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
1939 The description of the random number generator represented by the
1940 information in @var{buffer} is initialized similarly to what the function
1941 @code{srand48} does. The state is initialized from the parameter
1942 @var{seedval} and the parameters for the congruential formula are
1943 initialized to their default values.
1945 If the return value is non-negative the function call succeeded.
1947 This function is a GNU extension and should not be used in portable
1951 @deftypefun int seed48_r (unsigned short int @var{seed16v}[3], struct drand48_data *@var{buffer})
1952 @standards{GNU, stdlib.h}
1953 @safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
1954 This function is similar to @code{srand48_r} but like @code{seed48} it
1955 initializes all 48 bits of the state from the parameter @var{seed16v}.
1957 If the return value is non-negative the function call succeeded. It
1958 does not return a pointer to the previous state of the random number
1959 generator like the @code{seed48} function does. If the user wants to
1960 preserve the state for a later re-run s/he can copy the whole buffer
1961 pointed to by @var{buffer}.
1963 This function is a GNU extension and should not be used in portable
1967 @deftypefun int lcong48_r (unsigned short int @var{param}[7], struct drand48_data *@var{buffer})
1968 @standards{GNU, stdlib.h}
1969 @safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
1970 This function initializes all aspects of the random number generator
1971 described in @var{buffer} with the data in @var{param}. Here it is
1972 especially true that the function does more than just copying the
1973 contents of @var{param} and @var{buffer}. More work is required and
1974 therefore it is important to use this function rather than initializing
1975 the random number generator directly.
1977 If the return value is non-negative the function call succeeded.
1979 This function is a GNU extension and should not be used in portable
1983 @node FP Function Optimizations
1984 @section Is Fast Code or Small Code preferred?
1985 @cindex Optimization
1987 If an application uses many floating point functions it is often the case
1988 that the cost of the function calls themselves is not negligible.
1989 Modern processors can often execute the operations themselves
1990 very fast, but the function call disrupts the instruction pipeline.
1992 For this reason @theglibc{} provides optimizations for many of the
1993 frequently-used math functions. When GNU CC is used and the user
1994 activates the optimizer, several new inline functions and macros are
1995 defined. These new functions and macros have the same names as the
1996 library functions and so are used instead of the latter. In the case of
1997 inline functions the compiler will decide whether it is reasonable to
1998 use them, and this decision is usually correct.
2000 This means that no calls to the library functions may be necessary, and
2001 can increase the speed of generated code significantly. The drawback is
2002 that code size will increase, and the increase is not always negligible.
2004 There are two kinds of inline functions: those that give the same result
2005 as the library functions and others that might not set @code{errno} and
2006 might have a reduced precision and/or argument range in comparison with
2007 the library functions. The latter inline functions are only available
2008 if the flag @code{-ffast-math} is given to GNU CC.
2010 In cases where the inline functions and macros are not wanted the symbol
2011 @code{__NO_MATH_INLINES} should be defined before any system header is
2012 included. This will ensure that only library functions are used. Of
2013 course, it can be determined for each file in the project whether
2014 giving this option is preferable or not.
2016 Not all hardware implements the entire @w{IEEE 754} standard, and even
2017 if it does there may be a substantial performance penalty for using some
2018 of its features. For example, enabling traps on some processors forces
2019 the FPU to run un-pipelined, which can more than double calculation time.
2020 @c ***Add explanation of -lieee, -mieee.