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