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