]>
Commit | Line | Data |
---|---|---|
28f540f4 | 1 | @node Arithmetic, Date and Time, Mathematics, Top |
7a68c94a UD |
2 | @c %MENU% Low level arithmetic functions |
3 | @chapter Arithmetic Functions | |
28f540f4 RM |
4 | |
5 | This chapter contains information about functions for doing basic | |
6 | arithmetic operations, such as splitting a float into its integer and | |
b4012b75 UD |
7 | fractional parts or retrieving the imaginary part of a complex value. |
8 | These functions are declared in the header files @file{math.h} and | |
9 | @file{complex.h}. | |
28f540f4 RM |
10 | |
11 | @menu | |
0e4ee106 UD |
12 | * Integers:: Basic integer types and concepts |
13 | * Integer Division:: Integer division with guaranteed rounding. | |
7a68c94a UD |
14 | * Floating Point Numbers:: Basic concepts. IEEE 754. |
15 | * Floating Point Classes:: The five kinds of floating-point number. | |
16 | * Floating Point Errors:: When something goes wrong in a calculation. | |
17 | * Rounding:: Controlling how results are rounded. | |
18 | * Control Functions:: Saving and restoring the FPU's state. | |
19 | * Arithmetic Functions:: Fundamental operations provided by the library. | |
20 | * Complex Numbers:: The types. Writing complex constants. | |
21 | * Operations on Complex:: Projection, conjugation, decomposition. | |
7a68c94a UD |
22 | * Parsing of Numbers:: Converting strings to numbers. |
23 | * System V Number Conversion:: An archaic way to convert numbers to strings. | |
28f540f4 RM |
24 | @end menu |
25 | ||
0e4ee106 UD |
26 | @node Integers |
27 | @section Integers | |
28 | @cindex integer | |
29 | ||
30 | The C language defines several integer data types: integer, short integer, | |
31 | long integer, and character, all in both signed and unsigned varieties. | |
e6e81391 UD |
32 | The GNU C compiler extends the language to contain long long integers |
33 | as well. | |
0e4ee106 UD |
34 | @cindex signedness |
35 | ||
36 | The C integer types were intended to allow code to be portable among | |
37 | machines with different inherent data sizes (word sizes), so each type | |
38 | may have different ranges on different machines. The problem with | |
39 | this is that a program often needs to be written for a particular range | |
40 | of integers, and sometimes must be written for a particular size of | |
41 | storage, regardless of what machine the program runs on. | |
42 | ||
1f77f049 | 43 | To address this problem, @theglibc{} contains C type definitions |
0e4ee106 | 44 | you can use to declare integers that meet your exact needs. Because the |
1f77f049 | 45 | @glibcadj{} header files are customized to a specific machine, your |
0e4ee106 UD |
46 | program source code doesn't have to be. |
47 | ||
48 | These @code{typedef}s are in @file{stdint.h}. | |
49 | @pindex stdint.h | |
50 | ||
51 | If you require that an integer be represented in exactly N bits, use one | |
52 | of the following types, with the obvious mapping to bit size and signedness: | |
53 | ||
68979757 | 54 | @itemize @bullet |
0e4ee106 UD |
55 | @item int8_t |
56 | @item int16_t | |
57 | @item int32_t | |
58 | @item int64_t | |
59 | @item uint8_t | |
60 | @item uint16_t | |
61 | @item uint32_t | |
62 | @item uint64_t | |
63 | @end itemize | |
64 | ||
65 | If your C compiler and target machine do not allow integers of a certain | |
66 | size, the corresponding above type does not exist. | |
67 | ||
68 | If you don't need a specific storage size, but want the smallest data | |
69 | structure with @emph{at least} N bits, use one of these: | |
70 | ||
68979757 | 71 | @itemize @bullet |
150f9fb8 AJ |
72 | @item int_least8_t |
73 | @item int_least16_t | |
74 | @item int_least32_t | |
75 | @item int_least64_t | |
76 | @item uint_least8_t | |
77 | @item uint_least16_t | |
78 | @item uint_least32_t | |
79 | @item uint_least64_t | |
0e4ee106 UD |
80 | @end itemize |
81 | ||
e6e81391 | 82 | If you don't need a specific storage size, but want the data structure |
0e4ee106 UD |
83 | that allows the fastest access while having at least N bits (and |
84 | among data structures with the same access speed, the smallest one), use | |
85 | one of these: | |
86 | ||
68979757 | 87 | @itemize @bullet |
150f9fb8 AJ |
88 | @item int_fast8_t |
89 | @item int_fast16_t | |
90 | @item int_fast32_t | |
91 | @item int_fast64_t | |
92 | @item uint_fast8_t | |
93 | @item uint_fast16_t | |
94 | @item uint_fast32_t | |
95 | @item uint_fast64_t | |
0e4ee106 UD |
96 | @end itemize |
97 | ||
e6e81391 | 98 | If you want an integer with the widest range possible on the platform on |
0e4ee106 UD |
99 | which it is being used, use one of the following. If you use these, |
100 | you should write code that takes into account the variable size and range | |
101 | of the integer. | |
102 | ||
68979757 | 103 | @itemize @bullet |
0e4ee106 UD |
104 | @item intmax_t |
105 | @item uintmax_t | |
106 | @end itemize | |
107 | ||
1f77f049 | 108 | @Theglibc{} also provides macros that tell you the maximum and |
0e4ee106 UD |
109 | minimum possible values for each integer data type. The macro names |
110 | follow these examples: @code{INT32_MAX}, @code{UINT8_MAX}, | |
111 | @code{INT_FAST32_MIN}, @code{INT_LEAST64_MIN}, @code{UINTMAX_MAX}, | |
112 | @code{INTMAX_MAX}, @code{INTMAX_MIN}. Note that there are no macros for | |
5b17fd0d JM |
113 | unsigned integer minima. These are always zero. Similiarly, there |
114 | are macros such as @code{INTMAX_WIDTH} for the width of these types. | |
115 | Those macros for integer type widths come from TS 18661-1:2014. | |
0e4ee106 | 116 | @cindex maximum possible integer |
0bc93a2f | 117 | @cindex minimum possible integer |
0e4ee106 UD |
118 | |
119 | There are similar macros for use with C's built in integer types which | |
120 | should come with your C compiler. These are described in @ref{Data Type | |
121 | Measurements}. | |
122 | ||
123 | Don't forget you can use the C @code{sizeof} function with any of these | |
124 | data types to get the number of bytes of storage each uses. | |
125 | ||
126 | ||
127 | @node Integer Division | |
128 | @section Integer Division | |
129 | @cindex integer division functions | |
130 | ||
131 | This section describes functions for performing integer division. These | |
132 | functions are redundant when GNU CC is used, because in GNU C the | |
133 | @samp{/} operator always rounds towards zero. But in other C | |
134 | implementations, @samp{/} may round differently with negative arguments. | |
135 | @code{div} and @code{ldiv} are useful because they specify how to round | |
136 | the quotient: towards zero. The remainder has the same sign as the | |
137 | numerator. | |
138 | ||
139 | These functions are specified to return a result @var{r} such that the value | |
140 | @code{@var{r}.quot*@var{denominator} + @var{r}.rem} equals | |
141 | @var{numerator}. | |
142 | ||
143 | @pindex stdlib.h | |
144 | To use these facilities, you should include the header file | |
145 | @file{stdlib.h} in your program. | |
146 | ||
147 | @comment stdlib.h | |
148 | @comment ISO | |
149 | @deftp {Data Type} div_t | |
150 | This is a structure type used to hold the result returned by the @code{div} | |
151 | function. It has the following members: | |
152 | ||
153 | @table @code | |
154 | @item int quot | |
155 | The quotient from the division. | |
156 | ||
157 | @item int rem | |
158 | The remainder from the division. | |
159 | @end table | |
160 | @end deftp | |
161 | ||
162 | @comment stdlib.h | |
163 | @comment ISO | |
164 | @deftypefun div_t div (int @var{numerator}, int @var{denominator}) | |
b719dafd AO |
165 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
166 | @c Functions in this section are pure, and thus safe. | |
0e4ee106 UD |
167 | This function @code{div} computes the quotient and remainder from |
168 | the division of @var{numerator} by @var{denominator}, returning the | |
169 | result in a structure of type @code{div_t}. | |
170 | ||
171 | If the result cannot be represented (as in a division by zero), the | |
172 | behavior is undefined. | |
173 | ||
174 | Here is an example, albeit not a very useful one. | |
175 | ||
176 | @smallexample | |
177 | div_t result; | |
178 | result = div (20, -6); | |
179 | @end smallexample | |
180 | ||
181 | @noindent | |
182 | Now @code{result.quot} is @code{-3} and @code{result.rem} is @code{2}. | |
183 | @end deftypefun | |
184 | ||
185 | @comment stdlib.h | |
186 | @comment ISO | |
187 | @deftp {Data Type} ldiv_t | |
188 | This is a structure type used to hold the result returned by the @code{ldiv} | |
189 | function. It has the following members: | |
190 | ||
191 | @table @code | |
192 | @item long int quot | |
193 | The quotient from the division. | |
194 | ||
195 | @item long int rem | |
196 | The remainder from the division. | |
197 | @end table | |
198 | ||
199 | (This is identical to @code{div_t} except that the components are of | |
200 | type @code{long int} rather than @code{int}.) | |
201 | @end deftp | |
202 | ||
203 | @comment stdlib.h | |
204 | @comment ISO | |
205 | @deftypefun ldiv_t ldiv (long int @var{numerator}, long int @var{denominator}) | |
b719dafd | 206 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
0e4ee106 UD |
207 | The @code{ldiv} function is similar to @code{div}, except that the |
208 | arguments are of type @code{long int} and the result is returned as a | |
209 | structure of type @code{ldiv_t}. | |
210 | @end deftypefun | |
211 | ||
212 | @comment stdlib.h | |
213 | @comment ISO | |
214 | @deftp {Data Type} lldiv_t | |
215 | This is a structure type used to hold the result returned by the @code{lldiv} | |
216 | function. It has the following members: | |
217 | ||
218 | @table @code | |
219 | @item long long int quot | |
220 | The quotient from the division. | |
221 | ||
222 | @item long long int rem | |
223 | The remainder from the division. | |
224 | @end table | |
225 | ||
226 | (This is identical to @code{div_t} except that the components are of | |
227 | type @code{long long int} rather than @code{int}.) | |
228 | @end deftp | |
229 | ||
230 | @comment stdlib.h | |
231 | @comment ISO | |
232 | @deftypefun lldiv_t lldiv (long long int @var{numerator}, long long int @var{denominator}) | |
b719dafd | 233 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
0e4ee106 UD |
234 | The @code{lldiv} function is like the @code{div} function, but the |
235 | arguments are of type @code{long long int} and the result is returned as | |
236 | a structure of type @code{lldiv_t}. | |
237 | ||
238 | The @code{lldiv} function was added in @w{ISO C99}. | |
239 | @end deftypefun | |
240 | ||
241 | @comment inttypes.h | |
242 | @comment ISO | |
243 | @deftp {Data Type} imaxdiv_t | |
244 | This is a structure type used to hold the result returned by the @code{imaxdiv} | |
245 | function. It has the following members: | |
246 | ||
247 | @table @code | |
248 | @item intmax_t quot | |
249 | The quotient from the division. | |
250 | ||
251 | @item intmax_t rem | |
252 | The remainder from the division. | |
253 | @end table | |
254 | ||
255 | (This is identical to @code{div_t} except that the components are of | |
256 | type @code{intmax_t} rather than @code{int}.) | |
257 | ||
258 | See @ref{Integers} for a description of the @code{intmax_t} type. | |
259 | ||
260 | @end deftp | |
261 | ||
262 | @comment inttypes.h | |
263 | @comment ISO | |
264 | @deftypefun imaxdiv_t imaxdiv (intmax_t @var{numerator}, intmax_t @var{denominator}) | |
b719dafd | 265 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
0e4ee106 UD |
266 | The @code{imaxdiv} function is like the @code{div} function, but the |
267 | arguments are of type @code{intmax_t} and the result is returned as | |
268 | a structure of type @code{imaxdiv_t}. | |
269 | ||
270 | See @ref{Integers} for a description of the @code{intmax_t} type. | |
271 | ||
272 | The @code{imaxdiv} function was added in @w{ISO C99}. | |
273 | @end deftypefun | |
274 | ||
275 | ||
7a68c94a UD |
276 | @node Floating Point Numbers |
277 | @section Floating Point Numbers | |
278 | @cindex floating point | |
279 | @cindex IEEE 754 | |
b4012b75 UD |
280 | @cindex IEEE floating point |
281 | ||
7a68c94a UD |
282 | Most computer hardware has support for two different kinds of numbers: |
283 | integers (@math{@dots{}-3, -2, -1, 0, 1, 2, 3@dots{}}) and | |
284 | floating-point numbers. Floating-point numbers have three parts: the | |
285 | @dfn{mantissa}, the @dfn{exponent}, and the @dfn{sign bit}. The real | |
286 | number represented by a floating-point value is given by | |
287 | @tex | |
288 | $(s \mathrel? -1 \mathrel: 1) \cdot 2^e \cdot M$ | |
289 | @end tex | |
290 | @ifnottex | |
291 | @math{(s ? -1 : 1) @mul{} 2^e @mul{} M} | |
292 | @end ifnottex | |
293 | where @math{s} is the sign bit, @math{e} the exponent, and @math{M} | |
294 | the mantissa. @xref{Floating Point Concepts}, for details. (It is | |
295 | possible to have a different @dfn{base} for the exponent, but all modern | |
296 | hardware uses @math{2}.) | |
297 | ||
298 | Floating-point numbers can represent a finite subset of the real | |
299 | numbers. While this subset is large enough for most purposes, it is | |
300 | important to remember that the only reals that can be represented | |
301 | exactly are rational numbers that have a terminating binary expansion | |
302 | shorter than the width of the mantissa. Even simple fractions such as | |
303 | @math{1/5} can only be approximated by floating point. | |
304 | ||
305 | Mathematical operations and functions frequently need to produce values | |
306 | that are not representable. Often these values can be approximated | |
307 | closely enough for practical purposes, but sometimes they can't. | |
308 | Historically there was no way to tell when the results of a calculation | |
309 | were inaccurate. Modern computers implement the @w{IEEE 754} standard | |
310 | for numerical computations, which defines a framework for indicating to | |
311 | the program when the results of calculation are not trustworthy. This | |
312 | framework consists of a set of @dfn{exceptions} that indicate why a | |
313 | result could not be represented, and the special values @dfn{infinity} | |
314 | and @dfn{not a number} (NaN). | |
315 | ||
316 | @node Floating Point Classes | |
317 | @section Floating-Point Number Classification Functions | |
318 | @cindex floating-point classes | |
319 | @cindex classes, floating-point | |
320 | @pindex math.h | |
b4012b75 | 321 | |
ec751a23 | 322 | @w{ISO C99} defines macros that let you determine what sort of |
7a68c94a | 323 | floating-point number a variable holds. |
b4012b75 UD |
324 | |
325 | @comment math.h | |
326 | @comment ISO | |
7a68c94a | 327 | @deftypefn {Macro} int fpclassify (@emph{float-type} @var{x}) |
b719dafd | 328 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
329 | This is a generic macro which works on all floating-point types and |
330 | which returns a value of type @code{int}. The possible values are: | |
28f540f4 | 331 | |
7a68c94a UD |
332 | @vtable @code |
333 | @item FP_NAN | |
334 | The floating-point number @var{x} is ``Not a Number'' (@pxref{Infinity | |
335 | and NaN}) | |
336 | @item FP_INFINITE | |
337 | The value of @var{x} is either plus or minus infinity (@pxref{Infinity | |
338 | and NaN}) | |
339 | @item FP_ZERO | |
340 | The value of @var{x} is zero. In floating-point formats like @w{IEEE | |
341 | 754}, where zero can be signed, this value is also returned if | |
342 | @var{x} is negative zero. | |
343 | @item FP_SUBNORMAL | |
344 | Numbers whose absolute value is too small to be represented in the | |
345 | normal format are represented in an alternate, @dfn{denormalized} format | |
346 | (@pxref{Floating Point Concepts}). This format is less precise but can | |
347 | represent values closer to zero. @code{fpclassify} returns this value | |
348 | for values of @var{x} in this alternate format. | |
349 | @item FP_NORMAL | |
350 | This value is returned for all other values of @var{x}. It indicates | |
351 | that there is nothing special about the number. | |
352 | @end vtable | |
28f540f4 | 353 | |
7a68c94a | 354 | @end deftypefn |
28f540f4 | 355 | |
7a68c94a UD |
356 | @code{fpclassify} is most useful if more than one property of a number |
357 | must be tested. There are more specific macros which only test one | |
358 | property at a time. Generally these macros execute faster than | |
359 | @code{fpclassify}, since there is special hardware support for them. | |
360 | You should therefore use the specific macros whenever possible. | |
28f540f4 | 361 | |
29cb9293 JM |
362 | @comment math.h |
363 | @comment ISO | |
364 | @deftypefn {Macro} int iscanonical (@emph{float-type} @var{x}) | |
365 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} | |
366 | In some floating-point formats, some values have canonical (preferred) | |
367 | and noncanonical encodings (for IEEE interchange binary formats, all | |
368 | encodings are canonical). This macro returns a nonzero value if | |
369 | @var{x} has a canonical encoding. It is from TS 18661-1:2014. | |
370 | ||
371 | Note that some formats have multiple encodings of a value which are | |
372 | all equally canonical; @code{iscanonical} returns a nonzero value for | |
373 | all such encodings. Also, formats may have encodings that do not | |
374 | correspond to any valid value of the type. In ISO C terms these are | |
375 | @dfn{trap representations}; in @theglibc{}, @code{iscanonical} returns | |
376 | zero for such encodings. | |
377 | @end deftypefn | |
378 | ||
28f540f4 | 379 | @comment math.h |
7a68c94a UD |
380 | @comment ISO |
381 | @deftypefn {Macro} int isfinite (@emph{float-type} @var{x}) | |
b719dafd | 382 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
383 | This macro returns a nonzero value if @var{x} is finite: not plus or |
384 | minus infinity, and not NaN. It is equivalent to | |
fe0ec73e UD |
385 | |
386 | @smallexample | |
7a68c94a | 387 | (fpclassify (x) != FP_NAN && fpclassify (x) != FP_INFINITE) |
fe0ec73e UD |
388 | @end smallexample |
389 | ||
7a68c94a UD |
390 | @code{isfinite} is implemented as a macro which accepts any |
391 | floating-point type. | |
392 | @end deftypefn | |
fe0ec73e | 393 | |
7a68c94a UD |
394 | @comment math.h |
395 | @comment ISO | |
396 | @deftypefn {Macro} int isnormal (@emph{float-type} @var{x}) | |
b719dafd | 397 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
398 | This macro returns a nonzero value if @var{x} is finite and normalized. |
399 | It is equivalent to | |
b4012b75 UD |
400 | |
401 | @smallexample | |
7a68c94a | 402 | (fpclassify (x) == FP_NORMAL) |
b4012b75 | 403 | @end smallexample |
7a68c94a | 404 | @end deftypefn |
b4012b75 | 405 | |
7a68c94a UD |
406 | @comment math.h |
407 | @comment ISO | |
408 | @deftypefn {Macro} int isnan (@emph{float-type} @var{x}) | |
b719dafd | 409 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
410 | This macro returns a nonzero value if @var{x} is NaN. It is equivalent |
411 | to | |
b4012b75 UD |
412 | |
413 | @smallexample | |
7a68c94a | 414 | (fpclassify (x) == FP_NAN) |
b4012b75 | 415 | @end smallexample |
7a68c94a | 416 | @end deftypefn |
b4012b75 | 417 | |
57267616 | 418 | @comment math.h |
bf91be88 | 419 | @comment ISO |
57267616 | 420 | @deftypefn {Macro} int issignaling (@emph{float-type} @var{x}) |
b719dafd | 421 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
57267616 | 422 | This macro returns a nonzero value if @var{x} is a signaling NaN |
bf91be88 | 423 | (sNaN). It is from TS 18661-1:2014. |
57267616 TS |
424 | @end deftypefn |
425 | ||
d942e95c JM |
426 | @comment math.h |
427 | @comment ISO | |
428 | @deftypefn {Macro} int issubnormal (@emph{float-type} @var{x}) | |
429 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} | |
430 | This macro returns a nonzero value if @var{x} is subnormal. It is | |
431 | from TS 18661-1:2014. | |
432 | @end deftypefn | |
433 | ||
bb8081f5 JM |
434 | @comment math.h |
435 | @comment ISO | |
436 | @deftypefn {Macro} int iszero (@emph{float-type} @var{x}) | |
437 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} | |
438 | This macro returns a nonzero value if @var{x} is zero. It is from TS | |
439 | 18661-1:2014. | |
440 | @end deftypefn | |
441 | ||
7a68c94a | 442 | Another set of floating-point classification functions was provided by |
1f77f049 | 443 | BSD. @Theglibc{} also supports these functions; however, we |
ec751a23 | 444 | recommend that you use the ISO C99 macros in new code. Those are standard |
7a68c94a UD |
445 | and will be available more widely. Also, since they are macros, you do |
446 | not have to worry about the type of their argument. | |
28f540f4 RM |
447 | |
448 | @comment math.h | |
449 | @comment BSD | |
450 | @deftypefun int isinf (double @var{x}) | |
4260bc74 UD |
451 | @comment math.h |
452 | @comment BSD | |
779ae82e | 453 | @deftypefunx int isinff (float @var{x}) |
4260bc74 UD |
454 | @comment math.h |
455 | @comment BSD | |
779ae82e | 456 | @deftypefunx int isinfl (long double @var{x}) |
b719dafd | 457 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
28f540f4 RM |
458 | This function returns @code{-1} if @var{x} represents negative infinity, |
459 | @code{1} if @var{x} represents positive infinity, and @code{0} otherwise. | |
460 | @end deftypefun | |
461 | ||
462 | @comment math.h | |
463 | @comment BSD | |
464 | @deftypefun int isnan (double @var{x}) | |
4260bc74 UD |
465 | @comment math.h |
466 | @comment BSD | |
779ae82e | 467 | @deftypefunx int isnanf (float @var{x}) |
4260bc74 UD |
468 | @comment math.h |
469 | @comment BSD | |
779ae82e | 470 | @deftypefunx int isnanl (long double @var{x}) |
b719dafd | 471 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
28f540f4 | 472 | This function returns a nonzero value if @var{x} is a ``not a number'' |
7a68c94a | 473 | value, and zero otherwise. |
b9b49b44 | 474 | |
48b22986 | 475 | @strong{NB:} The @code{isnan} macro defined by @w{ISO C99} overrides |
7a68c94a UD |
476 | the BSD function. This is normally not a problem, because the two |
477 | routines behave identically. However, if you really need to get the BSD | |
478 | function for some reason, you can write | |
b9b49b44 | 479 | |
7a68c94a UD |
480 | @smallexample |
481 | (isnan) (x) | |
482 | @end smallexample | |
28f540f4 RM |
483 | @end deftypefun |
484 | ||
485 | @comment math.h | |
486 | @comment BSD | |
487 | @deftypefun int finite (double @var{x}) | |
4260bc74 UD |
488 | @comment math.h |
489 | @comment BSD | |
779ae82e | 490 | @deftypefunx int finitef (float @var{x}) |
4260bc74 UD |
491 | @comment math.h |
492 | @comment BSD | |
779ae82e | 493 | @deftypefunx int finitel (long double @var{x}) |
b719dafd | 494 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
28f540f4 RM |
495 | This function returns a nonzero value if @var{x} is finite or a ``not a |
496 | number'' value, and zero otherwise. | |
497 | @end deftypefun | |
498 | ||
28f540f4 RM |
499 | @strong{Portability Note:} The functions listed in this section are BSD |
500 | extensions. | |
501 | ||
b4012b75 | 502 | |
7a68c94a UD |
503 | @node Floating Point Errors |
504 | @section Errors in Floating-Point Calculations | |
505 | ||
506 | @menu | |
507 | * FP Exceptions:: IEEE 754 math exceptions and how to detect them. | |
508 | * Infinity and NaN:: Special values returned by calculations. | |
509 | * Status bit operations:: Checking for exceptions after the fact. | |
510 | * Math Error Reporting:: How the math functions report errors. | |
511 | @end menu | |
512 | ||
513 | @node FP Exceptions | |
514 | @subsection FP Exceptions | |
515 | @cindex exception | |
516 | @cindex signal | |
517 | @cindex zero divide | |
518 | @cindex division by zero | |
519 | @cindex inexact exception | |
520 | @cindex invalid exception | |
521 | @cindex overflow exception | |
522 | @cindex underflow exception | |
523 | ||
524 | The @w{IEEE 754} standard defines five @dfn{exceptions} that can occur | |
525 | during a calculation. Each corresponds to a particular sort of error, | |
526 | such as overflow. | |
527 | ||
528 | When exceptions occur (when exceptions are @dfn{raised}, in the language | |
529 | of the standard), one of two things can happen. By default the | |
530 | exception is simply noted in the floating-point @dfn{status word}, and | |
531 | the program continues as if nothing had happened. The operation | |
532 | produces a default value, which depends on the exception (see the table | |
533 | below). Your program can check the status word to find out which | |
534 | exceptions happened. | |
535 | ||
536 | Alternatively, you can enable @dfn{traps} for exceptions. In that case, | |
537 | when an exception is raised, your program will receive the @code{SIGFPE} | |
538 | signal. The default action for this signal is to terminate the | |
8b7fb588 | 539 | program. @xref{Signal Handling}, for how you can change the effect of |
7a68c94a UD |
540 | the signal. |
541 | ||
542 | @findex matherr | |
543 | In the System V math library, the user-defined function @code{matherr} | |
544 | is called when certain exceptions occur inside math library functions. | |
545 | However, the Unix98 standard deprecates this interface. We support it | |
546 | for historical compatibility, but recommend that you do not use it in | |
c5df7609 JM |
547 | new programs. When this interface is used, exceptions may not be |
548 | raised. | |
7a68c94a UD |
549 | |
550 | @noindent | |
551 | The exceptions defined in @w{IEEE 754} are: | |
552 | ||
553 | @table @samp | |
554 | @item Invalid Operation | |
555 | This exception is raised if the given operands are invalid for the | |
556 | operation to be performed. Examples are | |
557 | (see @w{IEEE 754}, @w{section 7}): | |
558 | @enumerate | |
559 | @item | |
560 | Addition or subtraction: @math{@infinity{} - @infinity{}}. (But | |
561 | @math{@infinity{} + @infinity{} = @infinity{}}). | |
562 | @item | |
563 | Multiplication: @math{0 @mul{} @infinity{}}. | |
564 | @item | |
565 | Division: @math{0/0} or @math{@infinity{}/@infinity{}}. | |
566 | @item | |
567 | Remainder: @math{x} REM @math{y}, where @math{y} is zero or @math{x} is | |
568 | infinite. | |
569 | @item | |
570 | Square root if the operand is less then zero. More generally, any | |
571 | mathematical function evaluated outside its domain produces this | |
572 | exception. | |
573 | @item | |
574 | Conversion of a floating-point number to an integer or decimal | |
575 | string, when the number cannot be represented in the target format (due | |
576 | to overflow, infinity, or NaN). | |
577 | @item | |
578 | Conversion of an unrecognizable input string. | |
579 | @item | |
580 | Comparison via predicates involving @math{<} or @math{>}, when one or | |
581 | other of the operands is NaN. You can prevent this exception by using | |
582 | the unordered comparison functions instead; see @ref{FP Comparison Functions}. | |
583 | @end enumerate | |
584 | ||
585 | If the exception does not trap, the result of the operation is NaN. | |
586 | ||
587 | @item Division by Zero | |
588 | This exception is raised when a finite nonzero number is divided | |
589 | by zero. If no trap occurs the result is either @math{+@infinity{}} or | |
590 | @math{-@infinity{}}, depending on the signs of the operands. | |
591 | ||
592 | @item Overflow | |
593 | This exception is raised whenever the result cannot be represented | |
594 | as a finite value in the precision format of the destination. If no trap | |
595 | occurs the result depends on the sign of the intermediate result and the | |
596 | current rounding mode (@w{IEEE 754}, @w{section 7.3}): | |
597 | @enumerate | |
598 | @item | |
599 | Round to nearest carries all overflows to @math{@infinity{}} | |
600 | with the sign of the intermediate result. | |
601 | @item | |
602 | Round toward @math{0} carries all overflows to the largest representable | |
603 | finite number with the sign of the intermediate result. | |
604 | @item | |
605 | Round toward @math{-@infinity{}} carries positive overflows to the | |
606 | largest representable finite number and negative overflows to | |
607 | @math{-@infinity{}}. | |
608 | ||
609 | @item | |
610 | Round toward @math{@infinity{}} carries negative overflows to the | |
611 | most negative representable finite number and positive overflows | |
612 | to @math{@infinity{}}. | |
613 | @end enumerate | |
614 | ||
615 | Whenever the overflow exception is raised, the inexact exception is also | |
616 | raised. | |
617 | ||
618 | @item Underflow | |
619 | The underflow exception is raised when an intermediate result is too | |
620 | small to be calculated accurately, or if the operation's result rounded | |
621 | to the destination precision is too small to be normalized. | |
622 | ||
623 | When no trap is installed for the underflow exception, underflow is | |
624 | signaled (via the underflow flag) only when both tininess and loss of | |
625 | accuracy have been detected. If no trap handler is installed the | |
626 | operation continues with an imprecise small value, or zero if the | |
627 | destination precision cannot hold the small exact result. | |
628 | ||
629 | @item Inexact | |
630 | This exception is signalled if a rounded result is not exact (such as | |
631 | when calculating the square root of two) or a result overflows without | |
632 | an overflow trap. | |
633 | @end table | |
634 | ||
635 | @node Infinity and NaN | |
636 | @subsection Infinity and NaN | |
637 | @cindex infinity | |
638 | @cindex not a number | |
639 | @cindex NaN | |
640 | ||
641 | @w{IEEE 754} floating point numbers can represent positive or negative | |
642 | infinity, and @dfn{NaN} (not a number). These three values arise from | |
643 | calculations whose result is undefined or cannot be represented | |
644 | accurately. You can also deliberately set a floating-point variable to | |
645 | any of them, which is sometimes useful. Some examples of calculations | |
646 | that produce infinity or NaN: | |
647 | ||
648 | @ifnottex | |
649 | @smallexample | |
650 | @math{1/0 = @infinity{}} | |
651 | @math{log (0) = -@infinity{}} | |
652 | @math{sqrt (-1) = NaN} | |
653 | @end smallexample | |
654 | @end ifnottex | |
655 | @tex | |
656 | $${1\over0} = \infty$$ | |
657 | $$\log 0 = -\infty$$ | |
658 | $$\sqrt{-1} = \hbox{NaN}$$ | |
659 | @end tex | |
660 | ||
661 | When a calculation produces any of these values, an exception also | |
662 | occurs; see @ref{FP Exceptions}. | |
663 | ||
664 | The basic operations and math functions all accept infinity and NaN and | |
665 | produce sensible output. Infinities propagate through calculations as | |
666 | one would expect: for example, @math{2 + @infinity{} = @infinity{}}, | |
667 | @math{4/@infinity{} = 0}, atan @math{(@infinity{}) = @pi{}/2}. NaN, on | |
668 | the other hand, infects any calculation that involves it. Unless the | |
669 | calculation would produce the same result no matter what real value | |
670 | replaced NaN, the result is NaN. | |
671 | ||
672 | In comparison operations, positive infinity is larger than all values | |
673 | except itself and NaN, and negative infinity is smaller than all values | |
674 | except itself and NaN. NaN is @dfn{unordered}: it is not equal to, | |
675 | greater than, or less than anything, @emph{including itself}. @code{x == | |
676 | x} is false if the value of @code{x} is NaN. You can use this to test | |
677 | whether a value is NaN or not, but the recommended way to test for NaN | |
678 | is with the @code{isnan} function (@pxref{Floating Point Classes}). In | |
679 | addition, @code{<}, @code{>}, @code{<=}, and @code{>=} will raise an | |
680 | exception when applied to NaNs. | |
681 | ||
682 | @file{math.h} defines macros that allow you to explicitly set a variable | |
683 | to infinity or NaN. | |
b4012b75 UD |
684 | |
685 | @comment math.h | |
686 | @comment ISO | |
7a68c94a UD |
687 | @deftypevr Macro float INFINITY |
688 | An expression representing positive infinity. It is equal to the value | |
689 | produced by mathematical operations like @code{1.0 / 0.0}. | |
690 | @code{-INFINITY} represents negative infinity. | |
691 | ||
692 | You can test whether a floating-point value is infinite by comparing it | |
693 | to this macro. However, this is not recommended; you should use the | |
694 | @code{isfinite} macro instead. @xref{Floating Point Classes}. | |
695 | ||
ec751a23 | 696 | This macro was introduced in the @w{ISO C99} standard. |
7a68c94a UD |
697 | @end deftypevr |
698 | ||
699 | @comment math.h | |
700 | @comment GNU | |
701 | @deftypevr Macro float NAN | |
702 | An expression representing a value which is ``not a number''. This | |
703 | macro is a GNU extension, available only on machines that support the | |
704 | ``not a number'' value---that is to say, on all machines that support | |
705 | IEEE floating point. | |
706 | ||
707 | You can use @samp{#ifdef NAN} to test whether the machine supports | |
708 | NaN. (Of course, you must arrange for GNU extensions to be visible, | |
709 | such as by defining @code{_GNU_SOURCE}, and then you must include | |
710 | @file{math.h}.) | |
711 | @end deftypevr | |
712 | ||
713 | @w{IEEE 754} also allows for another unusual value: negative zero. This | |
714 | value is produced when you divide a positive number by negative | |
715 | infinity, or when a negative result is smaller than the limits of | |
cd837b09 | 716 | representation. |
7a68c94a UD |
717 | |
718 | @node Status bit operations | |
719 | @subsection Examining the FPU status word | |
720 | ||
ec751a23 | 721 | @w{ISO C99} defines functions to query and manipulate the |
7a68c94a UD |
722 | floating-point status word. You can use these functions to check for |
723 | untrapped exceptions when it's convenient, rather than worrying about | |
724 | them in the middle of a calculation. | |
725 | ||
726 | These constants represent the various @w{IEEE 754} exceptions. Not all | |
727 | FPUs report all the different exceptions. Each constant is defined if | |
728 | and only if the FPU you are compiling for supports that exception, so | |
729 | you can test for FPU support with @samp{#ifdef}. They are defined in | |
730 | @file{fenv.h}. | |
b4012b75 UD |
731 | |
732 | @vtable @code | |
7a68c94a UD |
733 | @comment fenv.h |
734 | @comment ISO | |
735 | @item FE_INEXACT | |
736 | The inexact exception. | |
737 | @comment fenv.h | |
738 | @comment ISO | |
739 | @item FE_DIVBYZERO | |
740 | The divide by zero exception. | |
741 | @comment fenv.h | |
742 | @comment ISO | |
743 | @item FE_UNDERFLOW | |
744 | The underflow exception. | |
745 | @comment fenv.h | |
746 | @comment ISO | |
747 | @item FE_OVERFLOW | |
748 | The overflow exception. | |
749 | @comment fenv.h | |
750 | @comment ISO | |
751 | @item FE_INVALID | |
752 | The invalid exception. | |
b4012b75 UD |
753 | @end vtable |
754 | ||
7a68c94a UD |
755 | The macro @code{FE_ALL_EXCEPT} is the bitwise OR of all exception macros |
756 | which are supported by the FP implementation. | |
b4012b75 | 757 | |
7a68c94a UD |
758 | These functions allow you to clear exception flags, test for exceptions, |
759 | and save and restore the set of exceptions flagged. | |
b4012b75 | 760 | |
7a68c94a | 761 | @comment fenv.h |
b4012b75 | 762 | @comment ISO |
63ae7b63 | 763 | @deftypefun int feclearexcept (int @var{excepts}) |
b719dafd AO |
764 | @safety{@prelim{}@mtsafe{}@assafe{@assposix{}}@acsafe{@acsposix{}}} |
765 | @c The other functions in this section that modify FP status register | |
766 | @c mostly do so with non-atomic load-modify-store sequences, but since | |
767 | @c the register is thread-specific, this should be fine, and safe for | |
768 | @c cancellation. As long as the FP environment is restored before the | |
769 | @c signal handler returns control to the interrupted thread (like any | |
770 | @c kernel should do), the functions are also safe for use in signal | |
771 | @c handlers. | |
7a68c94a UD |
772 | This function clears all of the supported exception flags indicated by |
773 | @var{excepts}. | |
63ae7b63 UD |
774 | |
775 | The function returns zero in case the operation was successful, a | |
776 | non-zero value otherwise. | |
777 | @end deftypefun | |
778 | ||
779 | @comment fenv.h | |
780 | @comment ISO | |
781 | @deftypefun int feraiseexcept (int @var{excepts}) | |
b719dafd | 782 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
63ae7b63 UD |
783 | This function raises the supported exceptions indicated by |
784 | @var{excepts}. If more than one exception bit in @var{excepts} is set | |
785 | the order in which the exceptions are raised is undefined except that | |
786 | overflow (@code{FE_OVERFLOW}) or underflow (@code{FE_UNDERFLOW}) are | |
787 | raised before inexact (@code{FE_INEXACT}). Whether for overflow or | |
788 | underflow the inexact exception is also raised is also implementation | |
789 | dependent. | |
790 | ||
791 | The function returns zero in case the operation was successful, a | |
792 | non-zero value otherwise. | |
7a68c94a UD |
793 | @end deftypefun |
794 | ||
5146356f JM |
795 | @comment fenv.h |
796 | @comment ISO | |
797 | @deftypefun int fesetexcept (int @var{excepts}) | |
798 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} | |
799 | This function sets the supported exception flags indicated by | |
800 | @var{excepts}, like @code{feraiseexcept}, but without causing enabled | |
801 | traps to be taken. @code{fesetexcept} is from TS 18661-1:2014. | |
802 | ||
803 | The function returns zero in case the operation was successful, a | |
804 | non-zero value otherwise. | |
805 | @end deftypefun | |
806 | ||
7a68c94a UD |
807 | @comment fenv.h |
808 | @comment ISO | |
809 | @deftypefun int fetestexcept (int @var{excepts}) | |
b719dafd | 810 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
811 | Test whether the exception flags indicated by the parameter @var{except} |
812 | are currently set. If any of them are, a nonzero value is returned | |
813 | which specifies which exceptions are set. Otherwise the result is zero. | |
814 | @end deftypefun | |
815 | ||
816 | To understand these functions, imagine that the status word is an | |
817 | integer variable named @var{status}. @code{feclearexcept} is then | |
818 | equivalent to @samp{status &= ~excepts} and @code{fetestexcept} is | |
819 | equivalent to @samp{(status & excepts)}. The actual implementation may | |
820 | be very different, of course. | |
821 | ||
822 | Exception flags are only cleared when the program explicitly requests it, | |
823 | by calling @code{feclearexcept}. If you want to check for exceptions | |
824 | from a set of calculations, you should clear all the flags first. Here | |
825 | is a simple example of the way to use @code{fetestexcept}: | |
b4012b75 UD |
826 | |
827 | @smallexample | |
7a68c94a UD |
828 | @{ |
829 | double f; | |
830 | int raised; | |
831 | feclearexcept (FE_ALL_EXCEPT); | |
832 | f = compute (); | |
833 | raised = fetestexcept (FE_OVERFLOW | FE_INVALID); | |
95fdc6a0 UD |
834 | if (raised & FE_OVERFLOW) @{ /* @dots{} */ @} |
835 | if (raised & FE_INVALID) @{ /* @dots{} */ @} | |
836 | /* @dots{} */ | |
7a68c94a | 837 | @} |
b4012b75 UD |
838 | @end smallexample |
839 | ||
7a68c94a UD |
840 | You cannot explicitly set bits in the status word. You can, however, |
841 | save the entire status word and restore it later. This is done with the | |
842 | following functions: | |
b4012b75 | 843 | |
7a68c94a | 844 | @comment fenv.h |
b4012b75 | 845 | @comment ISO |
63ae7b63 | 846 | @deftypefun int fegetexceptflag (fexcept_t *@var{flagp}, int @var{excepts}) |
b719dafd | 847 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
848 | This function stores in the variable pointed to by @var{flagp} an |
849 | implementation-defined value representing the current setting of the | |
850 | exception flags indicated by @var{excepts}. | |
63ae7b63 UD |
851 | |
852 | The function returns zero in case the operation was successful, a | |
853 | non-zero value otherwise. | |
7a68c94a | 854 | @end deftypefun |
b4012b75 | 855 | |
7a68c94a UD |
856 | @comment fenv.h |
857 | @comment ISO | |
9251c568 | 858 | @deftypefun int fesetexceptflag (const fexcept_t *@var{flagp}, int @var{excepts}) |
b719dafd | 859 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
860 | This function restores the flags for the exceptions indicated by |
861 | @var{excepts} to the values stored in the variable pointed to by | |
862 | @var{flagp}. | |
63ae7b63 UD |
863 | |
864 | The function returns zero in case the operation was successful, a | |
865 | non-zero value otherwise. | |
7a68c94a UD |
866 | @end deftypefun |
867 | ||
868 | Note that the value stored in @code{fexcept_t} bears no resemblance to | |
869 | the bit mask returned by @code{fetestexcept}. The type may not even be | |
870 | an integer. Do not attempt to modify an @code{fexcept_t} variable. | |
871 | ||
780257d4 JM |
872 | @comment fenv.h |
873 | @comment ISO | |
874 | @deftypefun int fetestexceptflag (const fexcept_t *@var{flagp}, int @var{excepts}) | |
875 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} | |
876 | Test whether the exception flags indicated by the parameter | |
877 | @var{excepts} are set in the variable pointed to by @var{flagp}. If | |
878 | any of them are, a nonzero value is returned which specifies which | |
879 | exceptions are set. Otherwise the result is zero. | |
880 | @code{fetestexceptflag} is from TS 18661-1:2014. | |
881 | @end deftypefun | |
882 | ||
7a68c94a UD |
883 | @node Math Error Reporting |
884 | @subsection Error Reporting by Mathematical Functions | |
885 | @cindex errors, mathematical | |
886 | @cindex domain error | |
887 | @cindex range error | |
888 | ||
889 | Many of the math functions are defined only over a subset of the real or | |
890 | complex numbers. Even if they are mathematically defined, their result | |
891 | may be larger or smaller than the range representable by their return | |
c5df7609 JM |
892 | type without loss of accuracy. These are known as @dfn{domain errors}, |
893 | @dfn{overflows}, and | |
7a68c94a UD |
894 | @dfn{underflows}, respectively. Math functions do several things when |
895 | one of these errors occurs. In this manual we will refer to the | |
896 | complete response as @dfn{signalling} a domain error, overflow, or | |
897 | underflow. | |
898 | ||
899 | When a math function suffers a domain error, it raises the invalid | |
900 | exception and returns NaN. It also sets @var{errno} to @code{EDOM}; | |
901 | this is for compatibility with old systems that do not support @w{IEEE | |
902 | 754} exception handling. Likewise, when overflow occurs, math | |
c5df7609 JM |
903 | functions raise the overflow exception and, in the default rounding |
904 | mode, return @math{@infinity{}} or @math{-@infinity{}} as appropriate | |
905 | (in other rounding modes, the largest finite value of the appropriate | |
906 | sign is returned when appropriate for that rounding mode). They also | |
907 | set @var{errno} to @code{ERANGE} if returning @math{@infinity{}} or | |
908 | @math{-@infinity{}}; @var{errno} may or may not be set to | |
909 | @code{ERANGE} when a finite value is returned on overflow. When | |
910 | underflow occurs, the underflow exception is raised, and zero | |
911 | (appropriately signed) or a subnormal value, as appropriate for the | |
912 | mathematical result of the function and the rounding mode, is | |
913 | returned. @var{errno} may be set to @code{ERANGE}, but this is not | |
914 | guaranteed; it is intended that @theglibc{} should set it when the | |
915 | underflow is to an appropriately signed zero, but not necessarily for | |
916 | other underflows. | |
7a68c94a UD |
917 | |
918 | Some of the math functions are defined mathematically to result in a | |
919 | complex value over parts of their domains. The most familiar example of | |
920 | this is taking the square root of a negative number. The complex math | |
921 | functions, such as @code{csqrt}, will return the appropriate complex value | |
922 | in this case. The real-valued functions, such as @code{sqrt}, will | |
923 | signal a domain error. | |
924 | ||
925 | Some older hardware does not support infinities. On that hardware, | |
926 | overflows instead return a particular very large number (usually the | |
927 | largest representable number). @file{math.h} defines macros you can use | |
928 | to test for overflow on both old and new hardware. | |
b4012b75 UD |
929 | |
930 | @comment math.h | |
931 | @comment ISO | |
7a68c94a | 932 | @deftypevr Macro double HUGE_VAL |
4260bc74 UD |
933 | @comment math.h |
934 | @comment ISO | |
7a68c94a | 935 | @deftypevrx Macro float HUGE_VALF |
4260bc74 UD |
936 | @comment math.h |
937 | @comment ISO | |
7a68c94a UD |
938 | @deftypevrx Macro {long double} HUGE_VALL |
939 | An expression representing a particular very large number. On machines | |
940 | that use @w{IEEE 754} floating point format, @code{HUGE_VAL} is infinity. | |
941 | On other machines, it's typically the largest positive number that can | |
942 | be represented. | |
943 | ||
944 | Mathematical functions return the appropriately typed version of | |
945 | @code{HUGE_VAL} or @code{@minus{}HUGE_VAL} when the result is too large | |
946 | to be represented. | |
947 | @end deftypevr | |
b4012b75 | 948 | |
7a68c94a UD |
949 | @node Rounding |
950 | @section Rounding Modes | |
951 | ||
952 | Floating-point calculations are carried out internally with extra | |
953 | precision, and then rounded to fit into the destination type. This | |
954 | ensures that results are as precise as the input data. @w{IEEE 754} | |
955 | defines four possible rounding modes: | |
956 | ||
957 | @table @asis | |
958 | @item Round to nearest. | |
959 | This is the default mode. It should be used unless there is a specific | |
960 | need for one of the others. In this mode results are rounded to the | |
961 | nearest representable value. If the result is midway between two | |
962 | representable values, the even representable is chosen. @dfn{Even} here | |
963 | means the lowest-order bit is zero. This rounding mode prevents | |
964 | statistical bias and guarantees numeric stability: round-off errors in a | |
965 | lengthy calculation will remain smaller than half of @code{FLT_EPSILON}. | |
966 | ||
967 | @c @item Round toward @math{+@infinity{}} | |
968 | @item Round toward plus Infinity. | |
969 | All results are rounded to the smallest representable value | |
970 | which is greater than the result. | |
971 | ||
972 | @c @item Round toward @math{-@infinity{}} | |
973 | @item Round toward minus Infinity. | |
974 | All results are rounded to the largest representable value which is less | |
975 | than the result. | |
976 | ||
977 | @item Round toward zero. | |
978 | All results are rounded to the largest representable value whose | |
979 | magnitude is less than that of the result. In other words, if the | |
980 | result is negative it is rounded up; if it is positive, it is rounded | |
981 | down. | |
982 | @end table | |
b4012b75 | 983 | |
7a68c94a UD |
984 | @noindent |
985 | @file{fenv.h} defines constants which you can use to refer to the | |
986 | various rounding modes. Each one will be defined if and only if the FPU | |
987 | supports the corresponding rounding mode. | |
b4012b75 | 988 | |
7a68c94a UD |
989 | @table @code |
990 | @comment fenv.h | |
991 | @comment ISO | |
992 | @vindex FE_TONEAREST | |
993 | @item FE_TONEAREST | |
994 | Round to nearest. | |
b4012b75 | 995 | |
7a68c94a UD |
996 | @comment fenv.h |
997 | @comment ISO | |
998 | @vindex FE_UPWARD | |
999 | @item FE_UPWARD | |
1000 | Round toward @math{+@infinity{}}. | |
b4012b75 | 1001 | |
7a68c94a UD |
1002 | @comment fenv.h |
1003 | @comment ISO | |
1004 | @vindex FE_DOWNWARD | |
1005 | @item FE_DOWNWARD | |
1006 | Round toward @math{-@infinity{}}. | |
b4012b75 | 1007 | |
7a68c94a UD |
1008 | @comment fenv.h |
1009 | @comment ISO | |
1010 | @vindex FE_TOWARDZERO | |
1011 | @item FE_TOWARDZERO | |
1012 | Round toward zero. | |
1013 | @end table | |
b4012b75 | 1014 | |
7a68c94a UD |
1015 | Underflow is an unusual case. Normally, @w{IEEE 754} floating point |
1016 | numbers are always normalized (@pxref{Floating Point Concepts}). | |
1017 | Numbers smaller than @math{2^r} (where @math{r} is the minimum exponent, | |
1018 | @code{FLT_MIN_RADIX-1} for @var{float}) cannot be represented as | |
1019 | normalized numbers. Rounding all such numbers to zero or @math{2^r} | |
1020 | would cause some algorithms to fail at 0. Therefore, they are left in | |
1021 | denormalized form. That produces loss of precision, since some bits of | |
1022 | the mantissa are stolen to indicate the decimal point. | |
1023 | ||
1024 | If a result is too small to be represented as a denormalized number, it | |
1025 | is rounded to zero. However, the sign of the result is preserved; if | |
1026 | the calculation was negative, the result is @dfn{negative zero}. | |
1027 | Negative zero can also result from some operations on infinity, such as | |
cd837b09 | 1028 | @math{4/-@infinity{}}. |
7a68c94a UD |
1029 | |
1030 | At any time one of the above four rounding modes is selected. You can | |
1031 | find out which one with this function: | |
1032 | ||
1033 | @comment fenv.h | |
1034 | @comment ISO | |
1035 | @deftypefun int fegetround (void) | |
b719dafd | 1036 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
1037 | Returns the currently selected rounding mode, represented by one of the |
1038 | values of the defined rounding mode macros. | |
1039 | @end deftypefun | |
b4012b75 | 1040 | |
7a68c94a UD |
1041 | @noindent |
1042 | To change the rounding mode, use this function: | |
b4012b75 | 1043 | |
7a68c94a UD |
1044 | @comment fenv.h |
1045 | @comment ISO | |
1046 | @deftypefun int fesetround (int @var{round}) | |
b719dafd | 1047 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
1048 | Changes the currently selected rounding mode to @var{round}. If |
1049 | @var{round} does not correspond to one of the supported rounding modes | |
d5655997 UD |
1050 | nothing is changed. @code{fesetround} returns zero if it changed the |
1051 | rounding mode, a nonzero value if the mode is not supported. | |
7a68c94a | 1052 | @end deftypefun |
b4012b75 | 1053 | |
7a68c94a UD |
1054 | You should avoid changing the rounding mode if possible. It can be an |
1055 | expensive operation; also, some hardware requires you to compile your | |
1056 | program differently for it to work. The resulting code may run slower. | |
1057 | See your compiler documentation for details. | |
1058 | @c This section used to claim that functions existed to round one number | |
1059 | @c in a specific fashion. I can't find any functions in the library | |
1060 | @c that do that. -zw | |
1061 | ||
1062 | @node Control Functions | |
1063 | @section Floating-Point Control Functions | |
1064 | ||
1065 | @w{IEEE 754} floating-point implementations allow the programmer to | |
1066 | decide whether traps will occur for each of the exceptions, by setting | |
1067 | bits in the @dfn{control word}. In C, traps result in the program | |
1068 | receiving the @code{SIGFPE} signal; see @ref{Signal Handling}. | |
1069 | ||
48b22986 | 1070 | @strong{NB:} @w{IEEE 754} says that trap handlers are given details of |
7a68c94a UD |
1071 | the exceptional situation, and can set the result value. C signals do |
1072 | not provide any mechanism to pass this information back and forth. | |
1073 | Trapping exceptions in C is therefore not very useful. | |
1074 | ||
1075 | It is sometimes necessary to save the state of the floating-point unit | |
1076 | while you perform some calculation. The library provides functions | |
1077 | which save and restore the exception flags, the set of exceptions that | |
1078 | generate traps, and the rounding mode. This information is known as the | |
1079 | @dfn{floating-point environment}. | |
1080 | ||
1081 | The functions to save and restore the floating-point environment all use | |
1082 | a variable of type @code{fenv_t} to store information. This type is | |
1083 | defined in @file{fenv.h}. Its size and contents are | |
1084 | implementation-defined. You should not attempt to manipulate a variable | |
1085 | of this type directly. | |
1086 | ||
1087 | To save the state of the FPU, use one of these functions: | |
1088 | ||
1089 | @comment fenv.h | |
b4012b75 | 1090 | @comment ISO |
63ae7b63 | 1091 | @deftypefun int fegetenv (fenv_t *@var{envp}) |
b719dafd | 1092 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
1093 | Store the floating-point environment in the variable pointed to by |
1094 | @var{envp}. | |
63ae7b63 UD |
1095 | |
1096 | The function returns zero in case the operation was successful, a | |
1097 | non-zero value otherwise. | |
b4012b75 UD |
1098 | @end deftypefun |
1099 | ||
7a68c94a | 1100 | @comment fenv.h |
b4012b75 | 1101 | @comment ISO |
7a68c94a | 1102 | @deftypefun int feholdexcept (fenv_t *@var{envp}) |
b719dafd | 1103 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
1104 | Store the current floating-point environment in the object pointed to by |
1105 | @var{envp}. Then clear all exception flags, and set the FPU to trap no | |
1106 | exceptions. Not all FPUs support trapping no exceptions; if | |
0f6b172f UD |
1107 | @code{feholdexcept} cannot set this mode, it returns nonzero value. If it |
1108 | succeeds, it returns zero. | |
b4012b75 UD |
1109 | @end deftypefun |
1110 | ||
7a7a7ee5 | 1111 | The functions which restore the floating-point environment can take these |
7a68c94a | 1112 | kinds of arguments: |
b4012b75 | 1113 | |
7a68c94a UD |
1114 | @itemize @bullet |
1115 | @item | |
1116 | Pointers to @code{fenv_t} objects, which were initialized previously by a | |
1117 | call to @code{fegetenv} or @code{feholdexcept}. | |
1118 | @item | |
1119 | @vindex FE_DFL_ENV | |
1120 | The special macro @code{FE_DFL_ENV} which represents the floating-point | |
1121 | environment as it was available at program start. | |
1122 | @item | |
7a7a7ee5 AJ |
1123 | Implementation defined macros with names starting with @code{FE_} and |
1124 | having type @code{fenv_t *}. | |
b4012b75 | 1125 | |
7a68c94a | 1126 | @vindex FE_NOMASK_ENV |
1f77f049 | 1127 | If possible, @theglibc{} defines a macro @code{FE_NOMASK_ENV} |
7a68c94a UD |
1128 | which represents an environment where every exception raised causes a |
1129 | trap to occur. You can test for this macro using @code{#ifdef}. It is | |
1130 | only defined if @code{_GNU_SOURCE} is defined. | |
1131 | ||
1132 | Some platforms might define other predefined environments. | |
1133 | @end itemize | |
1134 | ||
1135 | @noindent | |
1136 | To set the floating-point environment, you can use either of these | |
1137 | functions: | |
1138 | ||
1139 | @comment fenv.h | |
b4012b75 | 1140 | @comment ISO |
63ae7b63 | 1141 | @deftypefun int fesetenv (const fenv_t *@var{envp}) |
b719dafd | 1142 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a | 1143 | Set the floating-point environment to that described by @var{envp}. |
63ae7b63 UD |
1144 | |
1145 | The function returns zero in case the operation was successful, a | |
1146 | non-zero value otherwise. | |
b4012b75 UD |
1147 | @end deftypefun |
1148 | ||
7a68c94a | 1149 | @comment fenv.h |
b4012b75 | 1150 | @comment ISO |
63ae7b63 | 1151 | @deftypefun int feupdateenv (const fenv_t *@var{envp}) |
b719dafd | 1152 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
1153 | Like @code{fesetenv}, this function sets the floating-point environment |
1154 | to that described by @var{envp}. However, if any exceptions were | |
1155 | flagged in the status word before @code{feupdateenv} was called, they | |
1156 | remain flagged after the call. In other words, after @code{feupdateenv} | |
1157 | is called, the status word is the bitwise OR of the previous status word | |
1158 | and the one saved in @var{envp}. | |
63ae7b63 UD |
1159 | |
1160 | The function returns zero in case the operation was successful, a | |
1161 | non-zero value otherwise. | |
b4012b75 UD |
1162 | @end deftypefun |
1163 | ||
ec94343f JM |
1164 | @noindent |
1165 | TS 18661-1:2014 defines additional functions to save and restore | |
1166 | floating-point control modes (such as the rounding mode and whether | |
1167 | traps are enabled) while leaving other status (such as raised flags) | |
1168 | unchanged. | |
1169 | ||
1170 | @vindex FE_DFL_MODE | |
1171 | The special macro @code{FE_DFL_MODE} may be passed to | |
1172 | @code{fesetmode}. It represents the floating-point control modes at | |
1173 | program start. | |
1174 | ||
1175 | @comment fenv.h | |
1176 | @comment ISO | |
1177 | @deftypefun int fegetmode (femode_t *@var{modep}) | |
1178 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} | |
1179 | Store the floating-point control modes in the variable pointed to by | |
1180 | @var{modep}. | |
1181 | ||
1182 | The function returns zero in case the operation was successful, a | |
1183 | non-zero value otherwise. | |
1184 | @end deftypefun | |
1185 | ||
1186 | @comment fenv.h | |
1187 | @comment ISO | |
1188 | @deftypefun int fesetmode (const femode_t *@var{modep}) | |
1189 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} | |
1190 | Set the floating-point control modes to those described by | |
1191 | @var{modep}. | |
1192 | ||
1193 | The function returns zero in case the operation was successful, a | |
1194 | non-zero value otherwise. | |
1195 | @end deftypefun | |
1196 | ||
05ef7ce9 UD |
1197 | @noindent |
1198 | To control for individual exceptions if raising them causes a trap to | |
1199 | occur, you can use the following two functions. | |
1200 | ||
1201 | @strong{Portability Note:} These functions are all GNU extensions. | |
1202 | ||
1203 | @comment fenv.h | |
1204 | @comment GNU | |
1205 | @deftypefun int feenableexcept (int @var{excepts}) | |
b719dafd | 1206 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
05ef7ce9 | 1207 | This functions enables traps for each of the exceptions as indicated by |
f638872a | 1208 | the parameter @var{except}. The individual exceptions are described in |
6e8afc1c | 1209 | @ref{Status bit operations}. Only the specified exceptions are |
05ef7ce9 UD |
1210 | enabled, the status of the other exceptions is not changed. |
1211 | ||
1212 | The function returns the previous enabled exceptions in case the | |
1213 | operation was successful, @code{-1} otherwise. | |
1214 | @end deftypefun | |
1215 | ||
1216 | @comment fenv.h | |
1217 | @comment GNU | |
1218 | @deftypefun int fedisableexcept (int @var{excepts}) | |
b719dafd | 1219 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
05ef7ce9 | 1220 | This functions disables traps for each of the exceptions as indicated by |
f638872a | 1221 | the parameter @var{except}. The individual exceptions are described in |
6e8afc1c | 1222 | @ref{Status bit operations}. Only the specified exceptions are |
05ef7ce9 UD |
1223 | disabled, the status of the other exceptions is not changed. |
1224 | ||
1225 | The function returns the previous enabled exceptions in case the | |
1226 | operation was successful, @code{-1} otherwise. | |
1227 | @end deftypefun | |
1228 | ||
1229 | @comment fenv.h | |
1230 | @comment GNU | |
8ded91fb | 1231 | @deftypefun int fegetexcept (void) |
b719dafd | 1232 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
05ef7ce9 UD |
1233 | The function returns a bitmask of all currently enabled exceptions. It |
1234 | returns @code{-1} in case of failure. | |
6e8afc1c | 1235 | @end deftypefun |
05ef7ce9 | 1236 | |
7a68c94a UD |
1237 | @node Arithmetic Functions |
1238 | @section Arithmetic Functions | |
b4012b75 | 1239 | |
7a68c94a UD |
1240 | The C library provides functions to do basic operations on |
1241 | floating-point numbers. These include absolute value, maximum and minimum, | |
1242 | normalization, bit twiddling, rounding, and a few others. | |
b4012b75 | 1243 | |
7a68c94a UD |
1244 | @menu |
1245 | * Absolute Value:: Absolute values of integers and floats. | |
1246 | * Normalization Functions:: Extracting exponents and putting them back. | |
1247 | * Rounding Functions:: Rounding floats to integers. | |
1248 | * Remainder Functions:: Remainders on division, precisely defined. | |
1249 | * FP Bit Twiddling:: Sign bit adjustment. Adding epsilon. | |
1250 | * FP Comparison Functions:: Comparisons without risk of exceptions. | |
1251 | * Misc FP Arithmetic:: Max, min, positive difference, multiply-add. | |
1252 | @end menu | |
b4012b75 | 1253 | |
28f540f4 | 1254 | @node Absolute Value |
7a68c94a | 1255 | @subsection Absolute Value |
28f540f4 RM |
1256 | @cindex absolute value functions |
1257 | ||
1258 | These functions are provided for obtaining the @dfn{absolute value} (or | |
1259 | @dfn{magnitude}) of a number. The absolute value of a real number | |
2d26e9eb | 1260 | @var{x} is @var{x} if @var{x} is positive, @minus{}@var{x} if @var{x} is |
28f540f4 RM |
1261 | negative. For a complex number @var{z}, whose real part is @var{x} and |
1262 | whose imaginary part is @var{y}, the absolute value is @w{@code{sqrt | |
1263 | (@var{x}*@var{x} + @var{y}*@var{y})}}. | |
1264 | ||
1265 | @pindex math.h | |
1266 | @pindex stdlib.h | |
fe0ec73e | 1267 | Prototypes for @code{abs}, @code{labs} and @code{llabs} are in @file{stdlib.h}; |
e518937a | 1268 | @code{imaxabs} is declared in @file{inttypes.h}; |
7a68c94a | 1269 | @code{fabs}, @code{fabsf} and @code{fabsl} are declared in @file{math.h}. |
b4012b75 | 1270 | @code{cabs}, @code{cabsf} and @code{cabsl} are declared in @file{complex.h}. |
28f540f4 RM |
1271 | |
1272 | @comment stdlib.h | |
f65fd747 | 1273 | @comment ISO |
28f540f4 | 1274 | @deftypefun int abs (int @var{number}) |
4260bc74 UD |
1275 | @comment stdlib.h |
1276 | @comment ISO | |
7a68c94a | 1277 | @deftypefunx {long int} labs (long int @var{number}) |
4260bc74 UD |
1278 | @comment stdlib.h |
1279 | @comment ISO | |
7a68c94a | 1280 | @deftypefunx {long long int} llabs (long long int @var{number}) |
e518937a UD |
1281 | @comment inttypes.h |
1282 | @comment ISO | |
1283 | @deftypefunx intmax_t imaxabs (intmax_t @var{number}) | |
b719dafd | 1284 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a | 1285 | These functions return the absolute value of @var{number}. |
28f540f4 RM |
1286 | |
1287 | Most computers use a two's complement integer representation, in which | |
1288 | the absolute value of @code{INT_MIN} (the smallest possible @code{int}) | |
1289 | cannot be represented; thus, @w{@code{abs (INT_MIN)}} is not defined. | |
28f540f4 | 1290 | |
ec751a23 | 1291 | @code{llabs} and @code{imaxdiv} are new to @w{ISO C99}. |
0e4ee106 UD |
1292 | |
1293 | See @ref{Integers} for a description of the @code{intmax_t} type. | |
1294 | ||
fe0ec73e UD |
1295 | @end deftypefun |
1296 | ||
28f540f4 | 1297 | @comment math.h |
f65fd747 | 1298 | @comment ISO |
28f540f4 | 1299 | @deftypefun double fabs (double @var{number}) |
4260bc74 UD |
1300 | @comment math.h |
1301 | @comment ISO | |
779ae82e | 1302 | @deftypefunx float fabsf (float @var{number}) |
4260bc74 UD |
1303 | @comment math.h |
1304 | @comment ISO | |
779ae82e | 1305 | @deftypefunx {long double} fabsl (long double @var{number}) |
b719dafd | 1306 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
28f540f4 RM |
1307 | This function returns the absolute value of the floating-point number |
1308 | @var{number}. | |
1309 | @end deftypefun | |
1310 | ||
b4012b75 UD |
1311 | @comment complex.h |
1312 | @comment ISO | |
1313 | @deftypefun double cabs (complex double @var{z}) | |
4260bc74 UD |
1314 | @comment complex.h |
1315 | @comment ISO | |
779ae82e | 1316 | @deftypefunx float cabsf (complex float @var{z}) |
4260bc74 UD |
1317 | @comment complex.h |
1318 | @comment ISO | |
779ae82e | 1319 | @deftypefunx {long double} cabsl (complex long double @var{z}) |
b719dafd | 1320 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
1321 | These functions return the absolute value of the complex number @var{z} |
1322 | (@pxref{Complex Numbers}). The absolute value of a complex number is: | |
28f540f4 RM |
1323 | |
1324 | @smallexample | |
b4012b75 | 1325 | sqrt (creal (@var{z}) * creal (@var{z}) + cimag (@var{z}) * cimag (@var{z})) |
28f540f4 | 1326 | @end smallexample |
dfd2257a | 1327 | |
7a68c94a UD |
1328 | This function should always be used instead of the direct formula |
1329 | because it takes special care to avoid losing precision. It may also | |
cf822e3c | 1330 | take advantage of hardware support for this operation. See @code{hypot} |
8b7fb588 | 1331 | in @ref{Exponents and Logarithms}. |
28f540f4 RM |
1332 | @end deftypefun |
1333 | ||
1334 | @node Normalization Functions | |
7a68c94a | 1335 | @subsection Normalization Functions |
28f540f4 RM |
1336 | @cindex normalization functions (floating-point) |
1337 | ||
1338 | The functions described in this section are primarily provided as a way | |
1339 | to efficiently perform certain low-level manipulations on floating point | |
1340 | numbers that are represented internally using a binary radix; | |
1341 | see @ref{Floating Point Concepts}. These functions are required to | |
1342 | have equivalent behavior even if the representation does not use a radix | |
1343 | of 2, but of course they are unlikely to be particularly efficient in | |
1344 | those cases. | |
1345 | ||
1346 | @pindex math.h | |
1347 | All these functions are declared in @file{math.h}. | |
1348 | ||
1349 | @comment math.h | |
f65fd747 | 1350 | @comment ISO |
28f540f4 | 1351 | @deftypefun double frexp (double @var{value}, int *@var{exponent}) |
4260bc74 UD |
1352 | @comment math.h |
1353 | @comment ISO | |
779ae82e | 1354 | @deftypefunx float frexpf (float @var{value}, int *@var{exponent}) |
4260bc74 UD |
1355 | @comment math.h |
1356 | @comment ISO | |
779ae82e | 1357 | @deftypefunx {long double} frexpl (long double @var{value}, int *@var{exponent}) |
b719dafd | 1358 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
b4012b75 | 1359 | These functions are used to split the number @var{value} |
28f540f4 RM |
1360 | into a normalized fraction and an exponent. |
1361 | ||
1362 | If the argument @var{value} is not zero, the return value is @var{value} | |
56b672e9 BN |
1363 | times a power of two, and its magnitude is always in the range 1/2 |
1364 | (inclusive) to 1 (exclusive). The corresponding exponent is stored in | |
28f540f4 RM |
1365 | @code{*@var{exponent}}; the return value multiplied by 2 raised to this |
1366 | exponent equals the original number @var{value}. | |
1367 | ||
1368 | For example, @code{frexp (12.8, &exponent)} returns @code{0.8} and | |
1369 | stores @code{4} in @code{exponent}. | |
1370 | ||
1371 | If @var{value} is zero, then the return value is zero and | |
1372 | zero is stored in @code{*@var{exponent}}. | |
1373 | @end deftypefun | |
1374 | ||
1375 | @comment math.h | |
f65fd747 | 1376 | @comment ISO |
28f540f4 | 1377 | @deftypefun double ldexp (double @var{value}, int @var{exponent}) |
4260bc74 UD |
1378 | @comment math.h |
1379 | @comment ISO | |
779ae82e | 1380 | @deftypefunx float ldexpf (float @var{value}, int @var{exponent}) |
4260bc74 UD |
1381 | @comment math.h |
1382 | @comment ISO | |
779ae82e | 1383 | @deftypefunx {long double} ldexpl (long double @var{value}, int @var{exponent}) |
b719dafd | 1384 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
b4012b75 | 1385 | These functions return the result of multiplying the floating-point |
28f540f4 RM |
1386 | number @var{value} by 2 raised to the power @var{exponent}. (It can |
1387 | be used to reassemble floating-point numbers that were taken apart | |
1388 | by @code{frexp}.) | |
1389 | ||
1390 | For example, @code{ldexp (0.8, 4)} returns @code{12.8}. | |
1391 | @end deftypefun | |
1392 | ||
7a68c94a | 1393 | The following functions, which come from BSD, provide facilities |
b7d03293 UD |
1394 | equivalent to those of @code{ldexp} and @code{frexp}. See also the |
1395 | @w{ISO C} function @code{logb} which originally also appeared in BSD. | |
7a68c94a UD |
1396 | |
1397 | @comment math.h | |
1398 | @comment BSD | |
8ded91fb | 1399 | @deftypefun double scalb (double @var{value}, double @var{exponent}) |
4260bc74 UD |
1400 | @comment math.h |
1401 | @comment BSD | |
8ded91fb | 1402 | @deftypefunx float scalbf (float @var{value}, float @var{exponent}) |
4260bc74 UD |
1403 | @comment math.h |
1404 | @comment BSD | |
8ded91fb | 1405 | @deftypefunx {long double} scalbl (long double @var{value}, long double @var{exponent}) |
b719dafd | 1406 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
1407 | The @code{scalb} function is the BSD name for @code{ldexp}. |
1408 | @end deftypefun | |
1409 | ||
1410 | @comment math.h | |
1411 | @comment BSD | |
9ad027fb | 1412 | @deftypefun double scalbn (double @var{x}, int @var{n}) |
4260bc74 UD |
1413 | @comment math.h |
1414 | @comment BSD | |
9ad027fb | 1415 | @deftypefunx float scalbnf (float @var{x}, int @var{n}) |
4260bc74 UD |
1416 | @comment math.h |
1417 | @comment BSD | |
9ad027fb | 1418 | @deftypefunx {long double} scalbnl (long double @var{x}, int @var{n}) |
b719dafd | 1419 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
1420 | @code{scalbn} is identical to @code{scalb}, except that the exponent |
1421 | @var{n} is an @code{int} instead of a floating-point number. | |
1422 | @end deftypefun | |
1423 | ||
1424 | @comment math.h | |
1425 | @comment BSD | |
9ad027fb | 1426 | @deftypefun double scalbln (double @var{x}, long int @var{n}) |
4260bc74 UD |
1427 | @comment math.h |
1428 | @comment BSD | |
9ad027fb | 1429 | @deftypefunx float scalblnf (float @var{x}, long int @var{n}) |
4260bc74 UD |
1430 | @comment math.h |
1431 | @comment BSD | |
9ad027fb | 1432 | @deftypefunx {long double} scalblnl (long double @var{x}, long int @var{n}) |
b719dafd | 1433 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
1434 | @code{scalbln} is identical to @code{scalb}, except that the exponent |
1435 | @var{n} is a @code{long int} instead of a floating-point number. | |
1436 | @end deftypefun | |
28f540f4 | 1437 | |
7a68c94a UD |
1438 | @comment math.h |
1439 | @comment BSD | |
8ded91fb | 1440 | @deftypefun double significand (double @var{x}) |
4260bc74 UD |
1441 | @comment math.h |
1442 | @comment BSD | |
8ded91fb | 1443 | @deftypefunx float significandf (float @var{x}) |
4260bc74 UD |
1444 | @comment math.h |
1445 | @comment BSD | |
8ded91fb | 1446 | @deftypefunx {long double} significandl (long double @var{x}) |
b719dafd | 1447 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
1448 | @code{significand} returns the mantissa of @var{x} scaled to the range |
1449 | @math{[1, 2)}. | |
1450 | It is equivalent to @w{@code{scalb (@var{x}, (double) -ilogb (@var{x}))}}. | |
1451 | ||
1452 | This function exists mainly for use in certain standardized tests | |
1453 | of @w{IEEE 754} conformance. | |
28f540f4 RM |
1454 | @end deftypefun |
1455 | ||
7a68c94a UD |
1456 | @node Rounding Functions |
1457 | @subsection Rounding Functions | |
28f540f4 RM |
1458 | @cindex converting floats to integers |
1459 | ||
1460 | @pindex math.h | |
7a68c94a | 1461 | The functions listed here perform operations such as rounding and |
cf822e3c | 1462 | truncation of floating-point values. Some of these functions convert |
7a68c94a UD |
1463 | floating point numbers to integer values. They are all declared in |
1464 | @file{math.h}. | |
28f540f4 RM |
1465 | |
1466 | You can also convert floating-point numbers to integers simply by | |
1467 | casting them to @code{int}. This discards the fractional part, | |
1468 | effectively rounding towards zero. However, this only works if the | |
1469 | result can actually be represented as an @code{int}---for very large | |
1470 | numbers, this is impossible. The functions listed here return the | |
1471 | result as a @code{double} instead to get around this problem. | |
1472 | ||
1473 | @comment math.h | |
f65fd747 | 1474 | @comment ISO |
28f540f4 | 1475 | @deftypefun double ceil (double @var{x}) |
4260bc74 UD |
1476 | @comment math.h |
1477 | @comment ISO | |
779ae82e | 1478 | @deftypefunx float ceilf (float @var{x}) |
4260bc74 UD |
1479 | @comment math.h |
1480 | @comment ISO | |
779ae82e | 1481 | @deftypefunx {long double} ceill (long double @var{x}) |
b719dafd | 1482 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
b4012b75 | 1483 | These functions round @var{x} upwards to the nearest integer, |
28f540f4 RM |
1484 | returning that value as a @code{double}. Thus, @code{ceil (1.5)} |
1485 | is @code{2.0}. | |
1486 | @end deftypefun | |
1487 | ||
1488 | @comment math.h | |
f65fd747 | 1489 | @comment ISO |
28f540f4 | 1490 | @deftypefun double floor (double @var{x}) |
4260bc74 UD |
1491 | @comment math.h |
1492 | @comment ISO | |
779ae82e | 1493 | @deftypefunx float floorf (float @var{x}) |
4260bc74 UD |
1494 | @comment math.h |
1495 | @comment ISO | |
779ae82e | 1496 | @deftypefunx {long double} floorl (long double @var{x}) |
b719dafd | 1497 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
b4012b75 | 1498 | These functions round @var{x} downwards to the nearest |
28f540f4 RM |
1499 | integer, returning that value as a @code{double}. Thus, @code{floor |
1500 | (1.5)} is @code{1.0} and @code{floor (-1.5)} is @code{-2.0}. | |
1501 | @end deftypefun | |
1502 | ||
7a68c94a UD |
1503 | @comment math.h |
1504 | @comment ISO | |
1505 | @deftypefun double trunc (double @var{x}) | |
4260bc74 UD |
1506 | @comment math.h |
1507 | @comment ISO | |
7a68c94a | 1508 | @deftypefunx float truncf (float @var{x}) |
4260bc74 UD |
1509 | @comment math.h |
1510 | @comment ISO | |
7a68c94a | 1511 | @deftypefunx {long double} truncl (long double @var{x}) |
b719dafd | 1512 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
e6e81391 UD |
1513 | The @code{trunc} functions round @var{x} towards zero to the nearest |
1514 | integer (returned in floating-point format). Thus, @code{trunc (1.5)} | |
1515 | is @code{1.0} and @code{trunc (-1.5)} is @code{-1.0}. | |
7a68c94a UD |
1516 | @end deftypefun |
1517 | ||
28f540f4 | 1518 | @comment math.h |
b4012b75 | 1519 | @comment ISO |
28f540f4 | 1520 | @deftypefun double rint (double @var{x}) |
4260bc74 UD |
1521 | @comment math.h |
1522 | @comment ISO | |
779ae82e | 1523 | @deftypefunx float rintf (float @var{x}) |
4260bc74 UD |
1524 | @comment math.h |
1525 | @comment ISO | |
779ae82e | 1526 | @deftypefunx {long double} rintl (long double @var{x}) |
b719dafd | 1527 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
b4012b75 | 1528 | These functions round @var{x} to an integer value according to the |
28f540f4 RM |
1529 | current rounding mode. @xref{Floating Point Parameters}, for |
1530 | information about the various rounding modes. The default | |
1531 | rounding mode is to round to the nearest integer; some machines | |
1532 | support other modes, but round-to-nearest is always used unless | |
7a68c94a UD |
1533 | you explicitly select another. |
1534 | ||
1535 | If @var{x} was not initially an integer, these functions raise the | |
1536 | inexact exception. | |
28f540f4 RM |
1537 | @end deftypefun |
1538 | ||
b4012b75 UD |
1539 | @comment math.h |
1540 | @comment ISO | |
1541 | @deftypefun double nearbyint (double @var{x}) | |
4260bc74 UD |
1542 | @comment math.h |
1543 | @comment ISO | |
779ae82e | 1544 | @deftypefunx float nearbyintf (float @var{x}) |
4260bc74 UD |
1545 | @comment math.h |
1546 | @comment ISO | |
779ae82e | 1547 | @deftypefunx {long double} nearbyintl (long double @var{x}) |
b719dafd | 1548 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
1549 | These functions return the same value as the @code{rint} functions, but |
1550 | do not raise the inexact exception if @var{x} is not an integer. | |
1551 | @end deftypefun | |
1552 | ||
1553 | @comment math.h | |
1554 | @comment ISO | |
1555 | @deftypefun double round (double @var{x}) | |
4260bc74 UD |
1556 | @comment math.h |
1557 | @comment ISO | |
7a68c94a | 1558 | @deftypefunx float roundf (float @var{x}) |
4260bc74 UD |
1559 | @comment math.h |
1560 | @comment ISO | |
7a68c94a | 1561 | @deftypefunx {long double} roundl (long double @var{x}) |
b719dafd | 1562 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a | 1563 | These functions are similar to @code{rint}, but they round halfway |
713df3d5 RM |
1564 | cases away from zero instead of to the nearest integer (or other |
1565 | current rounding mode). | |
7a68c94a UD |
1566 | @end deftypefun |
1567 | ||
1568 | @comment math.h | |
1569 | @comment ISO | |
1570 | @deftypefun {long int} lrint (double @var{x}) | |
4260bc74 UD |
1571 | @comment math.h |
1572 | @comment ISO | |
7a68c94a | 1573 | @deftypefunx {long int} lrintf (float @var{x}) |
4260bc74 UD |
1574 | @comment math.h |
1575 | @comment ISO | |
7a68c94a | 1576 | @deftypefunx {long int} lrintl (long double @var{x}) |
b719dafd | 1577 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
1578 | These functions are just like @code{rint}, but they return a |
1579 | @code{long int} instead of a floating-point number. | |
1580 | @end deftypefun | |
1581 | ||
1582 | @comment math.h | |
1583 | @comment ISO | |
1584 | @deftypefun {long long int} llrint (double @var{x}) | |
4260bc74 UD |
1585 | @comment math.h |
1586 | @comment ISO | |
7a68c94a | 1587 | @deftypefunx {long long int} llrintf (float @var{x}) |
4260bc74 UD |
1588 | @comment math.h |
1589 | @comment ISO | |
7a68c94a | 1590 | @deftypefunx {long long int} llrintl (long double @var{x}) |
b719dafd | 1591 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
1592 | These functions are just like @code{rint}, but they return a |
1593 | @code{long long int} instead of a floating-point number. | |
b4012b75 UD |
1594 | @end deftypefun |
1595 | ||
7a68c94a UD |
1596 | @comment math.h |
1597 | @comment ISO | |
1598 | @deftypefun {long int} lround (double @var{x}) | |
4260bc74 UD |
1599 | @comment math.h |
1600 | @comment ISO | |
7a68c94a | 1601 | @deftypefunx {long int} lroundf (float @var{x}) |
4260bc74 UD |
1602 | @comment math.h |
1603 | @comment ISO | |
7a68c94a | 1604 | @deftypefunx {long int} lroundl (long double @var{x}) |
b719dafd | 1605 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
1606 | These functions are just like @code{round}, but they return a |
1607 | @code{long int} instead of a floating-point number. | |
1608 | @end deftypefun | |
1609 | ||
1610 | @comment math.h | |
1611 | @comment ISO | |
1612 | @deftypefun {long long int} llround (double @var{x}) | |
4260bc74 UD |
1613 | @comment math.h |
1614 | @comment ISO | |
7a68c94a | 1615 | @deftypefunx {long long int} llroundf (float @var{x}) |
4260bc74 UD |
1616 | @comment math.h |
1617 | @comment ISO | |
7a68c94a | 1618 | @deftypefunx {long long int} llroundl (long double @var{x}) |
b719dafd | 1619 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
1620 | These functions are just like @code{round}, but they return a |
1621 | @code{long long int} instead of a floating-point number. | |
1622 | @end deftypefun | |
1623 | ||
1624 | ||
28f540f4 | 1625 | @comment math.h |
f65fd747 | 1626 | @comment ISO |
28f540f4 | 1627 | @deftypefun double modf (double @var{value}, double *@var{integer-part}) |
4260bc74 UD |
1628 | @comment math.h |
1629 | @comment ISO | |
f2ea0f5b | 1630 | @deftypefunx float modff (float @var{value}, float *@var{integer-part}) |
4260bc74 UD |
1631 | @comment math.h |
1632 | @comment ISO | |
779ae82e | 1633 | @deftypefunx {long double} modfl (long double @var{value}, long double *@var{integer-part}) |
b719dafd | 1634 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
b4012b75 | 1635 | These functions break the argument @var{value} into an integer part and a |
28f540f4 RM |
1636 | fractional part (between @code{-1} and @code{1}, exclusive). Their sum |
1637 | equals @var{value}. Each of the parts has the same sign as @var{value}, | |
7a68c94a | 1638 | and the integer part is always rounded toward zero. |
28f540f4 RM |
1639 | |
1640 | @code{modf} stores the integer part in @code{*@var{integer-part}}, and | |
1641 | returns the fractional part. For example, @code{modf (2.5, &intpart)} | |
1642 | returns @code{0.5} and stores @code{2.0} into @code{intpart}. | |
1643 | @end deftypefun | |
1644 | ||
7a68c94a UD |
1645 | @node Remainder Functions |
1646 | @subsection Remainder Functions | |
1647 | ||
1648 | The functions in this section compute the remainder on division of two | |
1649 | floating-point numbers. Each is a little different; pick the one that | |
1650 | suits your problem. | |
1651 | ||
28f540f4 | 1652 | @comment math.h |
f65fd747 | 1653 | @comment ISO |
28f540f4 | 1654 | @deftypefun double fmod (double @var{numerator}, double @var{denominator}) |
4260bc74 UD |
1655 | @comment math.h |
1656 | @comment ISO | |
779ae82e | 1657 | @deftypefunx float fmodf (float @var{numerator}, float @var{denominator}) |
4260bc74 UD |
1658 | @comment math.h |
1659 | @comment ISO | |
779ae82e | 1660 | @deftypefunx {long double} fmodl (long double @var{numerator}, long double @var{denominator}) |
b719dafd | 1661 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
b4012b75 | 1662 | These functions compute the remainder from the division of |
28f540f4 RM |
1663 | @var{numerator} by @var{denominator}. Specifically, the return value is |
1664 | @code{@var{numerator} - @w{@var{n} * @var{denominator}}}, where @var{n} | |
1665 | is the quotient of @var{numerator} divided by @var{denominator}, rounded | |
1666 | towards zero to an integer. Thus, @w{@code{fmod (6.5, 2.3)}} returns | |
1667 | @code{1.9}, which is @code{6.5} minus @code{4.6}. | |
1668 | ||
1669 | The result has the same sign as the @var{numerator} and has magnitude | |
1670 | less than the magnitude of the @var{denominator}. | |
1671 | ||
7a68c94a | 1672 | If @var{denominator} is zero, @code{fmod} signals a domain error. |
28f540f4 RM |
1673 | @end deftypefun |
1674 | ||
1675 | @comment math.h | |
1676 | @comment BSD | |
1677 | @deftypefun double drem (double @var{numerator}, double @var{denominator}) | |
4260bc74 UD |
1678 | @comment math.h |
1679 | @comment BSD | |
779ae82e | 1680 | @deftypefunx float dremf (float @var{numerator}, float @var{denominator}) |
4260bc74 UD |
1681 | @comment math.h |
1682 | @comment BSD | |
779ae82e | 1683 | @deftypefunx {long double} dreml (long double @var{numerator}, long double @var{denominator}) |
b719dafd | 1684 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
76cf9889 | 1685 | These functions are like @code{fmod} except that they round the |
28f540f4 RM |
1686 | internal quotient @var{n} to the nearest integer instead of towards zero |
1687 | to an integer. For example, @code{drem (6.5, 2.3)} returns @code{-0.4}, | |
1688 | which is @code{6.5} minus @code{6.9}. | |
1689 | ||
1690 | The absolute value of the result is less than or equal to half the | |
1691 | absolute value of the @var{denominator}. The difference between | |
1692 | @code{fmod (@var{numerator}, @var{denominator})} and @code{drem | |
1693 | (@var{numerator}, @var{denominator})} is always either | |
1694 | @var{denominator}, minus @var{denominator}, or zero. | |
1695 | ||
7a68c94a | 1696 | If @var{denominator} is zero, @code{drem} signals a domain error. |
28f540f4 RM |
1697 | @end deftypefun |
1698 | ||
7a68c94a UD |
1699 | @comment math.h |
1700 | @comment BSD | |
1701 | @deftypefun double remainder (double @var{numerator}, double @var{denominator}) | |
4260bc74 UD |
1702 | @comment math.h |
1703 | @comment BSD | |
7a68c94a | 1704 | @deftypefunx float remainderf (float @var{numerator}, float @var{denominator}) |
4260bc74 UD |
1705 | @comment math.h |
1706 | @comment BSD | |
7a68c94a | 1707 | @deftypefunx {long double} remainderl (long double @var{numerator}, long double @var{denominator}) |
b719dafd | 1708 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
1709 | This function is another name for @code{drem}. |
1710 | @end deftypefun | |
28f540f4 | 1711 | |
7a68c94a UD |
1712 | @node FP Bit Twiddling |
1713 | @subsection Setting and modifying single bits of FP values | |
fe0ec73e UD |
1714 | @cindex FP arithmetic |
1715 | ||
7a68c94a | 1716 | There are some operations that are too complicated or expensive to |
ec751a23 | 1717 | perform by hand on floating-point numbers. @w{ISO C99} defines |
7a68c94a UD |
1718 | functions to do these operations, which mostly involve changing single |
1719 | bits. | |
fe0ec73e UD |
1720 | |
1721 | @comment math.h | |
1722 | @comment ISO | |
1723 | @deftypefun double copysign (double @var{x}, double @var{y}) | |
4260bc74 UD |
1724 | @comment math.h |
1725 | @comment ISO | |
fe0ec73e | 1726 | @deftypefunx float copysignf (float @var{x}, float @var{y}) |
4260bc74 UD |
1727 | @comment math.h |
1728 | @comment ISO | |
fe0ec73e | 1729 | @deftypefunx {long double} copysignl (long double @var{x}, long double @var{y}) |
b719dafd | 1730 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
1731 | These functions return @var{x} but with the sign of @var{y}. They work |
1732 | even if @var{x} or @var{y} are NaN or zero. Both of these can carry a | |
1733 | sign (although not all implementations support it) and this is one of | |
1734 | the few operations that can tell the difference. | |
fe0ec73e | 1735 | |
7a68c94a UD |
1736 | @code{copysign} never raises an exception. |
1737 | @c except signalling NaNs | |
fe0ec73e UD |
1738 | |
1739 | This function is defined in @w{IEC 559} (and the appendix with | |
1740 | recommended functions in @w{IEEE 754}/@w{IEEE 854}). | |
1741 | @end deftypefun | |
1742 | ||
1743 | @comment math.h | |
1744 | @comment ISO | |
1745 | @deftypefun int signbit (@emph{float-type} @var{x}) | |
b719dafd | 1746 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
fe0ec73e UD |
1747 | @code{signbit} is a generic macro which can work on all floating-point |
1748 | types. It returns a nonzero value if the value of @var{x} has its sign | |
1749 | bit set. | |
1750 | ||
7a68c94a UD |
1751 | This is not the same as @code{x < 0.0}, because @w{IEEE 754} floating |
1752 | point allows zero to be signed. The comparison @code{-0.0 < 0.0} is | |
1753 | false, but @code{signbit (-0.0)} will return a nonzero value. | |
fe0ec73e UD |
1754 | @end deftypefun |
1755 | ||
1756 | @comment math.h | |
1757 | @comment ISO | |
1758 | @deftypefun double nextafter (double @var{x}, double @var{y}) | |
4260bc74 UD |
1759 | @comment math.h |
1760 | @comment ISO | |
fe0ec73e | 1761 | @deftypefunx float nextafterf (float @var{x}, float @var{y}) |
4260bc74 UD |
1762 | @comment math.h |
1763 | @comment ISO | |
fe0ec73e | 1764 | @deftypefunx {long double} nextafterl (long double @var{x}, long double @var{y}) |
b719dafd | 1765 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
fe0ec73e | 1766 | The @code{nextafter} function returns the next representable neighbor of |
7a68c94a UD |
1767 | @var{x} in the direction towards @var{y}. The size of the step between |
1768 | @var{x} and the result depends on the type of the result. If | |
0a7fef01 | 1769 | @math{@var{x} = @var{y}} the function simply returns @var{y}. If either |
7a68c94a UD |
1770 | value is @code{NaN}, @code{NaN} is returned. Otherwise |
1771 | a value corresponding to the value of the least significant bit in the | |
1772 | mantissa is added or subtracted, depending on the direction. | |
1773 | @code{nextafter} will signal overflow or underflow if the result goes | |
1774 | outside of the range of normalized numbers. | |
fe0ec73e UD |
1775 | |
1776 | This function is defined in @w{IEC 559} (and the appendix with | |
1777 | recommended functions in @w{IEEE 754}/@w{IEEE 854}). | |
1778 | @end deftypefun | |
1779 | ||
7a68c94a UD |
1780 | @comment math.h |
1781 | @comment ISO | |
36fe9ac9 | 1782 | @deftypefun double nexttoward (double @var{x}, long double @var{y}) |
4260bc74 UD |
1783 | @comment math.h |
1784 | @comment ISO | |
36fe9ac9 | 1785 | @deftypefunx float nexttowardf (float @var{x}, long double @var{y}) |
4260bc74 UD |
1786 | @comment math.h |
1787 | @comment ISO | |
36fe9ac9 | 1788 | @deftypefunx {long double} nexttowardl (long double @var{x}, long double @var{y}) |
b719dafd | 1789 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
1790 | These functions are identical to the corresponding versions of |
1791 | @code{nextafter} except that their second argument is a @code{long | |
1792 | double}. | |
1793 | @end deftypefun | |
1794 | ||
41a359e2 | 1795 | @comment math.h |
bf91be88 | 1796 | @comment ISO |
41a359e2 RS |
1797 | @deftypefun double nextup (double @var{x}) |
1798 | @comment math.h | |
bf91be88 | 1799 | @comment ISO |
41a359e2 RS |
1800 | @deftypefunx float nextupf (float @var{x}) |
1801 | @comment math.h | |
bf91be88 | 1802 | @comment ISO |
41a359e2 RS |
1803 | @deftypefunx {long double} nextupl (long double @var{x}) |
1804 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} | |
1805 | The @code{nextup} function returns the next representable neighbor of @var{x} | |
1806 | in the direction of positive infinity. If @var{x} is the smallest negative | |
1807 | subnormal number in the type of @var{x} the function returns @code{-0}. If | |
1808 | @math{@var{x} = @code{0}} the function returns the smallest positive subnormal | |
1809 | number in the type of @var{x}. If @var{x} is NaN, NaN is returned. | |
1810 | If @var{x} is @math{+@infinity{}}, @math{+@infinity{}} is returned. | |
71b48044 | 1811 | @code{nextup} is from TS 18661-1:2014. |
41a359e2 RS |
1812 | @code{nextup} never raises an exception except for signaling NaNs. |
1813 | @end deftypefun | |
1814 | ||
1815 | @comment math.h | |
bf91be88 | 1816 | @comment ISO |
41a359e2 RS |
1817 | @deftypefun double nextdown (double @var{x}) |
1818 | @comment math.h | |
bf91be88 | 1819 | @comment ISO |
41a359e2 RS |
1820 | @deftypefunx float nextdownf (float @var{x}) |
1821 | @comment math.h | |
bf91be88 | 1822 | @comment ISO |
41a359e2 RS |
1823 | @deftypefunx {long double} nextdownl (long double @var{x}) |
1824 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} | |
1825 | The @code{nextdown} function returns the next representable neighbor of @var{x} | |
1826 | in the direction of negative infinity. If @var{x} is the smallest positive | |
1827 | subnormal number in the type of @var{x} the function returns @code{+0}. If | |
1828 | @math{@var{x} = @code{0}} the function returns the smallest negative subnormal | |
1829 | number in the type of @var{x}. If @var{x} is NaN, NaN is returned. | |
1830 | If @var{x} is @math{-@infinity{}}, @math{-@infinity{}} is returned. | |
bf91be88 | 1831 | @code{nextdown} is from TS 18661-1:2014. |
41a359e2 RS |
1832 | @code{nextdown} never raises an exception except for signaling NaNs. |
1833 | @end deftypefun | |
1834 | ||
fe0ec73e UD |
1835 | @cindex NaN |
1836 | @comment math.h | |
1837 | @comment ISO | |
1838 | @deftypefun double nan (const char *@var{tagp}) | |
4260bc74 UD |
1839 | @comment math.h |
1840 | @comment ISO | |
fe0ec73e | 1841 | @deftypefunx float nanf (const char *@var{tagp}) |
4260bc74 UD |
1842 | @comment math.h |
1843 | @comment ISO | |
fe0ec73e | 1844 | @deftypefunx {long double} nanl (const char *@var{tagp}) |
b719dafd AO |
1845 | @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}} |
1846 | @c The unsafe-but-ruled-safe locale use comes from strtod. | |
7a68c94a UD |
1847 | The @code{nan} function returns a representation of NaN, provided that |
1848 | NaN is supported by the target platform. | |
1849 | @code{nan ("@var{n-char-sequence}")} is equivalent to | |
1850 | @code{strtod ("NAN(@var{n-char-sequence})")}. | |
1851 | ||
1852 | The argument @var{tagp} is used in an unspecified manner. On @w{IEEE | |
1853 | 754} systems, there are many representations of NaN, and @var{tagp} | |
1854 | selects one. On other systems it may do nothing. | |
fe0ec73e UD |
1855 | @end deftypefun |
1856 | ||
7a68c94a UD |
1857 | @node FP Comparison Functions |
1858 | @subsection Floating-Point Comparison Functions | |
1859 | @cindex unordered comparison | |
fe0ec73e | 1860 | |
7a68c94a UD |
1861 | The standard C comparison operators provoke exceptions when one or other |
1862 | of the operands is NaN. For example, | |
1863 | ||
1864 | @smallexample | |
1865 | int v = a < 1.0; | |
1866 | @end smallexample | |
1867 | ||
1868 | @noindent | |
1869 | will raise an exception if @var{a} is NaN. (This does @emph{not} | |
1870 | happen with @code{==} and @code{!=}; those merely return false and true, | |
1871 | respectively, when NaN is examined.) Frequently this exception is | |
ec751a23 | 1872 | undesirable. @w{ISO C99} therefore defines comparison functions that |
7a68c94a UD |
1873 | do not raise exceptions when NaN is examined. All of the functions are |
1874 | implemented as macros which allow their arguments to be of any | |
1875 | floating-point type. The macros are guaranteed to evaluate their | |
1876 | arguments only once. | |
1877 | ||
1878 | @comment math.h | |
1879 | @comment ISO | |
1880 | @deftypefn Macro int isgreater (@emph{real-floating} @var{x}, @emph{real-floating} @var{y}) | |
b719dafd | 1881 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
1882 | This macro determines whether the argument @var{x} is greater than |
1883 | @var{y}. It is equivalent to @code{(@var{x}) > (@var{y})}, but no | |
1884 | exception is raised if @var{x} or @var{y} are NaN. | |
1885 | @end deftypefn | |
1886 | ||
1887 | @comment math.h | |
1888 | @comment ISO | |
1889 | @deftypefn Macro int isgreaterequal (@emph{real-floating} @var{x}, @emph{real-floating} @var{y}) | |
b719dafd | 1890 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
1891 | This macro determines whether the argument @var{x} is greater than or |
1892 | equal to @var{y}. It is equivalent to @code{(@var{x}) >= (@var{y})}, but no | |
1893 | exception is raised if @var{x} or @var{y} are NaN. | |
1894 | @end deftypefn | |
1895 | ||
1896 | @comment math.h | |
1897 | @comment ISO | |
1898 | @deftypefn Macro int isless (@emph{real-floating} @var{x}, @emph{real-floating} @var{y}) | |
b719dafd | 1899 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
1900 | This macro determines whether the argument @var{x} is less than @var{y}. |
1901 | It is equivalent to @code{(@var{x}) < (@var{y})}, but no exception is | |
1902 | raised if @var{x} or @var{y} are NaN. | |
1903 | @end deftypefn | |
1904 | ||
1905 | @comment math.h | |
1906 | @comment ISO | |
1907 | @deftypefn Macro int islessequal (@emph{real-floating} @var{x}, @emph{real-floating} @var{y}) | |
b719dafd | 1908 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
1909 | This macro determines whether the argument @var{x} is less than or equal |
1910 | to @var{y}. It is equivalent to @code{(@var{x}) <= (@var{y})}, but no | |
1911 | exception is raised if @var{x} or @var{y} are NaN. | |
1912 | @end deftypefn | |
1913 | ||
1914 | @comment math.h | |
1915 | @comment ISO | |
1916 | @deftypefn Macro int islessgreater (@emph{real-floating} @var{x}, @emph{real-floating} @var{y}) | |
b719dafd | 1917 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
1918 | This macro determines whether the argument @var{x} is less or greater |
1919 | than @var{y}. It is equivalent to @code{(@var{x}) < (@var{y}) || | |
1920 | (@var{x}) > (@var{y})} (although it only evaluates @var{x} and @var{y} | |
1921 | once), but no exception is raised if @var{x} or @var{y} are NaN. | |
1922 | ||
1923 | This macro is not equivalent to @code{@var{x} != @var{y}}, because that | |
1924 | expression is true if @var{x} or @var{y} are NaN. | |
1925 | @end deftypefn | |
1926 | ||
1927 | @comment math.h | |
1928 | @comment ISO | |
1929 | @deftypefn Macro int isunordered (@emph{real-floating} @var{x}, @emph{real-floating} @var{y}) | |
b719dafd | 1930 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
1931 | This macro determines whether its arguments are unordered. In other |
1932 | words, it is true if @var{x} or @var{y} are NaN, and false otherwise. | |
1933 | @end deftypefn | |
1934 | ||
1935 | Not all machines provide hardware support for these operations. On | |
1936 | machines that don't, the macros can be very slow. Therefore, you should | |
1937 | not use these functions when NaN is not a concern. | |
1938 | ||
48b22986 | 1939 | @strong{NB:} There are no macros @code{isequal} or @code{isunequal}. |
7a68c94a UD |
1940 | They are unnecessary, because the @code{==} and @code{!=} operators do |
1941 | @emph{not} throw an exception if one or both of the operands are NaN. | |
1942 | ||
1943 | @node Misc FP Arithmetic | |
1944 | @subsection Miscellaneous FP arithmetic functions | |
fe0ec73e UD |
1945 | @cindex minimum |
1946 | @cindex maximum | |
7a68c94a UD |
1947 | @cindex positive difference |
1948 | @cindex multiply-add | |
fe0ec73e | 1949 | |
7a68c94a UD |
1950 | The functions in this section perform miscellaneous but common |
1951 | operations that are awkward to express with C operators. On some | |
1952 | processors these functions can use special machine instructions to | |
1953 | perform these operations faster than the equivalent C code. | |
fe0ec73e UD |
1954 | |
1955 | @comment math.h | |
1956 | @comment ISO | |
1957 | @deftypefun double fmin (double @var{x}, double @var{y}) | |
4260bc74 UD |
1958 | @comment math.h |
1959 | @comment ISO | |
fe0ec73e | 1960 | @deftypefunx float fminf (float @var{x}, float @var{y}) |
4260bc74 UD |
1961 | @comment math.h |
1962 | @comment ISO | |
fe0ec73e | 1963 | @deftypefunx {long double} fminl (long double @var{x}, long double @var{y}) |
b719dafd | 1964 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
1965 | The @code{fmin} function returns the lesser of the two values @var{x} |
1966 | and @var{y}. It is similar to the expression | |
1967 | @smallexample | |
1968 | ((x) < (y) ? (x) : (y)) | |
1969 | @end smallexample | |
1970 | except that @var{x} and @var{y} are only evaluated once. | |
fe0ec73e | 1971 | |
7a68c94a UD |
1972 | If an argument is NaN, the other argument is returned. If both arguments |
1973 | are NaN, NaN is returned. | |
fe0ec73e UD |
1974 | @end deftypefun |
1975 | ||
1976 | @comment math.h | |
1977 | @comment ISO | |
1978 | @deftypefun double fmax (double @var{x}, double @var{y}) | |
4260bc74 UD |
1979 | @comment math.h |
1980 | @comment ISO | |
fe0ec73e | 1981 | @deftypefunx float fmaxf (float @var{x}, float @var{y}) |
4260bc74 UD |
1982 | @comment math.h |
1983 | @comment ISO | |
fe0ec73e | 1984 | @deftypefunx {long double} fmaxl (long double @var{x}, long double @var{y}) |
b719dafd | 1985 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
1986 | The @code{fmax} function returns the greater of the two values @var{x} |
1987 | and @var{y}. | |
fe0ec73e | 1988 | |
7a68c94a UD |
1989 | If an argument is NaN, the other argument is returned. If both arguments |
1990 | are NaN, NaN is returned. | |
fe0ec73e UD |
1991 | @end deftypefun |
1992 | ||
1993 | @comment math.h | |
1994 | @comment ISO | |
1995 | @deftypefun double fdim (double @var{x}, double @var{y}) | |
4260bc74 UD |
1996 | @comment math.h |
1997 | @comment ISO | |
fe0ec73e | 1998 | @deftypefunx float fdimf (float @var{x}, float @var{y}) |
4260bc74 UD |
1999 | @comment math.h |
2000 | @comment ISO | |
fe0ec73e | 2001 | @deftypefunx {long double} fdiml (long double @var{x}, long double @var{y}) |
b719dafd | 2002 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
2003 | The @code{fdim} function returns the positive difference between |
2004 | @var{x} and @var{y}. The positive difference is @math{@var{x} - | |
2005 | @var{y}} if @var{x} is greater than @var{y}, and @math{0} otherwise. | |
fe0ec73e | 2006 | |
7a68c94a | 2007 | If @var{x}, @var{y}, or both are NaN, NaN is returned. |
fe0ec73e UD |
2008 | @end deftypefun |
2009 | ||
2010 | @comment math.h | |
2011 | @comment ISO | |
2012 | @deftypefun double fma (double @var{x}, double @var{y}, double @var{z}) | |
4260bc74 UD |
2013 | @comment math.h |
2014 | @comment ISO | |
fe0ec73e | 2015 | @deftypefunx float fmaf (float @var{x}, float @var{y}, float @var{z}) |
4260bc74 UD |
2016 | @comment math.h |
2017 | @comment ISO | |
fe0ec73e UD |
2018 | @deftypefunx {long double} fmal (long double @var{x}, long double @var{y}, long double @var{z}) |
2019 | @cindex butterfly | |
b719dafd | 2020 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
2021 | The @code{fma} function performs floating-point multiply-add. This is |
2022 | the operation @math{(@var{x} @mul{} @var{y}) + @var{z}}, but the | |
2023 | intermediate result is not rounded to the destination type. This can | |
2024 | sometimes improve the precision of a calculation. | |
2025 | ||
2026 | This function was introduced because some processors have a special | |
2027 | instruction to perform multiply-add. The C compiler cannot use it | |
2028 | directly, because the expression @samp{x*y + z} is defined to round the | |
2029 | intermediate result. @code{fma} lets you choose when you want to round | |
2030 | only once. | |
fe0ec73e UD |
2031 | |
2032 | @vindex FP_FAST_FMA | |
7a68c94a UD |
2033 | On processors which do not implement multiply-add in hardware, |
2034 | @code{fma} can be very slow since it must avoid intermediate rounding. | |
2035 | @file{math.h} defines the symbols @code{FP_FAST_FMA}, | |
2036 | @code{FP_FAST_FMAF}, and @code{FP_FAST_FMAL} when the corresponding | |
2037 | version of @code{fma} is no slower than the expression @samp{x*y + z}. | |
1f77f049 | 2038 | In @theglibc{}, this always means the operation is implemented in |
7a68c94a | 2039 | hardware. |
fe0ec73e UD |
2040 | @end deftypefun |
2041 | ||
7a68c94a UD |
2042 | @node Complex Numbers |
2043 | @section Complex Numbers | |
2044 | @pindex complex.h | |
2045 | @cindex complex numbers | |
2046 | ||
ec751a23 | 2047 | @w{ISO C99} introduces support for complex numbers in C. This is done |
7a68c94a UD |
2048 | with a new type qualifier, @code{complex}. It is a keyword if and only |
2049 | if @file{complex.h} has been included. There are three complex types, | |
2050 | corresponding to the three real types: @code{float complex}, | |
2051 | @code{double complex}, and @code{long double complex}. | |
2052 | ||
2053 | To construct complex numbers you need a way to indicate the imaginary | |
2054 | part of a number. There is no standard notation for an imaginary | |
2055 | floating point constant. Instead, @file{complex.h} defines two macros | |
2056 | that can be used to create complex numbers. | |
2057 | ||
2058 | @deftypevr Macro {const float complex} _Complex_I | |
2059 | This macro is a representation of the complex number ``@math{0+1i}''. | |
2060 | Multiplying a real floating-point value by @code{_Complex_I} gives a | |
2061 | complex number whose value is purely imaginary. You can use this to | |
2062 | construct complex constants: | |
2063 | ||
2064 | @smallexample | |
2065 | @math{3.0 + 4.0i} = @code{3.0 + 4.0 * _Complex_I} | |
2066 | @end smallexample | |
2067 | ||
2068 | Note that @code{_Complex_I * _Complex_I} has the value @code{-1}, but | |
2069 | the type of that value is @code{complex}. | |
2070 | @end deftypevr | |
2071 | ||
2072 | @c Put this back in when gcc supports _Imaginary_I. It's too confusing. | |
2073 | @ignore | |
2074 | @noindent | |
2075 | Without an optimizing compiler this is more expensive than the use of | |
2076 | @code{_Imaginary_I} but with is better than nothing. You can avoid all | |
2077 | the hassles if you use the @code{I} macro below if the name is not | |
2078 | problem. | |
2079 | ||
2080 | @deftypevr Macro {const float imaginary} _Imaginary_I | |
2081 | This macro is a representation of the value ``@math{1i}''. I.e., it is | |
2082 | the value for which | |
2083 | ||
2084 | @smallexample | |
2085 | _Imaginary_I * _Imaginary_I = -1 | |
2086 | @end smallexample | |
2087 | ||
2088 | @noindent | |
2089 | The result is not of type @code{float imaginary} but instead @code{float}. | |
2090 | One can use it to easily construct complex number like in | |
2091 | ||
2092 | @smallexample | |
2093 | 3.0 - _Imaginary_I * 4.0 | |
2094 | @end smallexample | |
2095 | ||
2096 | @noindent | |
2097 | which results in the complex number with a real part of 3.0 and a | |
2098 | imaginary part -4.0. | |
2099 | @end deftypevr | |
2100 | @end ignore | |
2101 | ||
2102 | @noindent | |
2103 | @code{_Complex_I} is a bit of a mouthful. @file{complex.h} also defines | |
2104 | a shorter name for the same constant. | |
2105 | ||
2106 | @deftypevr Macro {const float complex} I | |
2107 | This macro has exactly the same value as @code{_Complex_I}. Most of the | |
2108 | time it is preferable. However, it causes problems if you want to use | |
2109 | the identifier @code{I} for something else. You can safely write | |
2110 | ||
2111 | @smallexample | |
2112 | #include <complex.h> | |
2113 | #undef I | |
2114 | @end smallexample | |
2115 | ||
2116 | @noindent | |
2117 | if you need @code{I} for your own purposes. (In that case we recommend | |
2118 | you also define some other short name for @code{_Complex_I}, such as | |
2119 | @code{J}.) | |
2120 | ||
2121 | @ignore | |
2122 | If the implementation does not support the @code{imaginary} types | |
2123 | @code{I} is defined as @code{_Complex_I} which is the second best | |
2124 | solution. It still can be used in the same way but requires a most | |
2125 | clever compiler to get the same results. | |
2126 | @end ignore | |
2127 | @end deftypevr | |
2128 | ||
2129 | @node Operations on Complex | |
2130 | @section Projections, Conjugates, and Decomposing of Complex Numbers | |
2131 | @cindex project complex numbers | |
2132 | @cindex conjugate complex numbers | |
2133 | @cindex decompose complex numbers | |
2134 | @pindex complex.h | |
2135 | ||
ec751a23 | 2136 | @w{ISO C99} also defines functions that perform basic operations on |
7a68c94a UD |
2137 | complex numbers, such as decomposition and conjugation. The prototypes |
2138 | for all these functions are in @file{complex.h}. All functions are | |
2139 | available in three variants, one for each of the three complex types. | |
2140 | ||
2141 | @comment complex.h | |
2142 | @comment ISO | |
2143 | @deftypefun double creal (complex double @var{z}) | |
4260bc74 UD |
2144 | @comment complex.h |
2145 | @comment ISO | |
7a68c94a | 2146 | @deftypefunx float crealf (complex float @var{z}) |
4260bc74 UD |
2147 | @comment complex.h |
2148 | @comment ISO | |
7a68c94a | 2149 | @deftypefunx {long double} creall (complex long double @var{z}) |
b719dafd | 2150 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
2151 | These functions return the real part of the complex number @var{z}. |
2152 | @end deftypefun | |
2153 | ||
2154 | @comment complex.h | |
2155 | @comment ISO | |
2156 | @deftypefun double cimag (complex double @var{z}) | |
4260bc74 UD |
2157 | @comment complex.h |
2158 | @comment ISO | |
7a68c94a | 2159 | @deftypefunx float cimagf (complex float @var{z}) |
4260bc74 UD |
2160 | @comment complex.h |
2161 | @comment ISO | |
7a68c94a | 2162 | @deftypefunx {long double} cimagl (complex long double @var{z}) |
b719dafd | 2163 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
2164 | These functions return the imaginary part of the complex number @var{z}. |
2165 | @end deftypefun | |
2166 | ||
2167 | @comment complex.h | |
2168 | @comment ISO | |
2169 | @deftypefun {complex double} conj (complex double @var{z}) | |
4260bc74 UD |
2170 | @comment complex.h |
2171 | @comment ISO | |
7a68c94a | 2172 | @deftypefunx {complex float} conjf (complex float @var{z}) |
4260bc74 UD |
2173 | @comment complex.h |
2174 | @comment ISO | |
7a68c94a | 2175 | @deftypefunx {complex long double} conjl (complex long double @var{z}) |
b719dafd | 2176 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
2177 | These functions return the conjugate value of the complex number |
2178 | @var{z}. The conjugate of a complex number has the same real part and a | |
2179 | negated imaginary part. In other words, @samp{conj(a + bi) = a + -bi}. | |
2180 | @end deftypefun | |
2181 | ||
2182 | @comment complex.h | |
2183 | @comment ISO | |
2184 | @deftypefun double carg (complex double @var{z}) | |
4260bc74 UD |
2185 | @comment complex.h |
2186 | @comment ISO | |
7a68c94a | 2187 | @deftypefunx float cargf (complex float @var{z}) |
4260bc74 UD |
2188 | @comment complex.h |
2189 | @comment ISO | |
7a68c94a | 2190 | @deftypefunx {long double} cargl (complex long double @var{z}) |
b719dafd | 2191 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
2192 | These functions return the argument of the complex number @var{z}. |
2193 | The argument of a complex number is the angle in the complex plane | |
2194 | between the positive real axis and a line passing through zero and the | |
01f49f59 JT |
2195 | number. This angle is measured in the usual fashion and ranges from |
2196 | @math{-@pi{}} to @math{@pi{}}. | |
7a68c94a | 2197 | |
01f49f59 | 2198 | @code{carg} has a branch cut along the negative real axis. |
7a68c94a UD |
2199 | @end deftypefun |
2200 | ||
2201 | @comment complex.h | |
2202 | @comment ISO | |
2203 | @deftypefun {complex double} cproj (complex double @var{z}) | |
4260bc74 UD |
2204 | @comment complex.h |
2205 | @comment ISO | |
7a68c94a | 2206 | @deftypefunx {complex float} cprojf (complex float @var{z}) |
4260bc74 UD |
2207 | @comment complex.h |
2208 | @comment ISO | |
7a68c94a | 2209 | @deftypefunx {complex long double} cprojl (complex long double @var{z}) |
b719dafd | 2210 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a | 2211 | These functions return the projection of the complex value @var{z} onto |
9dcc8f11 | 2212 | the Riemann sphere. Values with an infinite imaginary part are projected |
7a68c94a UD |
2213 | to positive infinity on the real axis, even if the real part is NaN. If |
2214 | the real part is infinite, the result is equivalent to | |
2215 | ||
2216 | @smallexample | |
2217 | INFINITY + I * copysign (0.0, cimag (z)) | |
2218 | @end smallexample | |
2219 | @end deftypefun | |
fe0ec73e | 2220 | |
28f540f4 RM |
2221 | @node Parsing of Numbers |
2222 | @section Parsing of Numbers | |
2223 | @cindex parsing numbers (in formatted input) | |
2224 | @cindex converting strings to numbers | |
2225 | @cindex number syntax, parsing | |
2226 | @cindex syntax, for reading numbers | |
2227 | ||
2228 | This section describes functions for ``reading'' integer and | |
2229 | floating-point numbers from a string. It may be more convenient in some | |
2230 | cases to use @code{sscanf} or one of the related functions; see | |
2231 | @ref{Formatted Input}. But often you can make a program more robust by | |
2232 | finding the tokens in the string by hand, then converting the numbers | |
2233 | one by one. | |
2234 | ||
2235 | @menu | |
2236 | * Parsing of Integers:: Functions for conversion of integer values. | |
2237 | * Parsing of Floats:: Functions for conversion of floating-point | |
2238 | values. | |
2239 | @end menu | |
2240 | ||
2241 | @node Parsing of Integers | |
2242 | @subsection Parsing of Integers | |
2243 | ||
2244 | @pindex stdlib.h | |
b642f101 UD |
2245 | @pindex wchar.h |
2246 | The @samp{str} functions are declared in @file{stdlib.h} and those | |
2247 | beginning with @samp{wcs} are declared in @file{wchar.h}. One might | |
2248 | wonder about the use of @code{restrict} in the prototypes of the | |
2249 | functions in this section. It is seemingly useless but the @w{ISO C} | |
2250 | standard uses it (for the functions defined there) so we have to do it | |
2251 | as well. | |
28f540f4 RM |
2252 | |
2253 | @comment stdlib.h | |
f65fd747 | 2254 | @comment ISO |
b642f101 | 2255 | @deftypefun {long int} strtol (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base}) |
b719dafd AO |
2256 | @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}} |
2257 | @c strtol uses the thread-local pointer to the locale in effect, and | |
2258 | @c strtol_l loads the LC_NUMERIC locale data from it early on and once, | |
2259 | @c but if the locale is the global locale, and another thread calls | |
2260 | @c setlocale in a way that modifies the pointer to the LC_CTYPE locale | |
2261 | @c category, the behavior of e.g. IS*, TOUPPER will vary throughout the | |
2262 | @c execution of the function, because they re-read the locale data from | |
2263 | @c the given locale pointer. We solved this by documenting setlocale as | |
2264 | @c MT-Unsafe. | |
28f540f4 RM |
2265 | The @code{strtol} (``string-to-long'') function converts the initial |
2266 | part of @var{string} to a signed integer, which is returned as a value | |
b8fe19fa | 2267 | of type @code{long int}. |
28f540f4 RM |
2268 | |
2269 | This function attempts to decompose @var{string} as follows: | |
2270 | ||
2271 | @itemize @bullet | |
b8fe19fa | 2272 | @item |
28f540f4 RM |
2273 | A (possibly empty) sequence of whitespace characters. Which characters |
2274 | are whitespace is determined by the @code{isspace} function | |
2275 | (@pxref{Classification of Characters}). These are discarded. | |
2276 | ||
b8fe19fa | 2277 | @item |
28f540f4 RM |
2278 | An optional plus or minus sign (@samp{+} or @samp{-}). |
2279 | ||
b8fe19fa | 2280 | @item |
28f540f4 RM |
2281 | A nonempty sequence of digits in the radix specified by @var{base}. |
2282 | ||
2283 | If @var{base} is zero, decimal radix is assumed unless the series of | |
2284 | digits begins with @samp{0} (specifying octal radix), or @samp{0x} or | |
2285 | @samp{0X} (specifying hexadecimal radix); in other words, the same | |
2286 | syntax used for integer constants in C. | |
2287 | ||
600a7457 | 2288 | Otherwise @var{base} must have a value between @code{2} and @code{36}. |
28f540f4 | 2289 | If @var{base} is @code{16}, the digits may optionally be preceded by |
2c6fe0bd UD |
2290 | @samp{0x} or @samp{0X}. If base has no legal value the value returned |
2291 | is @code{0l} and the global variable @code{errno} is set to @code{EINVAL}. | |
28f540f4 | 2292 | |
b8fe19fa | 2293 | @item |
28f540f4 RM |
2294 | Any remaining characters in the string. If @var{tailptr} is not a null |
2295 | pointer, @code{strtol} stores a pointer to this tail in | |
2296 | @code{*@var{tailptr}}. | |
2297 | @end itemize | |
2298 | ||
2299 | If the string is empty, contains only whitespace, or does not contain an | |
2300 | initial substring that has the expected syntax for an integer in the | |
2301 | specified @var{base}, no conversion is performed. In this case, | |
2302 | @code{strtol} returns a value of zero and the value stored in | |
2303 | @code{*@var{tailptr}} is the value of @var{string}. | |
2304 | ||
2305 | In a locale other than the standard @code{"C"} locale, this function | |
2306 | may recognize additional implementation-dependent syntax. | |
2307 | ||
2308 | If the string has valid syntax for an integer but the value is not | |
2309 | representable because of overflow, @code{strtol} returns either | |
2310 | @code{LONG_MAX} or @code{LONG_MIN} (@pxref{Range of Type}), as | |
2311 | appropriate for the sign of the value. It also sets @code{errno} | |
2312 | to @code{ERANGE} to indicate there was overflow. | |
2313 | ||
7a68c94a UD |
2314 | You should not check for errors by examining the return value of |
2315 | @code{strtol}, because the string might be a valid representation of | |
2316 | @code{0l}, @code{LONG_MAX}, or @code{LONG_MIN}. Instead, check whether | |
2317 | @var{tailptr} points to what you expect after the number | |
2318 | (e.g. @code{'\0'} if the string should end after the number). You also | |
2319 | need to clear @var{errno} before the call and check it afterward, in | |
2320 | case there was overflow. | |
2c6fe0bd | 2321 | |
28f540f4 RM |
2322 | There is an example at the end of this section. |
2323 | @end deftypefun | |
2324 | ||
b642f101 UD |
2325 | @comment wchar.h |
2326 | @comment ISO | |
2327 | @deftypefun {long int} wcstol (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base}) | |
b719dafd | 2328 | @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}} |
3554743a AJ |
2329 | The @code{wcstol} function is equivalent to the @code{strtol} function |
2330 | in nearly all aspects but handles wide character strings. | |
b642f101 UD |
2331 | |
2332 | The @code{wcstol} function was introduced in @w{Amendment 1} of @w{ISO C90}. | |
2333 | @end deftypefun | |
2334 | ||
28f540f4 | 2335 | @comment stdlib.h |
f65fd747 | 2336 | @comment ISO |
b642f101 | 2337 | @deftypefun {unsigned long int} strtoul (const char *retrict @var{string}, char **restrict @var{tailptr}, int @var{base}) |
b719dafd | 2338 | @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}} |
28f540f4 | 2339 | The @code{strtoul} (``string-to-unsigned-long'') function is like |
0e4ee106 | 2340 | @code{strtol} except it converts to an @code{unsigned long int} value. |
7a68c94a | 2341 | The syntax is the same as described above for @code{strtol}. The value |
0e4ee106 UD |
2342 | returned on overflow is @code{ULONG_MAX} (@pxref{Range of Type}). |
2343 | ||
2344 | If @var{string} depicts a negative number, @code{strtoul} acts the same | |
2345 | as @var{strtol} but casts the result to an unsigned integer. That means | |
2346 | for example that @code{strtoul} on @code{"-1"} returns @code{ULONG_MAX} | |
e6e81391 | 2347 | and an input more negative than @code{LONG_MIN} returns |
0e4ee106 | 2348 | (@code{ULONG_MAX} + 1) / 2. |
7a68c94a UD |
2349 | |
2350 | @code{strtoul} sets @var{errno} to @code{EINVAL} if @var{base} is out of | |
2351 | range, or @code{ERANGE} on overflow. | |
2c6fe0bd UD |
2352 | @end deftypefun |
2353 | ||
b642f101 UD |
2354 | @comment wchar.h |
2355 | @comment ISO | |
2356 | @deftypefun {unsigned long int} wcstoul (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base}) | |
b719dafd | 2357 | @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}} |
3554743a AJ |
2358 | The @code{wcstoul} function is equivalent to the @code{strtoul} function |
2359 | in nearly all aspects but handles wide character strings. | |
b642f101 UD |
2360 | |
2361 | The @code{wcstoul} function was introduced in @w{Amendment 1} of @w{ISO C90}. | |
2362 | @end deftypefun | |
2363 | ||
2c6fe0bd | 2364 | @comment stdlib.h |
7a68c94a | 2365 | @comment ISO |
b642f101 | 2366 | @deftypefun {long long int} strtoll (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base}) |
b719dafd | 2367 | @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}} |
7a68c94a UD |
2368 | The @code{strtoll} function is like @code{strtol} except that it returns |
2369 | a @code{long long int} value, and accepts numbers with a correspondingly | |
2370 | larger range. | |
2c6fe0bd UD |
2371 | |
2372 | If the string has valid syntax for an integer but the value is not | |
fe7bdd63 | 2373 | representable because of overflow, @code{strtoll} returns either |
7bb764bc | 2374 | @code{LLONG_MAX} or @code{LLONG_MIN} (@pxref{Range of Type}), as |
2c6fe0bd UD |
2375 | appropriate for the sign of the value. It also sets @code{errno} to |
2376 | @code{ERANGE} to indicate there was overflow. | |
2c6fe0bd | 2377 | |
ec751a23 | 2378 | The @code{strtoll} function was introduced in @w{ISO C99}. |
2c6fe0bd UD |
2379 | @end deftypefun |
2380 | ||
b642f101 UD |
2381 | @comment wchar.h |
2382 | @comment ISO | |
2383 | @deftypefun {long long int} wcstoll (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base}) | |
b719dafd | 2384 | @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}} |
3554743a AJ |
2385 | The @code{wcstoll} function is equivalent to the @code{strtoll} function |
2386 | in nearly all aspects but handles wide character strings. | |
b642f101 UD |
2387 | |
2388 | The @code{wcstoll} function was introduced in @w{Amendment 1} of @w{ISO C90}. | |
2389 | @end deftypefun | |
2390 | ||
2c6fe0bd UD |
2391 | @comment stdlib.h |
2392 | @comment BSD | |
b642f101 | 2393 | @deftypefun {long long int} strtoq (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base}) |
b719dafd | 2394 | @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}} |
7a68c94a | 2395 | @code{strtoq} (``string-to-quad-word'') is the BSD name for @code{strtoll}. |
2c6fe0bd UD |
2396 | @end deftypefun |
2397 | ||
b642f101 UD |
2398 | @comment wchar.h |
2399 | @comment GNU | |
2400 | @deftypefun {long long int} wcstoq (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base}) | |
b719dafd | 2401 | @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}} |
3554743a AJ |
2402 | The @code{wcstoq} function is equivalent to the @code{strtoq} function |
2403 | in nearly all aspects but handles wide character strings. | |
b642f101 UD |
2404 | |
2405 | The @code{wcstoq} function is a GNU extension. | |
2406 | @end deftypefun | |
2407 | ||
2c6fe0bd | 2408 | @comment stdlib.h |
7a68c94a | 2409 | @comment ISO |
b642f101 | 2410 | @deftypefun {unsigned long long int} strtoull (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base}) |
b719dafd | 2411 | @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}} |
0e4ee106 UD |
2412 | The @code{strtoull} function is related to @code{strtoll} the same way |
2413 | @code{strtoul} is related to @code{strtol}. | |
fe7bdd63 | 2414 | |
ec751a23 | 2415 | The @code{strtoull} function was introduced in @w{ISO C99}. |
fe7bdd63 UD |
2416 | @end deftypefun |
2417 | ||
b642f101 UD |
2418 | @comment wchar.h |
2419 | @comment ISO | |
2420 | @deftypefun {unsigned long long int} wcstoull (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base}) | |
b719dafd | 2421 | @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}} |
3554743a AJ |
2422 | The @code{wcstoull} function is equivalent to the @code{strtoull} function |
2423 | in nearly all aspects but handles wide character strings. | |
b642f101 UD |
2424 | |
2425 | The @code{wcstoull} function was introduced in @w{Amendment 1} of @w{ISO C90}. | |
2426 | @end deftypefun | |
2427 | ||
fe7bdd63 UD |
2428 | @comment stdlib.h |
2429 | @comment BSD | |
b642f101 | 2430 | @deftypefun {unsigned long long int} strtouq (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base}) |
b719dafd | 2431 | @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}} |
7a68c94a | 2432 | @code{strtouq} is the BSD name for @code{strtoull}. |
28f540f4 RM |
2433 | @end deftypefun |
2434 | ||
b642f101 UD |
2435 | @comment wchar.h |
2436 | @comment GNU | |
2437 | @deftypefun {unsigned long long int} wcstouq (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base}) | |
b719dafd | 2438 | @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}} |
3554743a AJ |
2439 | The @code{wcstouq} function is equivalent to the @code{strtouq} function |
2440 | in nearly all aspects but handles wide character strings. | |
b642f101 | 2441 | |
f5708cb0 | 2442 | The @code{wcstouq} function is a GNU extension. |
b642f101 UD |
2443 | @end deftypefun |
2444 | ||
0e4ee106 | 2445 | @comment inttypes.h |
b642f101 UD |
2446 | @comment ISO |
2447 | @deftypefun intmax_t strtoimax (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base}) | |
b719dafd | 2448 | @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}} |
0e4ee106 UD |
2449 | The @code{strtoimax} function is like @code{strtol} except that it returns |
2450 | a @code{intmax_t} value, and accepts numbers of a corresponding range. | |
2451 | ||
2452 | If the string has valid syntax for an integer but the value is not | |
2453 | representable because of overflow, @code{strtoimax} returns either | |
2454 | @code{INTMAX_MAX} or @code{INTMAX_MIN} (@pxref{Integers}), as | |
2455 | appropriate for the sign of the value. It also sets @code{errno} to | |
2456 | @code{ERANGE} to indicate there was overflow. | |
2457 | ||
b642f101 UD |
2458 | See @ref{Integers} for a description of the @code{intmax_t} type. The |
2459 | @code{strtoimax} function was introduced in @w{ISO C99}. | |
2460 | @end deftypefun | |
0e4ee106 | 2461 | |
b642f101 UD |
2462 | @comment wchar.h |
2463 | @comment ISO | |
2464 | @deftypefun intmax_t wcstoimax (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base}) | |
b719dafd | 2465 | @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}} |
3554743a AJ |
2466 | The @code{wcstoimax} function is equivalent to the @code{strtoimax} function |
2467 | in nearly all aspects but handles wide character strings. | |
0e4ee106 | 2468 | |
b642f101 | 2469 | The @code{wcstoimax} function was introduced in @w{ISO C99}. |
0e4ee106 UD |
2470 | @end deftypefun |
2471 | ||
2472 | @comment inttypes.h | |
b642f101 UD |
2473 | @comment ISO |
2474 | @deftypefun uintmax_t strtoumax (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base}) | |
b719dafd | 2475 | @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}} |
0e4ee106 UD |
2476 | The @code{strtoumax} function is related to @code{strtoimax} |
2477 | the same way that @code{strtoul} is related to @code{strtol}. | |
2478 | ||
b642f101 UD |
2479 | See @ref{Integers} for a description of the @code{intmax_t} type. The |
2480 | @code{strtoumax} function was introduced in @w{ISO C99}. | |
2481 | @end deftypefun | |
0e4ee106 | 2482 | |
b642f101 UD |
2483 | @comment wchar.h |
2484 | @comment ISO | |
2485 | @deftypefun uintmax_t wcstoumax (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base}) | |
b719dafd | 2486 | @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}} |
3554743a AJ |
2487 | The @code{wcstoumax} function is equivalent to the @code{strtoumax} function |
2488 | in nearly all aspects but handles wide character strings. | |
b642f101 UD |
2489 | |
2490 | The @code{wcstoumax} function was introduced in @w{ISO C99}. | |
0e4ee106 UD |
2491 | @end deftypefun |
2492 | ||
28f540f4 | 2493 | @comment stdlib.h |
f65fd747 | 2494 | @comment ISO |
28f540f4 | 2495 | @deftypefun {long int} atol (const char *@var{string}) |
b719dafd | 2496 | @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}} |
28f540f4 RM |
2497 | This function is similar to the @code{strtol} function with a @var{base} |
2498 | argument of @code{10}, except that it need not detect overflow errors. | |
2499 | The @code{atol} function is provided mostly for compatibility with | |
2500 | existing code; using @code{strtol} is more robust. | |
2501 | @end deftypefun | |
2502 | ||
2503 | @comment stdlib.h | |
f65fd747 | 2504 | @comment ISO |
28f540f4 | 2505 | @deftypefun int atoi (const char *@var{string}) |
b719dafd | 2506 | @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}} |
7a68c94a UD |
2507 | This function is like @code{atol}, except that it returns an @code{int}. |
2508 | The @code{atoi} function is also considered obsolete; use @code{strtol} | |
2509 | instead. | |
28f540f4 RM |
2510 | @end deftypefun |
2511 | ||
fe7bdd63 | 2512 | @comment stdlib.h |
7a68c94a | 2513 | @comment ISO |
fe7bdd63 | 2514 | @deftypefun {long long int} atoll (const char *@var{string}) |
b719dafd | 2515 | @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}} |
fe7bdd63 | 2516 | This function is similar to @code{atol}, except it returns a @code{long |
7a68c94a | 2517 | long int}. |
fe7bdd63 | 2518 | |
ec751a23 | 2519 | The @code{atoll} function was introduced in @w{ISO C99}. It too is |
7a68c94a | 2520 | obsolete (despite having just been added); use @code{strtoll} instead. |
fe7bdd63 UD |
2521 | @end deftypefun |
2522 | ||
b642f101 UD |
2523 | All the functions mentioned in this section so far do not handle |
2524 | alternative representations of characters as described in the locale | |
2525 | data. Some locales specify thousands separator and the way they have to | |
2526 | be used which can help to make large numbers more readable. To read | |
2527 | such numbers one has to use the @code{scanf} functions with the @samp{'} | |
2528 | flag. | |
2c6fe0bd | 2529 | |
28f540f4 RM |
2530 | Here is a function which parses a string as a sequence of integers and |
2531 | returns the sum of them: | |
2532 | ||
2533 | @smallexample | |
2534 | int | |
2535 | sum_ints_from_string (char *string) | |
2536 | @{ | |
2537 | int sum = 0; | |
2538 | ||
2539 | while (1) @{ | |
2540 | char *tail; | |
2541 | int next; | |
2542 | ||
2543 | /* @r{Skip whitespace by hand, to detect the end.} */ | |
2544 | while (isspace (*string)) string++; | |
2545 | if (*string == 0) | |
2546 | break; | |
2547 | ||
2548 | /* @r{There is more nonwhitespace,} */ | |
2549 | /* @r{so it ought to be another number.} */ | |
2550 | errno = 0; | |
2551 | /* @r{Parse it.} */ | |
2552 | next = strtol (string, &tail, 0); | |
2553 | /* @r{Add it in, if not overflow.} */ | |
2554 | if (errno) | |
2555 | printf ("Overflow\n"); | |
2556 | else | |
2557 | sum += next; | |
2558 | /* @r{Advance past it.} */ | |
2559 | string = tail; | |
2560 | @} | |
2561 | ||
2562 | return sum; | |
2563 | @} | |
2564 | @end smallexample | |
2565 | ||
2566 | @node Parsing of Floats | |
2567 | @subsection Parsing of Floats | |
2568 | ||
2569 | @pindex stdlib.h | |
b642f101 UD |
2570 | The @samp{str} functions are declared in @file{stdlib.h} and those |
2571 | beginning with @samp{wcs} are declared in @file{wchar.h}. One might | |
2572 | wonder about the use of @code{restrict} in the prototypes of the | |
2573 | functions in this section. It is seemingly useless but the @w{ISO C} | |
2574 | standard uses it (for the functions defined there) so we have to do it | |
2575 | as well. | |
28f540f4 RM |
2576 | |
2577 | @comment stdlib.h | |
f65fd747 | 2578 | @comment ISO |
b642f101 | 2579 | @deftypefun double strtod (const char *restrict @var{string}, char **restrict @var{tailptr}) |
b719dafd AO |
2580 | @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}} |
2581 | @c Besides the unsafe-but-ruled-safe locale uses, this uses a lot of | |
2582 | @c mpn, but it's all safe. | |
2583 | @c | |
2584 | @c round_and_return | |
2585 | @c get_rounding_mode ok | |
2586 | @c mpn_add_1 ok | |
2587 | @c mpn_rshift ok | |
2588 | @c MPN_ZERO ok | |
2589 | @c MPN2FLOAT -> mpn_construct_(float|double|long_double) ok | |
2590 | @c str_to_mpn | |
2591 | @c mpn_mul_1 -> umul_ppmm ok | |
2592 | @c mpn_add_1 ok | |
2593 | @c mpn_lshift_1 -> mpn_lshift ok | |
2594 | @c STRTOF_INTERNAL | |
2595 | @c MPN_VAR ok | |
2596 | @c SET_MANTISSA ok | |
2597 | @c STRNCASECMP ok, wide and narrow | |
2598 | @c round_and_return ok | |
2599 | @c mpn_mul ok | |
2600 | @c mpn_addmul_1 ok | |
2601 | @c ... mpn_sub | |
2602 | @c mpn_lshift ok | |
2603 | @c udiv_qrnnd ok | |
2604 | @c count_leading_zeros ok | |
2605 | @c add_ssaaaa ok | |
2606 | @c sub_ddmmss ok | |
2607 | @c umul_ppmm ok | |
2608 | @c mpn_submul_1 ok | |
28f540f4 RM |
2609 | The @code{strtod} (``string-to-double'') function converts the initial |
2610 | part of @var{string} to a floating-point number, which is returned as a | |
b8fe19fa | 2611 | value of type @code{double}. |
28f540f4 RM |
2612 | |
2613 | This function attempts to decompose @var{string} as follows: | |
2614 | ||
2615 | @itemize @bullet | |
b8fe19fa | 2616 | @item |
28f540f4 RM |
2617 | A (possibly empty) sequence of whitespace characters. Which characters |
2618 | are whitespace is determined by the @code{isspace} function | |
2619 | (@pxref{Classification of Characters}). These are discarded. | |
2620 | ||
2621 | @item | |
2622 | An optional plus or minus sign (@samp{+} or @samp{-}). | |
2623 | ||
0c34b1e9 UD |
2624 | @item A floating point number in decimal or hexadecimal format. The |
2625 | decimal format is: | |
2626 | @itemize @minus | |
2627 | ||
28f540f4 RM |
2628 | @item |
2629 | A nonempty sequence of digits optionally containing a decimal-point | |
2630 | character---normally @samp{.}, but it depends on the locale | |
85c165be | 2631 | (@pxref{General Numeric}). |
28f540f4 RM |
2632 | |
2633 | @item | |
2634 | An optional exponent part, consisting of a character @samp{e} or | |
2635 | @samp{E}, an optional sign, and a sequence of digits. | |
2636 | ||
0c34b1e9 UD |
2637 | @end itemize |
2638 | ||
2639 | The hexadecimal format is as follows: | |
2640 | @itemize @minus | |
2641 | ||
2642 | @item | |
2643 | A 0x or 0X followed by a nonempty sequence of hexadecimal digits | |
2644 | optionally containing a decimal-point character---normally @samp{.}, but | |
2645 | it depends on the locale (@pxref{General Numeric}). | |
2646 | ||
2647 | @item | |
2648 | An optional binary-exponent part, consisting of a character @samp{p} or | |
2649 | @samp{P}, an optional sign, and a sequence of digits. | |
2650 | ||
2651 | @end itemize | |
2652 | ||
28f540f4 RM |
2653 | @item |
2654 | Any remaining characters in the string. If @var{tailptr} is not a null | |
2655 | pointer, a pointer to this tail of the string is stored in | |
2656 | @code{*@var{tailptr}}. | |
2657 | @end itemize | |
2658 | ||
2659 | If the string is empty, contains only whitespace, or does not contain an | |
2660 | initial substring that has the expected syntax for a floating-point | |
2661 | number, no conversion is performed. In this case, @code{strtod} returns | |
2662 | a value of zero and the value returned in @code{*@var{tailptr}} is the | |
2663 | value of @var{string}. | |
2664 | ||
26761c28 | 2665 | In a locale other than the standard @code{"C"} or @code{"POSIX"} locales, |
2c6fe0bd | 2666 | this function may recognize additional locale-dependent syntax. |
28f540f4 RM |
2667 | |
2668 | If the string has valid syntax for a floating-point number but the value | |
7a68c94a UD |
2669 | is outside the range of a @code{double}, @code{strtod} will signal |
2670 | overflow or underflow as described in @ref{Math Error Reporting}. | |
2671 | ||
2672 | @code{strtod} recognizes four special input strings. The strings | |
2673 | @code{"inf"} and @code{"infinity"} are converted to @math{@infinity{}}, | |
2674 | or to the largest representable value if the floating-point format | |
2675 | doesn't support infinities. You can prepend a @code{"+"} or @code{"-"} | |
2676 | to specify the sign. Case is ignored when scanning these strings. | |
2677 | ||
95fdc6a0 UD |
2678 | The strings @code{"nan"} and @code{"nan(@var{chars@dots{}})"} are converted |
2679 | to NaN. Again, case is ignored. If @var{chars@dots{}} are provided, they | |
7a68c94a UD |
2680 | are used in some unspecified fashion to select a particular |
2681 | representation of NaN (there can be several). | |
2682 | ||
2683 | Since zero is a valid result as well as the value returned on error, you | |
2684 | should check for errors in the same way as for @code{strtol}, by | |
2685 | examining @var{errno} and @var{tailptr}. | |
28f540f4 RM |
2686 | @end deftypefun |
2687 | ||
2c6fe0bd | 2688 | @comment stdlib.h |
ec751a23 | 2689 | @comment ISO |
2c6fe0bd | 2690 | @deftypefun float strtof (const char *@var{string}, char **@var{tailptr}) |
4260bc74 | 2691 | @comment stdlib.h |
ec751a23 | 2692 | @comment ISO |
7a68c94a | 2693 | @deftypefunx {long double} strtold (const char *@var{string}, char **@var{tailptr}) |
b719dafd | 2694 | @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}} |
7a68c94a UD |
2695 | These functions are analogous to @code{strtod}, but return @code{float} |
2696 | and @code{long double} values respectively. They report errors in the | |
2697 | same way as @code{strtod}. @code{strtof} can be substantially faster | |
2698 | than @code{strtod}, but has less precision; conversely, @code{strtold} | |
2699 | can be much slower but has more precision (on systems where @code{long | |
2700 | double} is a separate type). | |
2701 | ||
ec751a23 | 2702 | These functions have been GNU extensions and are new to @w{ISO C99}. |
2c6fe0bd UD |
2703 | @end deftypefun |
2704 | ||
b642f101 UD |
2705 | @comment wchar.h |
2706 | @comment ISO | |
2707 | @deftypefun double wcstod (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}) | |
2708 | @comment stdlib.h | |
2709 | @comment ISO | |
2710 | @deftypefunx float wcstof (const wchar_t *@var{string}, wchar_t **@var{tailptr}) | |
2711 | @comment stdlib.h | |
2712 | @comment ISO | |
2713 | @deftypefunx {long double} wcstold (const wchar_t *@var{string}, wchar_t **@var{tailptr}) | |
b719dafd | 2714 | @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}} |
b642f101 UD |
2715 | The @code{wcstod}, @code{wcstof}, and @code{wcstol} functions are |
2716 | equivalent in nearly all aspect to the @code{strtod}, @code{strtof}, and | |
2717 | @code{strtold} functions but it handles wide character string. | |
2718 | ||
2719 | The @code{wcstod} function was introduced in @w{Amendment 1} of @w{ISO | |
2720 | C90}. The @code{wcstof} and @code{wcstold} functions were introduced in | |
2721 | @w{ISO C99}. | |
2722 | @end deftypefun | |
2723 | ||
28f540f4 | 2724 | @comment stdlib.h |
f65fd747 | 2725 | @comment ISO |
28f540f4 | 2726 | @deftypefun double atof (const char *@var{string}) |
b719dafd | 2727 | @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}} |
28f540f4 RM |
2728 | This function is similar to the @code{strtod} function, except that it |
2729 | need not detect overflow and underflow errors. The @code{atof} function | |
2730 | is provided mostly for compatibility with existing code; using | |
2731 | @code{strtod} is more robust. | |
2732 | @end deftypefun | |
880f421f | 2733 | |
1f77f049 | 2734 | @Theglibc{} also provides @samp{_l} versions of these functions, |
7a68c94a | 2735 | which take an additional argument, the locale to use in conversion. |
aa04af00 AM |
2736 | |
2737 | See also @ref{Parsing of Integers}. | |
880f421f | 2738 | |
7a68c94a UD |
2739 | @node System V Number Conversion |
2740 | @section Old-fashioned System V number-to-string functions | |
880f421f | 2741 | |
7a68c94a | 2742 | The old @w{System V} C library provided three functions to convert |
1f77f049 JM |
2743 | numbers to strings, with unusual and hard-to-use semantics. @Theglibc{} |
2744 | also provides these functions and some natural extensions. | |
880f421f | 2745 | |
1f77f049 | 2746 | These functions are only available in @theglibc{} and on systems descended |
7a68c94a UD |
2747 | from AT&T Unix. Therefore, unless these functions do precisely what you |
2748 | need, it is better to use @code{sprintf}, which is standard. | |
880f421f | 2749 | |
7a68c94a | 2750 | All these functions are defined in @file{stdlib.h}. |
880f421f UD |
2751 | |
2752 | @comment stdlib.h | |
2753 | @comment SVID, Unix98 | |
7a68c94a | 2754 | @deftypefun {char *} ecvt (double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg}) |
b719dafd | 2755 | @safety{@prelim{}@mtunsafe{@mtasurace{:ecvt}}@asunsafe{}@acsafe{}} |
880f421f | 2756 | The function @code{ecvt} converts the floating-point number @var{value} |
0ea5db4f | 2757 | to a string with at most @var{ndigit} decimal digits. The |
cf822e3c | 2758 | returned string contains no decimal point or sign. The first digit of |
0ea5db4f UD |
2759 | the string is non-zero (unless @var{value} is actually zero) and the |
2760 | last digit is rounded to nearest. @code{*@var{decpt}} is set to the | |
7a68c94a | 2761 | index in the string of the first digit after the decimal point. |
0ea5db4f UD |
2762 | @code{*@var{neg}} is set to a nonzero value if @var{value} is negative, |
2763 | zero otherwise. | |
880f421f | 2764 | |
67994d6f UD |
2765 | If @var{ndigit} decimal digits would exceed the precision of a |
2766 | @code{double} it is reduced to a system-specific value. | |
2767 | ||
880f421f UD |
2768 | The returned string is statically allocated and overwritten by each call |
2769 | to @code{ecvt}. | |
2770 | ||
0ea5db4f UD |
2771 | If @var{value} is zero, it is implementation defined whether |
2772 | @code{*@var{decpt}} is @code{0} or @code{1}. | |
880f421f | 2773 | |
0ea5db4f UD |
2774 | For example: @code{ecvt (12.3, 5, &d, &n)} returns @code{"12300"} |
2775 | and sets @var{d} to @code{2} and @var{n} to @code{0}. | |
880f421f UD |
2776 | @end deftypefun |
2777 | ||
880f421f UD |
2778 | @comment stdlib.h |
2779 | @comment SVID, Unix98 | |
0ea5db4f | 2780 | @deftypefun {char *} fcvt (double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg}) |
b719dafd | 2781 | @safety{@prelim{}@mtunsafe{@mtasurace{:fcvt}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}} |
7a68c94a UD |
2782 | The function @code{fcvt} is like @code{ecvt}, but @var{ndigit} specifies |
2783 | the number of digits after the decimal point. If @var{ndigit} is less | |
2784 | than zero, @var{value} is rounded to the @math{@var{ndigit}+1}'th place to the | |
2785 | left of the decimal point. For example, if @var{ndigit} is @code{-1}, | |
2786 | @var{value} will be rounded to the nearest 10. If @var{ndigit} is | |
2787 | negative and larger than the number of digits to the left of the decimal | |
2788 | point in @var{value}, @var{value} will be rounded to one significant digit. | |
880f421f | 2789 | |
67994d6f UD |
2790 | If @var{ndigit} decimal digits would exceed the precision of a |
2791 | @code{double} it is reduced to a system-specific value. | |
2792 | ||
880f421f UD |
2793 | The returned string is statically allocated and overwritten by each call |
2794 | to @code{fcvt}. | |
880f421f UD |
2795 | @end deftypefun |
2796 | ||
2797 | @comment stdlib.h | |
2798 | @comment SVID, Unix98 | |
2799 | @deftypefun {char *} gcvt (double @var{value}, int @var{ndigit}, char *@var{buf}) | |
b719dafd AO |
2800 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
2801 | @c gcvt calls sprintf, that ultimately calls vfprintf, which malloc()s | |
2802 | @c args_value if it's too large, but gcvt never exercises this path. | |
7a68c94a UD |
2803 | @code{gcvt} is functionally equivalent to @samp{sprintf(buf, "%*g", |
2804 | ndigit, value}. It is provided only for compatibility's sake. It | |
2805 | returns @var{buf}. | |
67994d6f UD |
2806 | |
2807 | If @var{ndigit} decimal digits would exceed the precision of a | |
2808 | @code{double} it is reduced to a system-specific value. | |
880f421f UD |
2809 | @end deftypefun |
2810 | ||
1f77f049 | 2811 | As extensions, @theglibc{} provides versions of these three |
7a68c94a | 2812 | functions that take @code{long double} arguments. |
880f421f UD |
2813 | |
2814 | @comment stdlib.h | |
2815 | @comment GNU | |
7a68c94a | 2816 | @deftypefun {char *} qecvt (long double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg}) |
b719dafd | 2817 | @safety{@prelim{}@mtunsafe{@mtasurace{:qecvt}}@asunsafe{}@acsafe{}} |
67994d6f UD |
2818 | This function is equivalent to @code{ecvt} except that it takes a |
2819 | @code{long double} for the first parameter and that @var{ndigit} is | |
2820 | restricted by the precision of a @code{long double}. | |
880f421f UD |
2821 | @end deftypefun |
2822 | ||
2823 | @comment stdlib.h | |
2824 | @comment GNU | |
0ea5db4f | 2825 | @deftypefun {char *} qfcvt (long double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg}) |
b719dafd | 2826 | @safety{@prelim{}@mtunsafe{@mtasurace{:qfcvt}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}} |
7a68c94a | 2827 | This function is equivalent to @code{fcvt} except that it |
67994d6f UD |
2828 | takes a @code{long double} for the first parameter and that @var{ndigit} is |
2829 | restricted by the precision of a @code{long double}. | |
880f421f UD |
2830 | @end deftypefun |
2831 | ||
2832 | @comment stdlib.h | |
2833 | @comment GNU | |
2834 | @deftypefun {char *} qgcvt (long double @var{value}, int @var{ndigit}, char *@var{buf}) | |
b719dafd | 2835 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
67994d6f UD |
2836 | This function is equivalent to @code{gcvt} except that it takes a |
2837 | @code{long double} for the first parameter and that @var{ndigit} is | |
2838 | restricted by the precision of a @code{long double}. | |
880f421f UD |
2839 | @end deftypefun |
2840 | ||
2841 | ||
2842 | @cindex gcvt_r | |
7a68c94a UD |
2843 | The @code{ecvt} and @code{fcvt} functions, and their @code{long double} |
2844 | equivalents, all return a string located in a static buffer which is | |
1f77f049 | 2845 | overwritten by the next call to the function. @Theglibc{} |
7a68c94a UD |
2846 | provides another set of extended functions which write the converted |
2847 | string into a user-supplied buffer. These have the conventional | |
2848 | @code{_r} suffix. | |
2849 | ||
2850 | @code{gcvt_r} is not necessary, because @code{gcvt} already uses a | |
2851 | user-supplied buffer. | |
880f421f UD |
2852 | |
2853 | @comment stdlib.h | |
2854 | @comment GNU | |
5c1c368f | 2855 | @deftypefun int ecvt_r (double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg}, char *@var{buf}, size_t @var{len}) |
b719dafd | 2856 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
2857 | The @code{ecvt_r} function is the same as @code{ecvt}, except |
2858 | that it places its result into the user-specified buffer pointed to by | |
5c1c368f UD |
2859 | @var{buf}, with length @var{len}. The return value is @code{-1} in |
2860 | case of an error and zero otherwise. | |
880f421f | 2861 | |
7a68c94a | 2862 | This function is a GNU extension. |
880f421f UD |
2863 | @end deftypefun |
2864 | ||
2865 | @comment stdlib.h | |
2866 | @comment SVID, Unix98 | |
5c1c368f | 2867 | @deftypefun int fcvt_r (double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg}, char *@var{buf}, size_t @var{len}) |
b719dafd | 2868 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
5c1c368f UD |
2869 | The @code{fcvt_r} function is the same as @code{fcvt}, except that it |
2870 | places its result into the user-specified buffer pointed to by | |
2871 | @var{buf}, with length @var{len}. The return value is @code{-1} in | |
2872 | case of an error and zero otherwise. | |
880f421f | 2873 | |
7a68c94a | 2874 | This function is a GNU extension. |
880f421f UD |
2875 | @end deftypefun |
2876 | ||
2877 | @comment stdlib.h | |
2878 | @comment GNU | |
5c1c368f | 2879 | @deftypefun int qecvt_r (long double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg}, char *@var{buf}, size_t @var{len}) |
b719dafd | 2880 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
2881 | The @code{qecvt_r} function is the same as @code{qecvt}, except |
2882 | that it places its result into the user-specified buffer pointed to by | |
5c1c368f UD |
2883 | @var{buf}, with length @var{len}. The return value is @code{-1} in |
2884 | case of an error and zero otherwise. | |
880f421f | 2885 | |
7a68c94a | 2886 | This function is a GNU extension. |
880f421f UD |
2887 | @end deftypefun |
2888 | ||
2889 | @comment stdlib.h | |
2890 | @comment GNU | |
5c1c368f | 2891 | @deftypefun int qfcvt_r (long double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg}, char *@var{buf}, size_t @var{len}) |
b719dafd | 2892 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} |
7a68c94a UD |
2893 | The @code{qfcvt_r} function is the same as @code{qfcvt}, except |
2894 | that it places its result into the user-specified buffer pointed to by | |
5c1c368f UD |
2895 | @var{buf}, with length @var{len}. The return value is @code{-1} in |
2896 | case of an error and zero otherwise. | |
880f421f | 2897 | |
7a68c94a | 2898 | This function is a GNU extension. |
880f421f | 2899 | @end deftypefun |