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