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