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