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