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