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