Remove "[Add new features here]" for 2.27
[glibc.git] / manual / arith.texi
blob28a0e134d5fee6f291cd5f08d94de83898c4e9ea
1 @node Arithmetic, Date and Time, Mathematics, Top
2 @c %MENU% Low level arithmetic functions
3 @chapter Arithmetic Functions
5 This chapter contains information about functions for doing basic
6 arithmetic operations, such as splitting a float into its integer and
7 fractional parts or retrieving the imaginary part of a complex value.
8 These functions are declared in the header files @file{math.h} and
9 @file{complex.h}.
11 @menu
12 * Integers::                    Basic integer types and concepts
13 * Integer Division::            Integer division with guaranteed rounding.
14 * Floating Point Numbers::      Basic concepts.  IEEE 754.
15 * Floating Point Classes::      The five kinds of floating-point number.
16 * Floating Point Errors::       When something goes wrong in a calculation.
17 * Rounding::                    Controlling how results are rounded.
18 * Control Functions::           Saving and restoring the FPU's state.
19 * Arithmetic Functions::        Fundamental operations provided by the library.
20 * Complex Numbers::             The types.  Writing complex constants.
21 * Operations on Complex::       Projection, conjugation, decomposition.
22 * Parsing of Numbers::          Converting strings to numbers.
23 * 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.  Similiarly, there
115 are macros such as @code{INTMAX_WIDTH} for the width of these types.
116 Those macros for integer type widths come from TS 18661-1:2014.
117 @cindex maximum possible integer
118 @cindex minimum possible integer
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 finite or a ``not a
471 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 @findex matherr
518 In the System V math library, the user-defined function @code{matherr}
519 is called when certain exceptions occur inside math library functions.
520 However, the Unix98 standard deprecates this interface.  We support it
521 for historical compatibility, but recommend that you do not use it in
522 new programs.  When this interface is used, exceptions may not be
523 raised.
525 @noindent
526 The exceptions defined in @w{IEEE 754} are:
528 @table @samp
529 @item Invalid Operation
530 This exception is raised if the given operands are invalid for the
531 operation to be performed.  Examples are
532 (see @w{IEEE 754}, @w{section 7}):
533 @enumerate
534 @item
535 Addition or subtraction: @math{@infinity{} - @infinity{}}.  (But
536 @math{@infinity{} + @infinity{} = @infinity{}}).
537 @item
538 Multiplication: @math{0 @mul{} @infinity{}}.
539 @item
540 Division: @math{0/0} or @math{@infinity{}/@infinity{}}.
541 @item
542 Remainder: @math{x} REM @math{y}, where @math{y} is zero or @math{x} is
543 infinite.
544 @item
545 Square root if the operand is less than zero.  More generally, any
546 mathematical function evaluated outside its domain produces this
547 exception.
548 @item
549 Conversion of a floating-point number to an integer or decimal
550 string, when the number cannot be represented in the target format (due
551 to overflow, infinity, or NaN).
552 @item
553 Conversion of an unrecognizable input string.
554 @item
555 Comparison via predicates involving @math{<} or @math{>}, when one or
556 other of the operands is NaN.  You can prevent this exception by using
557 the unordered comparison functions instead; see @ref{FP Comparison Functions}.
558 @end enumerate
560 If the exception does not trap, the result of the operation is NaN.
562 @item Division by Zero
563 This exception is raised when a finite nonzero number is divided
564 by zero.  If no trap occurs the result is either @math{+@infinity{}} or
565 @math{-@infinity{}}, depending on the signs of the operands.
567 @item Overflow
568 This exception is raised whenever the result cannot be represented
569 as a finite value in the precision format of the destination.  If no trap
570 occurs the result depends on the sign of the intermediate result and the
571 current rounding mode (@w{IEEE 754}, @w{section 7.3}):
572 @enumerate
573 @item
574 Round to nearest carries all overflows to @math{@infinity{}}
575 with the sign of the intermediate result.
576 @item
577 Round toward @math{0} carries all overflows to the largest representable
578 finite number with the sign of the intermediate result.
579 @item
580 Round toward @math{-@infinity{}} carries positive overflows to the
581 largest representable finite number and negative overflows to
582 @math{-@infinity{}}.
584 @item
585 Round toward @math{@infinity{}} carries negative overflows to the
586 most negative representable finite number and positive overflows
587 to @math{@infinity{}}.
588 @end enumerate
590 Whenever the overflow exception is raised, the inexact exception is also
591 raised.
593 @item Underflow
594 The underflow exception is raised when an intermediate result is too
595 small to be calculated accurately, or if the operation's result rounded
596 to the destination precision is too small to be normalized.
598 When no trap is installed for the underflow exception, underflow is
599 signaled (via the underflow flag) only when both tininess and loss of
600 accuracy have been detected.  If no trap handler is installed the
601 operation continues with an imprecise small value, or zero if the
602 destination precision cannot hold the small exact result.
604 @item Inexact
605 This exception is signalled if a rounded result is not exact (such as
606 when calculating the square root of two) or a result overflows without
607 an overflow trap.
608 @end table
610 @node Infinity and NaN
611 @subsection Infinity and NaN
612 @cindex infinity
613 @cindex not a number
614 @cindex NaN
616 @w{IEEE 754} floating point numbers can represent positive or negative
617 infinity, and @dfn{NaN} (not a number).  These three values arise from
618 calculations whose result is undefined or cannot be represented
619 accurately.  You can also deliberately set a floating-point variable to
620 any of them, which is sometimes useful.  Some examples of calculations
621 that produce infinity or NaN:
623 @ifnottex
624 @smallexample
625 @math{1/0 = @infinity{}}
626 @math{log (0) = -@infinity{}}
627 @math{sqrt (-1) = NaN}
628 @end smallexample
629 @end ifnottex
630 @tex
631 $${1\over0} = \infty$$
632 $$\log 0 = -\infty$$
633 $$\sqrt{-1} = \hbox{NaN}$$
634 @end tex
636 When a calculation produces any of these values, an exception also
637 occurs; see @ref{FP Exceptions}.
639 The basic operations and math functions all accept infinity and NaN and
640 produce sensible output.  Infinities propagate through calculations as
641 one would expect: for example, @math{2 + @infinity{} = @infinity{}},
642 @math{4/@infinity{} = 0}, atan @math{(@infinity{}) = @pi{}/2}.  NaN, on
643 the other hand, infects any calculation that involves it.  Unless the
644 calculation would produce the same result no matter what real value
645 replaced NaN, the result is NaN.
647 In comparison operations, positive infinity is larger than all values
648 except itself and NaN, and negative infinity is smaller than all values
649 except itself and NaN.  NaN is @dfn{unordered}: it is not equal to,
650 greater than, or less than anything, @emph{including itself}. @code{x ==
651 x} is false if the value of @code{x} is NaN.  You can use this to test
652 whether a value is NaN or not, but the recommended way to test for NaN
653 is with the @code{isnan} function (@pxref{Floating Point Classes}).  In
654 addition, @code{<}, @code{>}, @code{<=}, and @code{>=} will raise an
655 exception when applied to NaNs.
657 @file{math.h} defines macros that allow you to explicitly set a variable
658 to infinity or NaN.
660 @deftypevr Macro float INFINITY
661 @standards{ISO, math.h}
662 An expression representing positive infinity.  It is equal to the value
663 produced  by mathematical operations like @code{1.0 / 0.0}.
664 @code{-INFINITY} represents negative infinity.
666 You can test whether a floating-point value is infinite by comparing it
667 to this macro.  However, this is not recommended; you should use the
668 @code{isfinite} macro instead.  @xref{Floating Point Classes}.
670 This macro was introduced in the @w{ISO C99} standard.
671 @end deftypevr
673 @deftypevr Macro float NAN
674 @standards{GNU, math.h}
675 An expression representing a value which is ``not a number''.  This
676 macro is a GNU extension, available only on machines that support the
677 ``not a number'' value---that is to say, on all machines that support
678 IEEE floating point.
680 You can use @samp{#ifdef NAN} to test whether the machine supports
681 NaN.  (Of course, you must arrange for GNU extensions to be visible,
682 such as by defining @code{_GNU_SOURCE}, and then you must include
683 @file{math.h}.)
684 @end deftypevr
686 @deftypevr Macro float SNANF
687 @deftypevrx Macro double SNAN
688 @deftypevrx Macro {long double} SNANL
689 @deftypevrx Macro _FloatN SNANFN
690 @deftypevrx Macro _FloatNx SNANFNx
691 @standards{TS 18661-1:2014, math.h}
692 @standardsx{SNANFN, TS 18661-3:2015, math.h}
693 @standardsx{SNANFNx, TS 18661-3:2015, math.h}
694 These macros, defined by TS 18661-1:2014 and TS 18661-3:2015, are
695 constant expressions for signaling NaNs.
696 @end deftypevr
698 @deftypevr Macro int FE_SNANS_ALWAYS_SIGNAL
699 @standards{ISO, fenv.h}
700 This macro, defined by TS 18661-1:2014, is defined to @code{1} in
701 @file{fenv.h} to indicate that functions and operations with signaling
702 NaN inputs and floating-point results always raise the invalid
703 exception and return a quiet NaN, even in cases (such as @code{fmax},
704 @code{hypot} and @code{pow}) where a quiet NaN input can produce a
705 non-NaN result.  Because some compiler optimizations may not handle
706 signaling NaNs correctly, this macro is only defined if compiler
707 support for signaling NaNs is enabled.  That support can be enabled
708 with the GCC option @option{-fsignaling-nans}.
709 @end deftypevr
711 @w{IEEE 754} also allows for another unusual value: negative zero.  This
712 value is produced when you divide a positive number by negative
713 infinity, or when a negative result is smaller than the limits of
714 representation.
716 @node Status bit operations
717 @subsection Examining the FPU status word
719 @w{ISO C99} defines functions to query and manipulate the
720 floating-point status word.  You can use these functions to check for
721 untrapped exceptions when it's convenient, rather than worrying about
722 them in the middle of a calculation.
724 These constants represent the various @w{IEEE 754} exceptions.  Not all
725 FPUs report all the different exceptions.  Each constant is defined if
726 and only if the FPU you are compiling for supports that exception, so
727 you can test for FPU support with @samp{#ifdef}.  They are defined in
728 @file{fenv.h}.
730 @vtable @code
731 @item FE_INEXACT
732 @standards{ISO, fenv.h}
733  The inexact exception.
734 @item FE_DIVBYZERO
735 @standards{ISO, fenv.h}
736  The divide by zero exception.
737 @item FE_UNDERFLOW
738 @standards{ISO, fenv.h}
739  The underflow exception.
740 @item FE_OVERFLOW
741 @standards{ISO, fenv.h}
742  The overflow exception.
743 @item FE_INVALID
744 @standards{ISO, fenv.h}
745  The invalid exception.
746 @end vtable
748 The macro @code{FE_ALL_EXCEPT} is the bitwise OR of all exception macros
749 which are supported by the FP implementation.
751 These functions allow you to clear exception flags, test for exceptions,
752 and save and restore the set of exceptions flagged.
754 @deftypefun int feclearexcept (int @var{excepts})
755 @standards{ISO, fenv.h}
756 @safety{@prelim{}@mtsafe{}@assafe{@assposix{}}@acsafe{@acsposix{}}}
757 @c The other functions in this section that modify FP status register
758 @c mostly do so with non-atomic load-modify-store sequences, but since
759 @c the register is thread-specific, this should be fine, and safe for
760 @c cancellation.  As long as the FP environment is restored before the
761 @c signal handler returns control to the interrupted thread (like any
762 @c kernel should do), the functions are also safe for use in signal
763 @c handlers.
764 This function clears all of the supported exception flags indicated by
765 @var{excepts}.
767 The function returns zero in case the operation was successful, a
768 non-zero value otherwise.
769 @end deftypefun
771 @deftypefun int feraiseexcept (int @var{excepts})
772 @standards{ISO, fenv.h}
773 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
774 This function raises the supported exceptions indicated by
775 @var{excepts}.  If more than one exception bit in @var{excepts} is set
776 the order in which the exceptions are raised is undefined except that
777 overflow (@code{FE_OVERFLOW}) or underflow (@code{FE_UNDERFLOW}) are
778 raised before inexact (@code{FE_INEXACT}).  Whether for overflow or
779 underflow the inexact exception is also raised is also implementation
780 dependent.
782 The function returns zero in case the operation was successful, a
783 non-zero value otherwise.
784 @end deftypefun
786 @deftypefun int fesetexcept (int @var{excepts})
787 @standards{ISO, fenv.h}
788 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
789 This function sets the supported exception flags indicated by
790 @var{excepts}, like @code{feraiseexcept}, but without causing enabled
791 traps to be taken.  @code{fesetexcept} is from TS 18661-1:2014.
793 The function returns zero in case the operation was successful, a
794 non-zero value otherwise.
795 @end deftypefun
797 @deftypefun int fetestexcept (int @var{excepts})
798 @standards{ISO, fenv.h}
799 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
800 Test whether the exception flags indicated by the parameter @var{except}
801 are currently set.  If any of them are, a nonzero value is returned
802 which specifies which exceptions are set.  Otherwise the result is zero.
803 @end deftypefun
805 To understand these functions, imagine that the status word is an
806 integer variable named @var{status}.  @code{feclearexcept} is then
807 equivalent to @samp{status &= ~excepts} and @code{fetestexcept} is
808 equivalent to @samp{(status & excepts)}.  The actual implementation may
809 be very different, of course.
811 Exception flags are only cleared when the program explicitly requests it,
812 by calling @code{feclearexcept}.  If you want to check for exceptions
813 from a set of calculations, you should clear all the flags first.  Here
814 is a simple example of the way to use @code{fetestexcept}:
816 @smallexample
818   double f;
819   int raised;
820   feclearexcept (FE_ALL_EXCEPT);
821   f = compute ();
822   raised = fetestexcept (FE_OVERFLOW | FE_INVALID);
823   if (raised & FE_OVERFLOW) @{ /* @dots{} */ @}
824   if (raised & FE_INVALID) @{ /* @dots{} */ @}
825   /* @dots{} */
827 @end smallexample
829 You cannot explicitly set bits in the status word.  You can, however,
830 save the entire status word and restore it later.  This is done with the
831 following functions:
833 @deftypefun int fegetexceptflag (fexcept_t *@var{flagp}, int @var{excepts})
834 @standards{ISO, fenv.h}
835 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
836 This function stores in the variable pointed to by @var{flagp} an
837 implementation-defined value representing the current setting of the
838 exception flags indicated by @var{excepts}.
840 The function returns zero in case the operation was successful, a
841 non-zero value otherwise.
842 @end deftypefun
844 @deftypefun int fesetexceptflag (const fexcept_t *@var{flagp}, int @var{excepts})
845 @standards{ISO, fenv.h}
846 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
847 This function restores the flags for the exceptions indicated by
848 @var{excepts} to the values stored in the variable pointed to by
849 @var{flagp}.
851 The function returns zero in case the operation was successful, a
852 non-zero value otherwise.
853 @end deftypefun
855 Note that the value stored in @code{fexcept_t} bears no resemblance to
856 the bit mask returned by @code{fetestexcept}.  The type may not even be
857 an integer.  Do not attempt to modify an @code{fexcept_t} variable.
859 @deftypefun int fetestexceptflag (const fexcept_t *@var{flagp}, int @var{excepts})
860 @standards{ISO, fenv.h}
861 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
862 Test whether the exception flags indicated by the parameter
863 @var{excepts} are set in the variable pointed to by @var{flagp}.  If
864 any of them are, a nonzero value is returned which specifies which
865 exceptions are set.  Otherwise the result is zero.
866 @code{fetestexceptflag} is from TS 18661-1:2014.
867 @end deftypefun
869 @node Math Error Reporting
870 @subsection Error Reporting by Mathematical Functions
871 @cindex errors, mathematical
872 @cindex domain error
873 @cindex range error
875 Many of the math functions are defined only over a subset of the real or
876 complex numbers.  Even if they are mathematically defined, their result
877 may be larger or smaller than the range representable by their return
878 type without loss of accuracy.  These are known as @dfn{domain errors},
879 @dfn{overflows}, and
880 @dfn{underflows}, respectively.  Math functions do several things when
881 one of these errors occurs.  In this manual we will refer to the
882 complete response as @dfn{signalling} a domain error, overflow, or
883 underflow.
885 When a math function suffers a domain error, it raises the invalid
886 exception and returns NaN.  It also sets @var{errno} to @code{EDOM};
887 this is for compatibility with old systems that do not support @w{IEEE
888 754} exception handling.  Likewise, when overflow occurs, math
889 functions raise the overflow exception and, in the default rounding
890 mode, return @math{@infinity{}} or @math{-@infinity{}} as appropriate
891 (in other rounding modes, the largest finite value of the appropriate
892 sign is returned when appropriate for that rounding mode).  They also
893 set @var{errno} to @code{ERANGE} if returning @math{@infinity{}} or
894 @math{-@infinity{}}; @var{errno} may or may not be set to
895 @code{ERANGE} when a finite value is returned on overflow.  When
896 underflow occurs, the underflow exception is raised, and zero
897 (appropriately signed) or a subnormal value, as appropriate for the
898 mathematical result of the function and the rounding mode, is
899 returned.  @var{errno} may be set to @code{ERANGE}, but this is not
900 guaranteed; it is intended that @theglibc{} should set it when the
901 underflow is to an appropriately signed zero, but not necessarily for
902 other underflows.
904 When a math function has an argument that is a signaling NaN,
905 @theglibc{} does not consider this a domain error, so @code{errno} is
906 unchanged, but the invalid exception is still raised (except for a few
907 functions that are specified to handle signaling NaNs differently).
909 Some of the math functions are defined mathematically to result in a
910 complex value over parts of their domains.  The most familiar example of
911 this is taking the square root of a negative number.  The complex math
912 functions, such as @code{csqrt}, will return the appropriate complex value
913 in this case.  The real-valued functions, such as @code{sqrt}, will
914 signal a domain error.
916 Some older hardware does not support infinities.  On that hardware,
917 overflows instead return a particular very large number (usually the
918 largest representable number).  @file{math.h} defines macros you can use
919 to test for overflow on both old and new hardware.
921 @deftypevr Macro double HUGE_VAL
922 @deftypevrx Macro float HUGE_VALF
923 @deftypevrx Macro {long double} HUGE_VALL
924 @deftypevrx Macro _FloatN HUGE_VAL_FN
925 @deftypevrx Macro _FloatNx HUGE_VAL_FNx
926 @standards{ISO, math.h}
927 @standardsx{HUGE_VAL_FN, TS 18661-3:2015, math.h}
928 @standardsx{HUGE_VAL_FNx, TS 18661-3:2015, math.h}
929 An expression representing a particular very large number.  On machines
930 that use @w{IEEE 754} floating point format, @code{HUGE_VAL} is infinity.
931 On other machines, it's typically the largest positive number that can
932 be represented.
934 Mathematical functions return the appropriately typed version of
935 @code{HUGE_VAL} or @code{@minus{}HUGE_VAL} when the result is too large
936 to be represented.
937 @end deftypevr
939 @node Rounding
940 @section Rounding Modes
942 Floating-point calculations are carried out internally with extra
943 precision, and then rounded to fit into the destination type.  This
944 ensures that results are as precise as the input data.  @w{IEEE 754}
945 defines four possible rounding modes:
947 @table @asis
948 @item Round to nearest.
949 This is the default mode.  It should be used unless there is a specific
950 need for one of the others.  In this mode results are rounded to the
951 nearest representable value.  If the result is midway between two
952 representable values, the even representable is chosen. @dfn{Even} here
953 means the lowest-order bit is zero.  This rounding mode prevents
954 statistical bias and guarantees numeric stability: round-off errors in a
955 lengthy calculation will remain smaller than half of @code{FLT_EPSILON}.
957 @c @item Round toward @math{+@infinity{}}
958 @item Round toward plus Infinity.
959 All results are rounded to the smallest representable value
960 which is greater than the result.
962 @c @item Round toward @math{-@infinity{}}
963 @item Round toward minus Infinity.
964 All results are rounded to the largest representable value which is less
965 than the result.
967 @item Round toward zero.
968 All results are rounded to the largest representable value whose
969 magnitude is less than that of the result.  In other words, if the
970 result is negative it is rounded up; if it is positive, it is rounded
971 down.
972 @end table
974 @noindent
975 @file{fenv.h} defines constants which you can use to refer to the
976 various rounding modes.  Each one will be defined if and only if the FPU
977 supports the corresponding rounding mode.
979 @vtable @code
980 @item FE_TONEAREST
981 @standards{ISO, fenv.h}
982 Round to nearest.
984 @item FE_UPWARD
985 @standards{ISO, fenv.h}
986 Round toward @math{+@infinity{}}.
988 @item FE_DOWNWARD
989 @standards{ISO, fenv.h}
990 Round toward @math{-@infinity{}}.
992 @item FE_TOWARDZERO
993 @standards{ISO, fenv.h}
994 Round toward zero.
995 @end vtable
997 Underflow is an unusual case.  Normally, @w{IEEE 754} floating point
998 numbers are always normalized (@pxref{Floating Point Concepts}).
999 Numbers smaller than @math{2^r} (where @math{r} is the minimum exponent,
1000 @code{FLT_MIN_RADIX-1} for @var{float}) cannot be represented as
1001 normalized numbers.  Rounding all such numbers to zero or @math{2^r}
1002 would cause some algorithms to fail at 0.  Therefore, they are left in
1003 denormalized form.  That produces loss of precision, since some bits of
1004 the mantissa are stolen to indicate the decimal point.
1006 If a result is too small to be represented as a denormalized number, it
1007 is rounded to zero.  However, the sign of the result is preserved; if
1008 the calculation was negative, the result is @dfn{negative zero}.
1009 Negative zero can also result from some operations on infinity, such as
1010 @math{4/-@infinity{}}.
1012 At any time, one of the above four rounding modes is selected.  You can
1013 find out which one with this function:
1015 @deftypefun int fegetround (void)
1016 @standards{ISO, fenv.h}
1017 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1018 Returns the currently selected rounding mode, represented by one of the
1019 values of the defined rounding mode macros.
1020 @end deftypefun
1022 @noindent
1023 To change the rounding mode, use this function:
1025 @deftypefun int fesetround (int @var{round})
1026 @standards{ISO, fenv.h}
1027 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1028 Changes the currently selected rounding mode to @var{round}.  If
1029 @var{round} does not correspond to one of the supported rounding modes
1030 nothing is changed.  @code{fesetround} returns zero if it changed the
1031 rounding mode, or a nonzero value if the mode is not supported.
1032 @end deftypefun
1034 You should avoid changing the rounding mode if possible.  It can be an
1035 expensive operation; also, some hardware requires you to compile your
1036 program differently for it to work.  The resulting code may run slower.
1037 See your compiler documentation for details.
1038 @c This section used to claim that functions existed to round one number
1039 @c in a specific fashion.  I can't find any functions in the library
1040 @c that do that. -zw
1042 @node Control Functions
1043 @section Floating-Point Control Functions
1045 @w{IEEE 754} floating-point implementations allow the programmer to
1046 decide whether traps will occur for each of the exceptions, by setting
1047 bits in the @dfn{control word}.  In C, traps result in the program
1048 receiving the @code{SIGFPE} signal; see @ref{Signal Handling}.
1050 @strong{NB:} @w{IEEE 754} says that trap handlers are given details of
1051 the exceptional situation, and can set the result value.  C signals do
1052 not provide any mechanism to pass this information back and forth.
1053 Trapping exceptions in C is therefore not very useful.
1055 It is sometimes necessary to save the state of the floating-point unit
1056 while you perform some calculation.  The library provides functions
1057 which save and restore the exception flags, the set of exceptions that
1058 generate traps, and the rounding mode.  This information is known as the
1059 @dfn{floating-point environment}.
1061 The functions to save and restore the floating-point environment all use
1062 a variable of type @code{fenv_t} to store information.  This type is
1063 defined in @file{fenv.h}.  Its size and contents are
1064 implementation-defined.  You should not attempt to manipulate a variable
1065 of this type directly.
1067 To save the state of the FPU, use one of these functions:
1069 @deftypefun int fegetenv (fenv_t *@var{envp})
1070 @standards{ISO, fenv.h}
1071 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1072 Store the floating-point environment in the variable pointed to by
1073 @var{envp}.
1075 The function returns zero in case the operation was successful, a
1076 non-zero value otherwise.
1077 @end deftypefun
1079 @deftypefun int feholdexcept (fenv_t *@var{envp})
1080 @standards{ISO, fenv.h}
1081 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1082 Store the current floating-point environment in the object pointed to by
1083 @var{envp}.  Then clear all exception flags, and set the FPU to trap no
1084 exceptions.  Not all FPUs support trapping no exceptions; if
1085 @code{feholdexcept} cannot set this mode, it returns nonzero value.  If it
1086 succeeds, it returns zero.
1087 @end deftypefun
1089 The functions which restore the floating-point environment can take these
1090 kinds of arguments:
1092 @itemize @bullet
1093 @item
1094 Pointers to @code{fenv_t} objects, which were initialized previously by a
1095 call to @code{fegetenv} or @code{feholdexcept}.
1096 @item
1097 @vindex FE_DFL_ENV
1098 The special macro @code{FE_DFL_ENV} which represents the floating-point
1099 environment as it was available at program start.
1100 @item
1101 Implementation defined macros with names starting with @code{FE_} and
1102 having type @code{fenv_t *}.
1104 @vindex FE_NOMASK_ENV
1105 If possible, @theglibc{} defines a macro @code{FE_NOMASK_ENV}
1106 which represents an environment where every exception raised causes a
1107 trap to occur.  You can test for this macro using @code{#ifdef}.  It is
1108 only defined if @code{_GNU_SOURCE} is defined.
1110 Some platforms might define other predefined environments.
1111 @end itemize
1113 @noindent
1114 To set the floating-point environment, you can use either of these
1115 functions:
1117 @deftypefun int fesetenv (const fenv_t *@var{envp})
1118 @standards{ISO, fenv.h}
1119 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1120 Set the floating-point environment to that described by @var{envp}.
1122 The function returns zero in case the operation was successful, a
1123 non-zero value otherwise.
1124 @end deftypefun
1126 @deftypefun int feupdateenv (const fenv_t *@var{envp})
1127 @standards{ISO, fenv.h}
1128 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1129 Like @code{fesetenv}, this function sets the floating-point environment
1130 to that described by @var{envp}.  However, if any exceptions were
1131 flagged in the status word before @code{feupdateenv} was called, they
1132 remain flagged after the call.  In other words, after @code{feupdateenv}
1133 is called, the status word is the bitwise OR of the previous status word
1134 and the one saved in @var{envp}.
1136 The function returns zero in case the operation was successful, a
1137 non-zero value otherwise.
1138 @end deftypefun
1140 @noindent
1141 TS 18661-1:2014 defines additional functions to save and restore
1142 floating-point control modes (such as the rounding mode and whether
1143 traps are enabled) while leaving other status (such as raised flags)
1144 unchanged.
1146 @vindex FE_DFL_MODE
1147 The special macro @code{FE_DFL_MODE} may be passed to
1148 @code{fesetmode}.  It represents the floating-point control modes at
1149 program start.
1151 @deftypefun int fegetmode (femode_t *@var{modep})
1152 @standards{ISO, fenv.h}
1153 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1154 Store the floating-point control modes in the variable pointed to by
1155 @var{modep}.
1157 The function returns zero in case the operation was successful, a
1158 non-zero value otherwise.
1159 @end deftypefun
1161 @deftypefun int fesetmode (const femode_t *@var{modep})
1162 @standards{ISO, fenv.h}
1163 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1164 Set the floating-point control modes to those described by
1165 @var{modep}.
1167 The function returns zero in case the operation was successful, a
1168 non-zero value otherwise.
1169 @end deftypefun
1171 @noindent
1172 To control for individual exceptions if raising them causes a trap to
1173 occur, you can use the following two functions.
1175 @strong{Portability Note:} These functions are all GNU extensions.
1177 @deftypefun int feenableexcept (int @var{excepts})
1178 @standards{GNU, fenv.h}
1179 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1180 This function enables traps for each of the exceptions as indicated by
1181 the parameter @var{excepts}.  The individual exceptions are described in
1182 @ref{Status bit operations}.  Only the specified exceptions are
1183 enabled, the status of the other exceptions is not changed.
1185 The function returns the previous enabled exceptions in case the
1186 operation was successful, @code{-1} otherwise.
1187 @end deftypefun
1189 @deftypefun int fedisableexcept (int @var{excepts})
1190 @standards{GNU, fenv.h}
1191 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1192 This function disables traps for each of the exceptions as indicated by
1193 the parameter @var{excepts}.  The individual exceptions are described in
1194 @ref{Status bit operations}.  Only the specified exceptions are
1195 disabled, the status of the other exceptions is not changed.
1197 The function returns the previous enabled exceptions in case the
1198 operation was successful, @code{-1} otherwise.
1199 @end deftypefun
1201 @deftypefun int fegetexcept (void)
1202 @standards{GNU, fenv.h}
1203 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1204 The function returns a bitmask of all currently enabled exceptions.  It
1205 returns @code{-1} in case of failure.
1206 @end deftypefun
1208 @node Arithmetic Functions
1209 @section Arithmetic Functions
1211 The C library provides functions to do basic operations on
1212 floating-point numbers.  These include absolute value, maximum and minimum,
1213 normalization, bit twiddling, rounding, and a few others.
1215 @menu
1216 * Absolute Value::              Absolute values of integers and floats.
1217 * Normalization Functions::     Extracting exponents and putting them back.
1218 * Rounding Functions::          Rounding floats to integers.
1219 * Remainder Functions::         Remainders on division, precisely defined.
1220 * FP Bit Twiddling::            Sign bit adjustment.  Adding epsilon.
1221 * FP Comparison Functions::     Comparisons without risk of exceptions.
1222 * Misc FP Arithmetic::          Max, min, positive difference, multiply-add.
1223 @end menu
1225 @node Absolute Value
1226 @subsection Absolute Value
1227 @cindex absolute value functions
1229 These functions are provided for obtaining the @dfn{absolute value} (or
1230 @dfn{magnitude}) of a number.  The absolute value of a real number
1231 @var{x} is @var{x} if @var{x} is positive, @minus{}@var{x} if @var{x} is
1232 negative.  For a complex number @var{z}, whose real part is @var{x} and
1233 whose imaginary part is @var{y}, the absolute value is @w{@code{sqrt
1234 (@var{x}*@var{x} + @var{y}*@var{y})}}.
1236 @pindex math.h
1237 @pindex stdlib.h
1238 Prototypes for @code{abs}, @code{labs} and @code{llabs} are in @file{stdlib.h};
1239 @code{imaxabs} is declared in @file{inttypes.h};
1240 the @code{fabs} functions are declared in @file{math.h};
1241 the @code{cabs} functions are declared in @file{complex.h}.
1243 @deftypefun int abs (int @var{number})
1244 @deftypefunx {long int} labs (long int @var{number})
1245 @deftypefunx {long long int} llabs (long long int @var{number})
1246 @deftypefunx intmax_t imaxabs (intmax_t @var{number})
1247 @standards{ISO, stdlib.h}
1248 @standardsx{imaxabs, ISO, inttypes.h}
1249 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1250 These functions return the absolute value of @var{number}.
1252 Most computers use a two's complement integer representation, in which
1253 the absolute value of @code{INT_MIN} (the smallest possible @code{int})
1254 cannot be represented; thus, @w{@code{abs (INT_MIN)}} is not defined.
1256 @code{llabs} and @code{imaxdiv} are new to @w{ISO C99}.
1258 See @ref{Integers} for a description of the @code{intmax_t} type.
1260 @end deftypefun
1262 @deftypefun double fabs (double @var{number})
1263 @deftypefunx float fabsf (float @var{number})
1264 @deftypefunx {long double} fabsl (long double @var{number})
1265 @deftypefunx _FloatN fabsfN (_Float@var{N} @var{number})
1266 @deftypefunx _FloatNx fabsfNx (_Float@var{N}x @var{number})
1267 @standards{ISO, math.h}
1268 @standardsx{fabsfN, TS 18661-3:2015, math.h}
1269 @standardsx{fabsfNx, TS 18661-3:2015, math.h}
1270 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1271 This function returns the absolute value of the floating-point number
1272 @var{number}.
1273 @end deftypefun
1275 @deftypefun double cabs (complex double @var{z})
1276 @deftypefunx float cabsf (complex float @var{z})
1277 @deftypefunx {long double} cabsl (complex long double @var{z})
1278 @deftypefunx _FloatN cabsfN (complex _Float@var{N} @var{z})
1279 @deftypefunx _FloatNx cabsfNx (complex _Float@var{N}x @var{z})
1280 @standards{ISO, complex.h}
1281 @standardsx{cabsfN, TS 18661-3:2015, complex.h}
1282 @standardsx{cabsfNx, TS 18661-3:2015, complex.h}
1283 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1284 These functions return the absolute  value of the complex number @var{z}
1285 (@pxref{Complex Numbers}).  The absolute value of a complex number is:
1287 @smallexample
1288 sqrt (creal (@var{z}) * creal (@var{z}) + cimag (@var{z}) * cimag (@var{z}))
1289 @end smallexample
1291 This function should always be used instead of the direct formula
1292 because it takes special care to avoid losing precision.  It may also
1293 take advantage of hardware support for this operation.  See @code{hypot}
1294 in @ref{Exponents and Logarithms}.
1295 @end deftypefun
1297 @node Normalization Functions
1298 @subsection Normalization Functions
1299 @cindex normalization functions (floating-point)
1301 The functions described in this section are primarily provided as a way
1302 to efficiently perform certain low-level manipulations on floating point
1303 numbers that are represented internally using a binary radix;
1304 see @ref{Floating Point Concepts}.  These functions are required to
1305 have equivalent behavior even if the representation does not use a radix
1306 of 2, but of course they are unlikely to be particularly efficient in
1307 those cases.
1309 @pindex math.h
1310 All these functions are declared in @file{math.h}.
1312 @deftypefun double frexp (double @var{value}, int *@var{exponent})
1313 @deftypefunx float frexpf (float @var{value}, int *@var{exponent})
1314 @deftypefunx {long double} frexpl (long double @var{value}, int *@var{exponent})
1315 @deftypefunx _FloatN frexpfN (_Float@var{N} @var{value}, int *@var{exponent})
1316 @deftypefunx _FloatNx frexpfNx (_Float@var{N}x @var{value}, int *@var{exponent})
1317 @standards{ISO, math.h}
1318 @standardsx{frexpfN, TS 18661-3:2015, math.h}
1319 @standardsx{frexpfNx, TS 18661-3:2015, math.h}
1320 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1321 These functions are used to split the number @var{value}
1322 into a normalized fraction and an exponent.
1324 If the argument @var{value} is not zero, the return value is @var{value}
1325 times a power of two, and its magnitude is always in the range 1/2
1326 (inclusive) to 1 (exclusive).  The corresponding exponent is stored in
1327 @code{*@var{exponent}}; the return value multiplied by 2 raised to this
1328 exponent equals the original number @var{value}.
1330 For example, @code{frexp (12.8, &exponent)} returns @code{0.8} and
1331 stores @code{4} in @code{exponent}.
1333 If @var{value} is zero, then the return value is zero and
1334 zero is stored in @code{*@var{exponent}}.
1335 @end deftypefun
1337 @deftypefun double ldexp (double @var{value}, int @var{exponent})
1338 @deftypefunx float ldexpf (float @var{value}, int @var{exponent})
1339 @deftypefunx {long double} ldexpl (long double @var{value}, int @var{exponent})
1340 @deftypefunx _FloatN ldexpfN (_Float@var{N} @var{value}, int @var{exponent})
1341 @deftypefunx _FloatNx ldexpfNx (_Float@var{N}x @var{value}, int @var{exponent})
1342 @standards{ISO, math.h}
1343 @standardsx{ldexpfN, TS 18661-3:2015, math.h}
1344 @standardsx{ldexpfNx, TS 18661-3:2015, math.h}
1345 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1346 These functions return the result of multiplying the floating-point
1347 number @var{value} by 2 raised to the power @var{exponent}.  (It can
1348 be used to reassemble floating-point numbers that were taken apart
1349 by @code{frexp}.)
1351 For example, @code{ldexp (0.8, 4)} returns @code{12.8}.
1352 @end deftypefun
1354 The following functions, which come from BSD, provide facilities
1355 equivalent to those of @code{ldexp} and @code{frexp}.  See also the
1356 @w{ISO C} function @code{logb} which originally also appeared in BSD.
1357 The @code{_Float@var{N}} and @code{_Float@var{N}} variants of the
1358 following functions come from TS 18661-3:2015.
1360 @deftypefun double scalb (double @var{value}, double @var{exponent})
1361 @deftypefunx float scalbf (float @var{value}, float @var{exponent})
1362 @deftypefunx {long double} scalbl (long double @var{value}, long double @var{exponent})
1363 @standards{BSD, math.h}
1364 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1365 The @code{scalb} function is the BSD name for @code{ldexp}.
1366 @end deftypefun
1368 @deftypefun double scalbn (double @var{x}, int @var{n})
1369 @deftypefunx float scalbnf (float @var{x}, int @var{n})
1370 @deftypefunx {long double} scalbnl (long double @var{x}, int @var{n})
1371 @deftypefunx _FloatN scalbnfN (_Float@var{N} @var{x}, int @var{n})
1372 @deftypefunx _FloatNx scalbnfNx (_Float@var{N}x @var{x}, int @var{n})
1373 @standards{BSD, math.h}
1374 @standardsx{scalbnfN, TS 18661-3:2015, math.h}
1375 @standardsx{scalbnfNx, TS 18661-3:2015, math.h}
1376 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1377 @code{scalbn} is identical to @code{scalb}, except that the exponent
1378 @var{n} is an @code{int} instead of a floating-point number.
1379 @end deftypefun
1381 @deftypefun double scalbln (double @var{x}, long int @var{n})
1382 @deftypefunx float scalblnf (float @var{x}, long int @var{n})
1383 @deftypefunx {long double} scalblnl (long double @var{x}, long int @var{n})
1384 @deftypefunx _FloatN scalblnfN (_Float@var{N} @var{x}, long int @var{n})
1385 @deftypefunx _FloatNx scalblnfNx (_Float@var{N}x @var{x}, long int @var{n})
1386 @standards{BSD, math.h}
1387 @standardsx{scalblnfN, TS 18661-3:2015, math.h}
1388 @standardsx{scalblnfNx, TS 18661-3:2015, math.h}
1389 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1390 @code{scalbln} is identical to @code{scalb}, except that the exponent
1391 @var{n} is a @code{long int} instead of a floating-point number.
1392 @end deftypefun
1394 @deftypefun double significand (double @var{x})
1395 @deftypefunx float significandf (float @var{x})
1396 @deftypefunx {long double} significandl (long double @var{x})
1397 @standards{BSD, math.h}
1398 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1399 @code{significand} returns the mantissa of @var{x} scaled to the range
1400 @math{[1, 2)}.
1401 It is equivalent to @w{@code{scalb (@var{x}, (double) -ilogb (@var{x}))}}.
1403 This function exists mainly for use in certain standardized tests
1404 of @w{IEEE 754} conformance.
1405 @end deftypefun
1407 @node Rounding Functions
1408 @subsection Rounding Functions
1409 @cindex converting floats to integers
1411 @pindex math.h
1412 The functions listed here perform operations such as rounding and
1413 truncation of floating-point values.  Some of these functions convert
1414 floating point numbers to integer values.  They are all declared in
1415 @file{math.h}.
1417 You can also convert floating-point numbers to integers simply by
1418 casting them to @code{int}.  This discards the fractional part,
1419 effectively rounding towards zero.  However, this only works if the
1420 result can actually be represented as an @code{int}---for very large
1421 numbers, this is impossible.  The functions listed here return the
1422 result as a @code{double} instead to get around this problem.
1424 The @code{fromfp} functions use the following macros, from TS
1425 18661-1:2014, to specify the direction of rounding.  These correspond
1426 to the rounding directions defined in IEEE 754-2008.
1428 @vtable @code
1429 @item FP_INT_UPWARD
1430 @standards{ISO, math.h}
1431 Round toward @math{+@infinity{}}.
1433 @item FP_INT_DOWNWARD
1434 @standards{ISO, math.h}
1435 Round toward @math{-@infinity{}}.
1437 @item FP_INT_TOWARDZERO
1438 @standards{ISO, math.h}
1439 Round toward zero.
1441 @item FP_INT_TONEARESTFROMZERO
1442 @standards{ISO, math.h}
1443 Round to nearest, ties round away from zero.
1445 @item FP_INT_TONEAREST
1446 @standards{ISO, math.h}
1447 Round to nearest, ties round to even.
1448 @end vtable
1450 @deftypefun double ceil (double @var{x})
1451 @deftypefunx float ceilf (float @var{x})
1452 @deftypefunx {long double} ceill (long double @var{x})
1453 @deftypefunx _FloatN ceilfN (_Float@var{N} @var{x})
1454 @deftypefunx _FloatNx ceilfNx (_Float@var{N}x @var{x})
1455 @standards{ISO, math.h}
1456 @standardsx{ceilfN, TS 18661-3:2015, math.h}
1457 @standardsx{ceilfNx, TS 18661-3:2015, math.h}
1458 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1459 These functions round @var{x} upwards to the nearest integer,
1460 returning that value as a @code{double}.  Thus, @code{ceil (1.5)}
1461 is @code{2.0}.
1462 @end deftypefun
1464 @deftypefun double floor (double @var{x})
1465 @deftypefunx float floorf (float @var{x})
1466 @deftypefunx {long double} floorl (long double @var{x})
1467 @deftypefunx _FloatN floorfN (_Float@var{N} @var{x})
1468 @deftypefunx _FloatNx floorfNx (_Float@var{N}x @var{x})
1469 @standards{ISO, math.h}
1470 @standardsx{floorfN, TS 18661-3:2015, math.h}
1471 @standardsx{floorfNx, TS 18661-3:2015, math.h}
1472 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1473 These functions round @var{x} downwards to the nearest
1474 integer, returning that value as a @code{double}.  Thus, @code{floor
1475 (1.5)} is @code{1.0} and @code{floor (-1.5)} is @code{-2.0}.
1476 @end deftypefun
1478 @deftypefun double trunc (double @var{x})
1479 @deftypefunx float truncf (float @var{x})
1480 @deftypefunx {long double} truncl (long double @var{x})
1481 @deftypefunx _FloatN truncfN (_Float@var{N} @var{x})
1482 @deftypefunx _FloatNx truncfNx (_Float@var{N}x @var{x})
1483 @standards{ISO, math.h}
1484 @standardsx{truncfN, TS 18661-3:2015, math.h}
1485 @standardsx{truncfNx, TS 18661-3:2015, math.h}
1486 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1487 The @code{trunc} functions round @var{x} towards zero to the nearest
1488 integer (returned in floating-point format).  Thus, @code{trunc (1.5)}
1489 is @code{1.0} and @code{trunc (-1.5)} is @code{-1.0}.
1490 @end deftypefun
1492 @deftypefun double rint (double @var{x})
1493 @deftypefunx float rintf (float @var{x})
1494 @deftypefunx {long double} rintl (long double @var{x})
1495 @deftypefunx _FloatN rintfN (_Float@var{N} @var{x})
1496 @deftypefunx _FloatNx rintfNx (_Float@var{N}x @var{x})
1497 @standards{ISO, math.h}
1498 @standardsx{rintfN, TS 18661-3:2015, math.h}
1499 @standardsx{rintfNx, TS 18661-3:2015, math.h}
1500 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1501 These functions round @var{x} to an integer value according to the
1502 current rounding mode.  @xref{Floating Point Parameters}, for
1503 information about the various rounding modes.  The default
1504 rounding mode is to round to the nearest integer; some machines
1505 support other modes, but round-to-nearest is always used unless
1506 you explicitly select another.
1508 If @var{x} was not initially an integer, these functions raise the
1509 inexact exception.
1510 @end deftypefun
1512 @deftypefun double nearbyint (double @var{x})
1513 @deftypefunx float nearbyintf (float @var{x})
1514 @deftypefunx {long double} nearbyintl (long double @var{x})
1515 @deftypefunx _FloatN nearbyintfN (_Float@var{N} @var{x})
1516 @deftypefunx _FloatNx nearbyintfNx (_Float@var{N}x @var{x})
1517 @standards{ISO, math.h}
1518 @standardsx{nearbyintfN, TS 18661-3:2015, math.h}
1519 @standardsx{nearbyintfNx, TS 18661-3:2015, math.h}
1520 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1521 These functions return the same value as the @code{rint} functions, but
1522 do not raise the inexact exception if @var{x} is not an integer.
1523 @end deftypefun
1525 @deftypefun double round (double @var{x})
1526 @deftypefunx float roundf (float @var{x})
1527 @deftypefunx {long double} roundl (long double @var{x})
1528 @deftypefunx _FloatN roundfN (_Float@var{N} @var{x})
1529 @deftypefunx _FloatNx roundfNx (_Float@var{N}x @var{x})
1530 @standards{ISO, math.h}
1531 @standardsx{roundfN, TS 18661-3:2015, math.h}
1532 @standardsx{roundfNx, TS 18661-3:2015, math.h}
1533 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1534 These functions are similar to @code{rint}, but they round halfway
1535 cases away from zero instead of to the nearest integer (or other
1536 current rounding mode).
1537 @end deftypefun
1539 @deftypefun double roundeven (double @var{x})
1540 @deftypefunx float roundevenf (float @var{x})
1541 @deftypefunx {long double} roundevenl (long double @var{x})
1542 @deftypefunx _FloatN roundevenfN (_Float@var{N} @var{x})
1543 @deftypefunx _FloatNx roundevenfNx (_Float@var{N}x @var{x})
1544 @standards{ISO, math.h}
1545 @standardsx{roundevenfN, TS 18661-3:2015, math.h}
1546 @standardsx{roundevenfNx, TS 18661-3:2015, math.h}
1547 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1548 These functions, from TS 18661-1:2014 and TS 18661-3:2015, are similar
1549 to @code{round}, but they round halfway cases to even instead of away
1550 from zero.
1551 @end deftypefun
1553 @deftypefun {long int} lrint (double @var{x})
1554 @deftypefunx {long int} lrintf (float @var{x})
1555 @deftypefunx {long int} lrintl (long double @var{x})
1556 @deftypefunx {long int} lrintfN (_Float@var{N} @var{x})
1557 @deftypefunx {long int} lrintfNx (_Float@var{N}x @var{x})
1558 @standards{ISO, math.h}
1559 @standardsx{lrintfN, TS 18661-3:2015, math.h}
1560 @standardsx{lrintfNx, TS 18661-3:2015, math.h}
1561 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1562 These functions are just like @code{rint}, but they return a
1563 @code{long int} instead of a floating-point number.
1564 @end deftypefun
1566 @deftypefun {long long int} llrint (double @var{x})
1567 @deftypefunx {long long int} llrintf (float @var{x})
1568 @deftypefunx {long long int} llrintl (long double @var{x})
1569 @deftypefunx {long long int} llrintfN (_Float@var{N} @var{x})
1570 @deftypefunx {long long int} llrintfNx (_Float@var{N}x @var{x})
1571 @standards{ISO, math.h}
1572 @standardsx{llrintfN, TS 18661-3:2015, math.h}
1573 @standardsx{llrintfNx, TS 18661-3:2015, math.h}
1574 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1575 These functions are just like @code{rint}, but they return a
1576 @code{long long int} instead of a floating-point number.
1577 @end deftypefun
1579 @deftypefun {long int} lround (double @var{x})
1580 @deftypefunx {long int} lroundf (float @var{x})
1581 @deftypefunx {long int} lroundl (long double @var{x})
1582 @deftypefunx {long int} lroundfN (_Float@var{N} @var{x})
1583 @deftypefunx {long int} lroundfNx (_Float@var{N}x @var{x})
1584 @standards{ISO, math.h}
1585 @standardsx{lroundfN, TS 18661-3:2015, math.h}
1586 @standardsx{lroundfNx, TS 18661-3:2015, math.h}
1587 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1588 These functions are just like @code{round}, but they return a
1589 @code{long int} instead of a floating-point number.
1590 @end deftypefun
1592 @deftypefun {long long int} llround (double @var{x})
1593 @deftypefunx {long long int} llroundf (float @var{x})
1594 @deftypefunx {long long int} llroundl (long double @var{x})
1595 @deftypefunx {long long int} llroundfN (_Float@var{N} @var{x})
1596 @deftypefunx {long long int} llroundfNx (_Float@var{N}x @var{x})
1597 @standards{ISO, math.h}
1598 @standardsx{llroundfN, TS 18661-3:2015, math.h}
1599 @standardsx{llroundfNx, TS 18661-3:2015, math.h}
1600 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1601 These functions are just like @code{round}, but they return a
1602 @code{long long int} instead of a floating-point number.
1603 @end deftypefun
1605 @deftypefun intmax_t fromfp (double @var{x}, int @var{round}, unsigned int @var{width})
1606 @deftypefunx intmax_t fromfpf (float @var{x}, int @var{round}, unsigned int @var{width})
1607 @deftypefunx intmax_t fromfpl (long double @var{x}, int @var{round}, unsigned int @var{width})
1608 @deftypefunx intmax_t fromfpfN (_Float@var{N} @var{x}, int @var{round}, unsigned int @var{width})
1609 @deftypefunx intmax_t fromfpfNx (_Float@var{N}x @var{x}, int @var{round}, unsigned int @var{width})
1610 @deftypefunx uintmax_t ufromfp (double @var{x}, int @var{round}, unsigned int @var{width})
1611 @deftypefunx uintmax_t ufromfpf (float @var{x}, int @var{round}, unsigned int @var{width})
1612 @deftypefunx uintmax_t ufromfpl (long double @var{x}, int @var{round}, unsigned int @var{width})
1613 @deftypefunx uintmax_t ufromfpfN (_Float@var{N} @var{x}, int @var{round}, unsigned int @var{width})
1614 @deftypefunx uintmax_t ufromfpfNx (_Float@var{N}x @var{x}, int @var{round}, unsigned int @var{width})
1615 @deftypefunx intmax_t fromfpx (double @var{x}, int @var{round}, unsigned int @var{width})
1616 @deftypefunx intmax_t fromfpxf (float @var{x}, int @var{round}, unsigned int @var{width})
1617 @deftypefunx intmax_t fromfpxl (long double @var{x}, int @var{round}, unsigned int @var{width})
1618 @deftypefunx intmax_t fromfpxfN (_Float@var{N} @var{x}, int @var{round}, unsigned int @var{width})
1619 @deftypefunx intmax_t fromfpxfNx (_Float@var{N}x @var{x}, int @var{round}, unsigned int @var{width})
1620 @deftypefunx uintmax_t ufromfpx (double @var{x}, int @var{round}, unsigned int @var{width})
1621 @deftypefunx uintmax_t ufromfpxf (float @var{x}, int @var{round}, unsigned int @var{width})
1622 @deftypefunx uintmax_t ufromfpxl (long double @var{x}, int @var{round}, unsigned int @var{width})
1623 @deftypefunx uintmax_t ufromfpxfN (_Float@var{N} @var{x}, int @var{round}, unsigned int @var{width})
1624 @deftypefunx uintmax_t ufromfpxfNx (_Float@var{N}x @var{x}, int @var{round}, unsigned int @var{width})
1625 @standards{ISO, math.h}
1626 @standardsx{fromfpfN, TS 18661-3:2015, math.h}
1627 @standardsx{fromfpfNx, TS 18661-3:2015, math.h}
1628 @standardsx{ufromfpfN, TS 18661-3:2015, math.h}
1629 @standardsx{ufromfpfNx, TS 18661-3:2015, math.h}
1630 @standardsx{fromfpxfN, TS 18661-3:2015, math.h}
1631 @standardsx{fromfpxfNx, TS 18661-3:2015, math.h}
1632 @standardsx{ufromfpxfN, TS 18661-3:2015, math.h}
1633 @standardsx{ufromfpxfNx, TS 18661-3:2015, math.h}
1634 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1635 These functions, from TS 18661-1:2014 and TS 18661-3:2015, convert a
1636 floating-point number to an integer according to the rounding direction
1637 @var{round} (one of the @code{FP_INT_*} macros).  If the integer is
1638 outside the range of a signed or unsigned (depending on the return type
1639 of the function) type of width @var{width} bits (or outside the range of
1640 the return type, if @var{width} is larger), or if @var{x} is infinite or
1641 NaN, or if @var{width} is zero, a domain error occurs and an unspecified
1642 value is returned.  The functions with an @samp{x} in their names raise
1643 the inexact exception when a domain error does not occur and the
1644 argument is not an integer; the other functions do not raise the inexact
1645 exception.
1646 @end deftypefun
1649 @deftypefun double modf (double @var{value}, double *@var{integer-part})
1650 @deftypefunx float modff (float @var{value}, float *@var{integer-part})
1651 @deftypefunx {long double} modfl (long double @var{value}, long double *@var{integer-part})
1652 @deftypefunx _FloatN modffN (_Float@var{N} @var{value}, _Float@var{N} *@var{integer-part})
1653 @deftypefunx _FloatNx modffNx (_Float@var{N}x @var{value}, _Float@var{N}x *@var{integer-part})
1654 @standards{ISO, math.h}
1655 @standardsx{modffN, TS 18661-3:2015, math.h}
1656 @standardsx{modffNx, TS 18661-3:2015, math.h}
1657 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1658 These functions break the argument @var{value} into an integer part and a
1659 fractional part (between @code{-1} and @code{1}, exclusive).  Their sum
1660 equals @var{value}.  Each of the parts has the same sign as @var{value},
1661 and the integer part is always rounded toward zero.
1663 @code{modf} stores the integer part in @code{*@var{integer-part}}, and
1664 returns the fractional part.  For example, @code{modf (2.5, &intpart)}
1665 returns @code{0.5} and stores @code{2.0} into @code{intpart}.
1666 @end deftypefun
1668 @node Remainder Functions
1669 @subsection Remainder Functions
1671 The functions in this section compute the remainder on division of two
1672 floating-point numbers.  Each is a little different; pick the one that
1673 suits your problem.
1675 @deftypefun double fmod (double @var{numerator}, double @var{denominator})
1676 @deftypefunx float fmodf (float @var{numerator}, float @var{denominator})
1677 @deftypefunx {long double} fmodl (long double @var{numerator}, long double @var{denominator})
1678 @deftypefunx _FloatN fmodfN (_Float@var{N} @var{numerator}, _Float@var{N} @var{denominator})
1679 @deftypefunx _FloatNx fmodfNx (_Float@var{N}x @var{numerator}, _Float@var{N}x @var{denominator})
1680 @standards{ISO, math.h}
1681 @standardsx{fmodfN, TS 18661-3:2015, math.h}
1682 @standardsx{fmodfNx, TS 18661-3:2015, math.h}
1683 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1684 These functions compute the remainder from the division of
1685 @var{numerator} by @var{denominator}.  Specifically, the return value is
1686 @code{@var{numerator} - @w{@var{n} * @var{denominator}}}, where @var{n}
1687 is the quotient of @var{numerator} divided by @var{denominator}, rounded
1688 towards zero to an integer.  Thus, @w{@code{fmod (6.5, 2.3)}} returns
1689 @code{1.9}, which is @code{6.5} minus @code{4.6}.
1691 The result has the same sign as the @var{numerator} and has magnitude
1692 less than the magnitude of the @var{denominator}.
1694 If @var{denominator} is zero, @code{fmod} signals a domain error.
1695 @end deftypefun
1697 @deftypefun double remainder (double @var{numerator}, double @var{denominator})
1698 @deftypefunx float remainderf (float @var{numerator}, float @var{denominator})
1699 @deftypefunx {long double} remainderl (long double @var{numerator}, long double @var{denominator})
1700 @deftypefunx _FloatN remainderfN (_Float@var{N} @var{numerator}, _Float@var{N} @var{denominator})
1701 @deftypefunx _FloatNx remainderfNx (_Float@var{N}x @var{numerator}, _Float@var{N}x @var{denominator})
1702 @standards{ISO, math.h}
1703 @standardsx{remainderfN, TS 18661-3:2015, math.h}
1704 @standardsx{remainderfNx, TS 18661-3:2015, math.h}
1705 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1706 These functions are like @code{fmod} except that they round the
1707 internal quotient @var{n} to the nearest integer instead of towards zero
1708 to an integer.  For example, @code{remainder (6.5, 2.3)} returns
1709 @code{-0.4}, which is @code{6.5} minus @code{6.9}.
1711 The absolute value of the result is less than or equal to half the
1712 absolute value of the @var{denominator}.  The difference between
1713 @code{fmod (@var{numerator}, @var{denominator})} and @code{remainder
1714 (@var{numerator}, @var{denominator})} is always either
1715 @var{denominator}, minus @var{denominator}, or zero.
1717 If @var{denominator} is zero, @code{remainder} signals a domain error.
1718 @end deftypefun
1720 @deftypefun double drem (double @var{numerator}, double @var{denominator})
1721 @deftypefunx float dremf (float @var{numerator}, float @var{denominator})
1722 @deftypefunx {long double} dreml (long double @var{numerator}, long double @var{denominator})
1723 @standards{BSD, math.h}
1724 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1725 This function is another name for @code{remainder}.
1726 @end deftypefun
1728 @node FP Bit Twiddling
1729 @subsection Setting and modifying single bits of FP values
1730 @cindex FP arithmetic
1732 There are some operations that are too complicated or expensive to
1733 perform by hand on floating-point numbers.  @w{ISO C99} defines
1734 functions to do these operations, which mostly involve changing single
1735 bits.
1737 @deftypefun double copysign (double @var{x}, double @var{y})
1738 @deftypefunx float copysignf (float @var{x}, float @var{y})
1739 @deftypefunx {long double} copysignl (long double @var{x}, long double @var{y})
1740 @deftypefunx _FloatN copysignfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
1741 @deftypefunx _FloatNx copysignfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
1742 @standards{ISO, math.h}
1743 @standardsx{copysignfN, TS 18661-3:2015, math.h}
1744 @standardsx{copysignfNx, TS 18661-3:2015, math.h}
1745 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1746 These functions return @var{x} but with the sign of @var{y}.  They work
1747 even if @var{x} or @var{y} are NaN or zero.  Both of these can carry a
1748 sign (although not all implementations support it) and this is one of
1749 the few operations that can tell the difference.
1751 @code{copysign} never raises an exception.
1752 @c except signalling NaNs
1754 This function is defined in @w{IEC 559} (and the appendix with
1755 recommended functions in @w{IEEE 754}/@w{IEEE 854}).
1756 @end deftypefun
1758 @deftypefun int signbit (@emph{float-type} @var{x})
1759 @standards{ISO, math.h}
1760 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1761 @code{signbit} is a generic macro which can work on all floating-point
1762 types.  It returns a nonzero value if the value of @var{x} has its sign
1763 bit set.
1765 This is not the same as @code{x < 0.0}, because @w{IEEE 754} floating
1766 point allows zero to be signed.  The comparison @code{-0.0 < 0.0} is
1767 false, but @code{signbit (-0.0)} will return a nonzero value.
1768 @end deftypefun
1770 @deftypefun double nextafter (double @var{x}, double @var{y})
1771 @deftypefunx float nextafterf (float @var{x}, float @var{y})
1772 @deftypefunx {long double} nextafterl (long double @var{x}, long double @var{y})
1773 @deftypefunx _FloatN nextafterfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
1774 @deftypefunx _FloatNx nextafterfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
1775 @standards{ISO, math.h}
1776 @standardsx{nextafterfN, TS 18661-3:2015, math.h}
1777 @standardsx{nextafterfNx, TS 18661-3:2015, math.h}
1778 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1779 The @code{nextafter} function returns the next representable neighbor of
1780 @var{x} in the direction towards @var{y}.  The size of the step between
1781 @var{x} and the result depends on the type of the result.  If
1782 @math{@var{x} = @var{y}} the function simply returns @var{y}.  If either
1783 value is @code{NaN}, @code{NaN} is returned.  Otherwise
1784 a value corresponding to the value of the least significant bit in the
1785 mantissa is added or subtracted, depending on the direction.
1786 @code{nextafter} will signal overflow or underflow if the result goes
1787 outside of the range of normalized numbers.
1789 This function is defined in @w{IEC 559} (and the appendix with
1790 recommended functions in @w{IEEE 754}/@w{IEEE 854}).
1791 @end deftypefun
1793 @deftypefun double nexttoward (double @var{x}, long double @var{y})
1794 @deftypefunx float nexttowardf (float @var{x}, long double @var{y})
1795 @deftypefunx {long double} nexttowardl (long double @var{x}, long double @var{y})
1796 @standards{ISO, math.h}
1797 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1798 These functions are identical to the corresponding versions of
1799 @code{nextafter} except that their second argument is a @code{long
1800 double}.
1801 @end deftypefun
1803 @deftypefun double nextup (double @var{x})
1804 @deftypefunx float nextupf (float @var{x})
1805 @deftypefunx {long double} nextupl (long double @var{x})
1806 @deftypefunx _FloatN nextupfN (_Float@var{N} @var{x})
1807 @deftypefunx _FloatNx nextupfNx (_Float@var{N}x @var{x})
1808 @standards{ISO, math.h}
1809 @standardsx{nextupfN, TS 18661-3:2015, math.h}
1810 @standardsx{nextupfNx, TS 18661-3:2015, math.h}
1811 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1812 The @code{nextup} function returns the next representable neighbor of @var{x}
1813 in the direction of positive infinity.  If @var{x} is the smallest negative
1814 subnormal number in the type of @var{x} the function returns @code{-0}.  If
1815 @math{@var{x} = @code{0}} the function returns the smallest positive subnormal
1816 number in the type of @var{x}.  If @var{x} is NaN, NaN is returned.
1817 If @var{x} is @math{+@infinity{}}, @math{+@infinity{}} is returned.
1818 @code{nextup} is from TS 18661-1:2014 and TS 18661-3:2015.
1819 @code{nextup} never raises an exception except for signaling NaNs.
1820 @end deftypefun
1822 @deftypefun double nextdown (double @var{x})
1823 @deftypefunx float nextdownf (float @var{x})
1824 @deftypefunx {long double} nextdownl (long double @var{x})
1825 @deftypefunx _FloatN nextdownfN (_Float@var{N} @var{x})
1826 @deftypefunx _FloatNx nextdownfNx (_Float@var{N}x @var{x})
1827 @standards{ISO, math.h}
1828 @standardsx{nextdownfN, TS 18661-3:2015, math.h}
1829 @standardsx{nextdownfNx, TS 18661-3:2015, math.h}
1830 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1831 The @code{nextdown} function returns the next representable neighbor of @var{x}
1832 in the direction of negative infinity.  If @var{x} is the smallest positive
1833 subnormal number in the type of @var{x} the function returns @code{+0}.  If
1834 @math{@var{x} = @code{0}} the function returns the smallest negative subnormal
1835 number in the type of @var{x}.  If @var{x} is NaN, NaN is returned.
1836 If @var{x} is @math{-@infinity{}}, @math{-@infinity{}} is returned.
1837 @code{nextdown} is from TS 18661-1:2014 and TS 18661-3:2015.
1838 @code{nextdown} never raises an exception except for signaling NaNs.
1839 @end deftypefun
1841 @cindex NaN
1842 @deftypefun double nan (const char *@var{tagp})
1843 @deftypefunx float nanf (const char *@var{tagp})
1844 @deftypefunx {long double} nanl (const char *@var{tagp})
1845 @deftypefunx _FloatN nanfN (const char *@var{tagp})
1846 @deftypefunx _FloatNx nanfNx (const char *@var{tagp})
1847 @standards{ISO, math.h}
1848 @standardsx{nanfN, TS 18661-3:2015, math.h}
1849 @standardsx{nanfNx, TS 18661-3:2015, math.h}
1850 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
1851 @c The unsafe-but-ruled-safe locale use comes from strtod.
1852 The @code{nan} function returns a representation of NaN, provided that
1853 NaN is supported by the target platform.
1854 @code{nan ("@var{n-char-sequence}")} is equivalent to
1855 @code{strtod ("NAN(@var{n-char-sequence})")}.
1857 The argument @var{tagp} is used in an unspecified manner.  On @w{IEEE
1858 754} systems, there are many representations of NaN, and @var{tagp}
1859 selects one.  On other systems it may do nothing.
1860 @end deftypefun
1862 @deftypefun int canonicalize (double *@var{cx}, const double *@var{x})
1863 @deftypefunx int canonicalizef (float *@var{cx}, const float *@var{x})
1864 @deftypefunx int canonicalizel (long double *@var{cx}, const long double *@var{x})
1865 @deftypefunx int canonicalizefN (_Float@var{N} *@var{cx}, const _Float@var{N} *@var{x})
1866 @deftypefunx int canonicalizefNx (_Float@var{N}x *@var{cx}, const _Float@var{N}x *@var{x})
1867 @standards{ISO, math.h}
1868 @standardsx{canonicalizefN, TS 18661-3:2015, math.h}
1869 @standardsx{canonicalizefNx, TS 18661-3:2015, math.h}
1870 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1871 In some floating-point formats, some values have canonical (preferred)
1872 and noncanonical encodings (for IEEE interchange binary formats, all
1873 encodings are canonical).  These functions, defined by TS
1874 18661-1:2014 and TS 18661-3:2015, attempt to produce a canonical version
1875 of the floating-point value pointed to by @var{x}; if that value is a
1876 signaling NaN, they raise the invalid exception and produce a quiet
1877 NaN.  If a canonical value is produced, it is stored in the object
1878 pointed to by @var{cx}, and these functions return zero.  Otherwise
1879 (if a canonical value could not be produced because the object pointed
1880 to by @var{x} is not a valid representation of any floating-point
1881 value), the object pointed to by @var{cx} is unchanged and a nonzero
1882 value is returned.
1884 Note that some formats have multiple encodings of a value which are
1885 all equally canonical; when such an encoding is used as an input to
1886 this function, any such encoding of the same value (or of the
1887 corresponding quiet NaN, if that value is a signaling NaN) may be
1888 produced as output.
1889 @end deftypefun
1891 @deftypefun double getpayload (const double *@var{x})
1892 @deftypefunx float getpayloadf (const float *@var{x})
1893 @deftypefunx {long double} getpayloadl (const long double *@var{x})
1894 @deftypefunx _FloatN getpayloadfN (const _Float@var{N} *@var{x})
1895 @deftypefunx _FloatNx getpayloadfNx (const _Float@var{N}x *@var{x})
1896 @standards{ISO, math.h}
1897 @standardsx{getpayloadfN, TS 18661-3:2015, math.h}
1898 @standardsx{getpayloadfNx, TS 18661-3:2015, math.h}
1899 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1900 IEEE 754 defines the @dfn{payload} of a NaN to be an integer value
1901 encoded in the representation of the NaN.  Payloads are typically
1902 propagated from NaN inputs to the result of a floating-point
1903 operation.  These functions, defined by TS 18661-1:2014 and TS
1904 18661-3:2015, return the payload of the NaN pointed to by @var{x}
1905 (returned as a positive integer, or positive zero, represented as a
1906 floating-point number); if @var{x} is not a NaN, they return an
1907 unspecified value.  They raise no floating-point exceptions even for
1908 signaling NaNs.
1909 @end deftypefun
1911 @deftypefun int setpayload (double *@var{x}, double @var{payload})
1912 @deftypefunx int setpayloadf (float *@var{x}, float @var{payload})
1913 @deftypefunx int setpayloadl (long double *@var{x}, long double @var{payload})
1914 @deftypefunx int setpayloadfN (_Float@var{N} *@var{x}, _Float@var{N} @var{payload})
1915 @deftypefunx int setpayloadfNx (_Float@var{N}x *@var{x}, _Float@var{N}x @var{payload})
1916 @standards{ISO, math.h}
1917 @standardsx{setpayloadfN, TS 18661-3:2015, math.h}
1918 @standardsx{setpayloadfNx, TS 18661-3:2015, math.h}
1919 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1920 These functions, defined by TS 18661-1:2014 and TS 18661-3:2015, set the
1921 object pointed to by @var{x} to a quiet NaN with payload @var{payload}
1922 and a zero sign bit and return zero.  If @var{payload} is not a
1923 positive-signed integer that is a valid payload for a quiet NaN of the
1924 given type, the object pointed to by @var{x} is set to positive zero and
1925 a nonzero value is returned.  They raise no floating-point exceptions.
1926 @end deftypefun
1928 @deftypefun int setpayloadsig (double *@var{x}, double @var{payload})
1929 @deftypefunx int setpayloadsigf (float *@var{x}, float @var{payload})
1930 @deftypefunx int setpayloadsigl (long double *@var{x}, long double @var{payload})
1931 @deftypefunx int setpayloadsigfN (_Float@var{N} *@var{x}, _Float@var{N} @var{payload})
1932 @deftypefunx int setpayloadsigfNx (_Float@var{N}x *@var{x}, _Float@var{N}x @var{payload})
1933 @standards{ISO, math.h}
1934 @standardsx{setpayloadsigfN, TS 18661-3:2015, math.h}
1935 @standardsx{setpayloadsigfNx, TS 18661-3:2015, math.h}
1936 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1937 These functions, defined by TS 18661-1:2014 and TS 18661-3:2015, set the
1938 object pointed to by @var{x} to a signaling NaN with payload
1939 @var{payload} and a zero sign bit and return zero.  If @var{payload} is
1940 not a positive-signed integer that is a valid payload for a signaling
1941 NaN of the given type, the object pointed to by @var{x} is set to
1942 positive zero and a nonzero value is returned.  They raise no
1943 floating-point exceptions.
1944 @end deftypefun
1946 @node FP Comparison Functions
1947 @subsection Floating-Point Comparison Functions
1948 @cindex unordered comparison
1950 The standard C comparison operators provoke exceptions when one or other
1951 of the operands is NaN.  For example,
1953 @smallexample
1954 int v = a < 1.0;
1955 @end smallexample
1957 @noindent
1958 will raise an exception if @var{a} is NaN.  (This does @emph{not}
1959 happen with @code{==} and @code{!=}; those merely return false and true,
1960 respectively, when NaN is examined.)  Frequently this exception is
1961 undesirable.  @w{ISO C99} therefore defines comparison functions that
1962 do not raise exceptions when NaN is examined.  All of the functions are
1963 implemented as macros which allow their arguments to be of any
1964 floating-point type.  The macros are guaranteed to evaluate their
1965 arguments only once.  TS 18661-1:2014 adds such a macro for an
1966 equality comparison that @emph{does} raise an exception for a NaN
1967 argument; it also adds functions that provide a total ordering on all
1968 floating-point values, including NaNs, without raising any exceptions
1969 even for signaling NaNs.
1971 @deftypefn Macro int isgreater (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
1972 @standards{ISO, math.h}
1973 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1974 This macro determines whether the argument @var{x} is greater than
1975 @var{y}.  It is equivalent to @code{(@var{x}) > (@var{y})}, but no
1976 exception is raised if @var{x} or @var{y} are NaN.
1977 @end deftypefn
1979 @deftypefn Macro int isgreaterequal (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
1980 @standards{ISO, math.h}
1981 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1982 This macro determines whether the argument @var{x} is greater than or
1983 equal to @var{y}.  It is equivalent to @code{(@var{x}) >= (@var{y})}, but no
1984 exception is raised if @var{x} or @var{y} are NaN.
1985 @end deftypefn
1987 @deftypefn Macro int isless (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
1988 @standards{ISO, math.h}
1989 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1990 This macro determines whether the argument @var{x} is less than @var{y}.
1991 It is equivalent to @code{(@var{x}) < (@var{y})}, but no exception is
1992 raised if @var{x} or @var{y} are NaN.
1993 @end deftypefn
1995 @deftypefn Macro int islessequal (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
1996 @standards{ISO, math.h}
1997 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1998 This macro determines whether the argument @var{x} is less than or equal
1999 to @var{y}.  It is equivalent to @code{(@var{x}) <= (@var{y})}, but no
2000 exception is raised if @var{x} or @var{y} are NaN.
2001 @end deftypefn
2003 @deftypefn Macro int islessgreater (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
2004 @standards{ISO, math.h}
2005 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2006 This macro determines whether the argument @var{x} is less or greater
2007 than @var{y}.  It is equivalent to @code{(@var{x}) < (@var{y}) ||
2008 (@var{x}) > (@var{y})} (although it only evaluates @var{x} and @var{y}
2009 once), but no exception is raised if @var{x} or @var{y} are NaN.
2011 This macro is not equivalent to @code{@var{x} != @var{y}}, because that
2012 expression is true if @var{x} or @var{y} are NaN.
2013 @end deftypefn
2015 @deftypefn Macro int isunordered (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
2016 @standards{ISO, math.h}
2017 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2018 This macro determines whether its arguments are unordered.  In other
2019 words, it is true if @var{x} or @var{y} are NaN, and false otherwise.
2020 @end deftypefn
2022 @deftypefn Macro int iseqsig (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
2023 @standards{ISO, math.h}
2024 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2025 This macro determines whether its arguments are equal.  It is
2026 equivalent to @code{(@var{x}) == (@var{y})}, but it raises the invalid
2027 exception and sets @code{errno} to @code{EDOM} if either argument is a
2028 NaN.
2029 @end deftypefn
2031 @deftypefun int totalorder (double @var{x}, double @var{y})
2032 @deftypefunx int totalorderf (float @var{x}, float @var{y})
2033 @deftypefunx int totalorderl (long double @var{x}, long double @var{y})
2034 @deftypefunx int totalorderfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
2035 @deftypefunx int totalorderfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
2036 @standards{TS 18661-1:2014, math.h}
2037 @standardsx{totalorderfN, TS 18661-3:2015, math.h}
2038 @standardsx{totalorderfNx, TS 18661-3:2015, math.h}
2039 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2040 These functions determine whether the total order relationship,
2041 defined in IEEE 754-2008, is true for @var{x} and @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 @var{x} comes before @var{y} in the following
2046 order: negative quiet NaNs, in order of decreasing payload; negative
2047 signaling NaNs, in order of decreasing payload; negative infinity;
2048 finite numbers, in ascending order, with negative zero before positive
2049 zero; positive infinity; positive signaling NaNs, in order of
2050 increasing payload; positive quiet NaNs, in order of increasing
2051 payload.
2052 @end deftypefun
2054 @deftypefun int totalordermag (double @var{x}, double @var{y})
2055 @deftypefunx int totalordermagf (float @var{x}, float @var{y})
2056 @deftypefunx int totalordermagl (long double @var{x}, long double @var{y})
2057 @deftypefunx int totalordermagfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
2058 @deftypefunx int totalordermagfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
2059 @standards{TS 18661-1:2014, math.h}
2060 @standardsx{totalordermagfN, TS 18661-3:2015, math.h}
2061 @standardsx{totalordermagfNx, TS 18661-3:2015, math.h}
2062 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2063 These functions determine whether the total order relationship,
2064 defined in IEEE 754-2008, is true for the absolute values of @var{x}
2065 and @var{y}, returning nonzero if it is true and zero if it is false.
2066 No exceptions are raised even for signaling NaNs.
2067 @end deftypefun
2069 Not all machines provide hardware support for these operations.  On
2070 machines that don't, the macros can be very slow.  Therefore, you should
2071 not use these functions when NaN is not a concern.
2073 @strong{NB:} There are no macros @code{isequal} or @code{isunequal}.
2074 They are unnecessary, because the @code{==} and @code{!=} operators do
2075 @emph{not} throw an exception if one or both of the operands are NaN.
2077 @node Misc FP Arithmetic
2078 @subsection Miscellaneous FP arithmetic functions
2079 @cindex minimum
2080 @cindex maximum
2081 @cindex positive difference
2082 @cindex multiply-add
2084 The functions in this section perform miscellaneous but common
2085 operations that are awkward to express with C operators.  On some
2086 processors these functions can use special machine instructions to
2087 perform these operations faster than the equivalent C code.
2089 @deftypefun double fmin (double @var{x}, double @var{y})
2090 @deftypefunx float fminf (float @var{x}, float @var{y})
2091 @deftypefunx {long double} fminl (long double @var{x}, long double @var{y})
2092 @deftypefunx _FloatN fminfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
2093 @deftypefunx _FloatNx fminfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
2094 @standards{ISO, math.h}
2095 @standardsx{fminfN, TS 18661-3:2015, math.h}
2096 @standardsx{fminfNx, TS 18661-3:2015, math.h}
2097 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2098 The @code{fmin} function returns the lesser of the two values @var{x}
2099 and @var{y}.  It is similar to the expression
2100 @smallexample
2101 ((x) < (y) ? (x) : (y))
2102 @end smallexample
2103 except that @var{x} and @var{y} are only evaluated once.
2105 If an argument is NaN, the other argument is returned.  If both arguments
2106 are NaN, NaN is returned.
2107 @end deftypefun
2109 @deftypefun double fmax (double @var{x}, double @var{y})
2110 @deftypefunx float fmaxf (float @var{x}, float @var{y})
2111 @deftypefunx {long double} fmaxl (long double @var{x}, long double @var{y})
2112 @deftypefunx _FloatN fmaxfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
2113 @deftypefunx _FloatNx fmaxfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
2114 @standards{ISO, math.h}
2115 @standardsx{fmaxfN, TS 18661-3:2015, math.h}
2116 @standardsx{fmaxfNx, TS 18661-3:2015, math.h}
2117 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2118 The @code{fmax} function returns the greater of the two values @var{x}
2119 and @var{y}.
2121 If an argument is NaN, the other argument is returned.  If both arguments
2122 are NaN, NaN is returned.
2123 @end deftypefun
2125 @deftypefun double fminmag (double @var{x}, double @var{y})
2126 @deftypefunx float fminmagf (float @var{x}, float @var{y})
2127 @deftypefunx {long double} fminmagl (long double @var{x}, long double @var{y})
2128 @deftypefunx _FloatN fminmagfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
2129 @deftypefunx _FloatNx fminmagfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
2130 @standards{ISO, math.h}
2131 @standardsx{fminmagfN, TS 18661-3:2015, math.h}
2132 @standardsx{fminmagfNx, TS 18661-3:2015, math.h}
2133 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2134 These functions, from TS 18661-1:2014 and TS 18661-3:2015, return
2135 whichever of the two values @var{x} and @var{y} has the smaller absolute
2136 value.  If both have the same absolute value, or either is NaN, they
2137 behave the same as the @code{fmin} functions.
2138 @end deftypefun
2140 @deftypefun double fmaxmag (double @var{x}, double @var{y})
2141 @deftypefunx float fmaxmagf (float @var{x}, float @var{y})
2142 @deftypefunx {long double} fmaxmagl (long double @var{x}, long double @var{y})
2143 @deftypefunx _FloatN fmaxmagfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
2144 @deftypefunx _FloatNx fmaxmagfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
2145 @standards{ISO, math.h}
2146 @standardsx{fmaxmagfN, TS 18661-3:2015, math.h}
2147 @standardsx{fmaxmagfNx, TS 18661-3:2015, math.h}
2148 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2149 These functions, from TS 18661-1:2014, return whichever of the two
2150 values @var{x} and @var{y} has the greater absolute value.  If both
2151 have the same absolute value, or either is NaN, they behave the same
2152 as the @code{fmax} functions.
2153 @end deftypefun
2155 @deftypefun double fdim (double @var{x}, double @var{y})
2156 @deftypefunx float fdimf (float @var{x}, float @var{y})
2157 @deftypefunx {long double} fdiml (long double @var{x}, long double @var{y})
2158 @deftypefunx _FloatN fdimfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
2159 @deftypefunx _FloatNx fdimfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
2160 @standards{ISO, math.h}
2161 @standardsx{fdimfN, TS 18661-3:2015, math.h}
2162 @standardsx{fdimfNx, TS 18661-3:2015, math.h}
2163 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2164 The @code{fdim} function returns the positive difference between
2165 @var{x} and @var{y}.  The positive difference is @math{@var{x} -
2166 @var{y}} if @var{x} is greater than @var{y}, and @math{0} otherwise.
2168 If @var{x}, @var{y}, or both are NaN, NaN is returned.
2169 @end deftypefun
2171 @deftypefun double fma (double @var{x}, double @var{y}, double @var{z})
2172 @deftypefunx float fmaf (float @var{x}, float @var{y}, float @var{z})
2173 @deftypefunx {long double} fmal (long double @var{x}, long double @var{y}, long double @var{z})
2174 @deftypefunx _FloatN fmafN (_Float@var{N} @var{x}, _Float@var{N} @var{y}, _Float@var{N} @var{z})
2175 @deftypefunx _FloatNx fmafNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y}, _Float@var{N}x @var{z})
2176 @standards{ISO, math.h}
2177 @standardsx{fmafN, TS 18661-3:2015, math.h}
2178 @standardsx{fmafNx, TS 18661-3:2015, math.h}
2179 @cindex butterfly
2180 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2181 The @code{fma} function performs floating-point multiply-add.  This is
2182 the operation @math{(@var{x} @mul{} @var{y}) + @var{z}}, but the
2183 intermediate result is not rounded to the destination type.  This can
2184 sometimes improve the precision of a calculation.
2186 This function was introduced because some processors have a special
2187 instruction to perform multiply-add.  The C compiler cannot use it
2188 directly, because the expression @samp{x*y + z} is defined to round the
2189 intermediate result.  @code{fma} lets you choose when you want to round
2190 only once.
2192 @vindex FP_FAST_FMA
2193 On processors which do not implement multiply-add in hardware,
2194 @code{fma} can be very slow since it must avoid intermediate rounding.
2195 @file{math.h} defines the symbols @code{FP_FAST_FMA},
2196 @code{FP_FAST_FMAF}, and @code{FP_FAST_FMAL} when the corresponding
2197 version of @code{fma} is no slower than the expression @samp{x*y + z}.
2198 In @theglibc{}, this always means the operation is implemented in
2199 hardware.
2200 @end deftypefun
2202 @node Complex Numbers
2203 @section Complex Numbers
2204 @pindex complex.h
2205 @cindex complex numbers
2207 @w{ISO C99} introduces support for complex numbers in C.  This is done
2208 with a new type qualifier, @code{complex}.  It is a keyword if and only
2209 if @file{complex.h} has been included.  There are three complex types,
2210 corresponding to the three real types:  @code{float complex},
2211 @code{double complex}, and @code{long double complex}.
2213 Likewise, on machines that have support for @code{_Float@var{N}} or
2214 @code{_Float@var{N}x} enabled, the complex types @code{_Float@var{N}
2215 complex} and @code{_Float@var{N}x complex} are also available if
2216 @file{complex.h} has been included; @pxref{Mathematics}.
2218 To construct complex numbers you need a way to indicate the imaginary
2219 part of a number.  There is no standard notation for an imaginary
2220 floating point constant.  Instead, @file{complex.h} defines two macros
2221 that can be used to create complex numbers.
2223 @deftypevr Macro {const float complex} _Complex_I
2224 @standards{C99, complex.h}
2225 This macro is a representation of the complex number ``@math{0+1i}''.
2226 Multiplying a real floating-point value by @code{_Complex_I} gives a
2227 complex number whose value is purely imaginary.  You can use this to
2228 construct complex constants:
2230 @smallexample
2231 @math{3.0 + 4.0i} = @code{3.0 + 4.0 * _Complex_I}
2232 @end smallexample
2234 Note that @code{_Complex_I * _Complex_I} has the value @code{-1}, but
2235 the type of that value is @code{complex}.
2236 @end deftypevr
2238 @c Put this back in when gcc supports _Imaginary_I.  It's too confusing.
2239 @ignore
2240 @noindent
2241 Without an optimizing compiler this is more expensive than the use of
2242 @code{_Imaginary_I} but with is better than nothing.  You can avoid all
2243 the hassles if you use the @code{I} macro below if the name is not
2244 problem.
2246 @deftypevr Macro {const float imaginary} _Imaginary_I
2247 This macro is a representation of the value ``@math{1i}''.  I.e., it is
2248 the value for which
2250 @smallexample
2251 _Imaginary_I * _Imaginary_I = -1
2252 @end smallexample
2254 @noindent
2255 The result is not of type @code{float imaginary} but instead @code{float}.
2256 One can use it to easily construct complex number like in
2258 @smallexample
2259 3.0 - _Imaginary_I * 4.0
2260 @end smallexample
2262 @noindent
2263 which results in the complex number with a real part of 3.0 and a
2264 imaginary part -4.0.
2265 @end deftypevr
2266 @end ignore
2268 @noindent
2269 @code{_Complex_I} is a bit of a mouthful.  @file{complex.h} also defines
2270 a shorter name for the same constant.
2272 @deftypevr Macro {const float complex} I
2273 @standards{C99, complex.h}
2274 This macro has exactly the same value as @code{_Complex_I}.  Most of the
2275 time it is preferable.  However, it causes problems if you want to use
2276 the identifier @code{I} for something else.  You can safely write
2278 @smallexample
2279 #include <complex.h>
2280 #undef I
2281 @end smallexample
2283 @noindent
2284 if you need @code{I} for your own purposes.  (In that case we recommend
2285 you also define some other short name for @code{_Complex_I}, such as
2286 @code{J}.)
2288 @ignore
2289 If the implementation does not support the @code{imaginary} types
2290 @code{I} is defined as @code{_Complex_I} which is the second best
2291 solution.  It still can be used in the same way but requires a most
2292 clever compiler to get the same results.
2293 @end ignore
2294 @end deftypevr
2296 @node Operations on Complex
2297 @section Projections, Conjugates, and Decomposing of Complex Numbers
2298 @cindex project complex numbers
2299 @cindex conjugate complex numbers
2300 @cindex decompose complex numbers
2301 @pindex complex.h
2303 @w{ISO C99} also defines functions that perform basic operations on
2304 complex numbers, such as decomposition and conjugation.  The prototypes
2305 for all these functions are in @file{complex.h}.  All functions are
2306 available in three variants, one for each of the three complex types.
2308 @deftypefun double creal (complex double @var{z})
2309 @deftypefunx float crealf (complex float @var{z})
2310 @deftypefunx {long double} creall (complex long double @var{z})
2311 @deftypefunx _FloatN crealfN (complex _Float@var{N} @var{z})
2312 @deftypefunx _FloatNx crealfNx (complex _Float@var{N}x @var{z})
2313 @standards{ISO, complex.h}
2314 @standardsx{crealfN, TS 18661-3:2015, complex.h}
2315 @standardsx{crealfNx, TS 18661-3:2015, complex.h}
2316 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2317 These functions return the real part of the complex number @var{z}.
2318 @end deftypefun
2320 @deftypefun double cimag (complex double @var{z})
2321 @deftypefunx float cimagf (complex float @var{z})
2322 @deftypefunx {long double} cimagl (complex long double @var{z})
2323 @deftypefunx _FloatN cimagfN (complex _Float@var{N} @var{z})
2324 @deftypefunx _FloatNx cimagfNx (complex _Float@var{N}x @var{z})
2325 @standards{ISO, complex.h}
2326 @standardsx{cimagfN, TS 18661-3:2015, complex.h}
2327 @standardsx{cimagfNx, TS 18661-3:2015, complex.h}
2328 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2329 These functions return the imaginary part of the complex number @var{z}.
2330 @end deftypefun
2332 @deftypefun {complex double} conj (complex double @var{z})
2333 @deftypefunx {complex float} conjf (complex float @var{z})
2334 @deftypefunx {complex long double} conjl (complex long double @var{z})
2335 @deftypefunx {complex _FloatN} conjfN (complex _Float@var{N} @var{z})
2336 @deftypefunx {complex _FloatNx} conjfNx (complex _Float@var{N}x @var{z})
2337 @standards{ISO, complex.h}
2338 @standardsx{conjfN, TS 18661-3:2015, complex.h}
2339 @standardsx{conjfNx, TS 18661-3:2015, complex.h}
2340 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2341 These functions return the conjugate value of the complex number
2342 @var{z}.  The conjugate of a complex number has the same real part and a
2343 negated imaginary part.  In other words, @samp{conj(a + bi) = a + -bi}.
2344 @end deftypefun
2346 @deftypefun double carg (complex double @var{z})
2347 @deftypefunx float cargf (complex float @var{z})
2348 @deftypefunx {long double} cargl (complex long double @var{z})
2349 @deftypefunx _FloatN cargfN (complex _Float@var{N} @var{z})
2350 @deftypefunx _FloatNx cargfNx (complex _Float@var{N}x @var{z})
2351 @standards{ISO, complex.h}
2352 @standardsx{cargfN, TS 18661-3:2015, complex.h}
2353 @standardsx{cargfNx, TS 18661-3:2015, complex.h}
2354 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2355 These functions return the argument of the complex number @var{z}.
2356 The argument of a complex number is the angle in the complex plane
2357 between the positive real axis and a line passing through zero and the
2358 number.  This angle is measured in the usual fashion and ranges from
2359 @math{-@pi{}} to @math{@pi{}}.
2361 @code{carg} has a branch cut along the negative real axis.
2362 @end deftypefun
2364 @deftypefun {complex double} cproj (complex double @var{z})
2365 @deftypefunx {complex float} cprojf (complex float @var{z})
2366 @deftypefunx {complex long double} cprojl (complex long double @var{z})
2367 @deftypefunx {complex _FloatN} cprojfN (complex _Float@var{N} @var{z})
2368 @deftypefunx {complex _FloatNx} cprojfNx (complex _Float@var{N}x @var{z})
2369 @standards{ISO, complex.h}
2370 @standardsx{cprojfN, TS 18661-3:2015, complex.h}
2371 @standardsx{cprojfNx, TS 18661-3:2015, complex.h}
2372 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2373 These functions return the projection of the complex value @var{z} onto
2374 the Riemann sphere.  Values with an infinite imaginary part are projected
2375 to positive infinity on the real axis, even if the real part is NaN.  If
2376 the real part is infinite, the result is equivalent to
2378 @smallexample
2379 INFINITY + I * copysign (0.0, cimag (z))
2380 @end smallexample
2381 @end deftypefun
2383 @node Parsing of Numbers
2384 @section Parsing of Numbers
2385 @cindex parsing numbers (in formatted input)
2386 @cindex converting strings to numbers
2387 @cindex number syntax, parsing
2388 @cindex syntax, for reading numbers
2390 This section describes functions for ``reading'' integer and
2391 floating-point numbers from a string.  It may be more convenient in some
2392 cases to use @code{sscanf} or one of the related functions; see
2393 @ref{Formatted Input}.  But often you can make a program more robust by
2394 finding the tokens in the string by hand, then converting the numbers
2395 one by one.
2397 @menu
2398 * Parsing of Integers::         Functions for conversion of integer values.
2399 * Parsing of Floats::           Functions for conversion of floating-point
2400                                  values.
2401 @end menu
2403 @node Parsing of Integers
2404 @subsection Parsing of Integers
2406 @pindex stdlib.h
2407 @pindex wchar.h
2408 The @samp{str} functions are declared in @file{stdlib.h} and those
2409 beginning with @samp{wcs} are declared in @file{wchar.h}.  One might
2410 wonder about the use of @code{restrict} in the prototypes of the
2411 functions in this section.  It is seemingly useless but the @w{ISO C}
2412 standard uses it (for the functions defined there) so we have to do it
2413 as well.
2415 @deftypefun {long int} strtol (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base})
2416 @standards{ISO, stdlib.h}
2417 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
2418 @c strtol uses the thread-local pointer to the locale in effect, and
2419 @c strtol_l loads the LC_NUMERIC locale data from it early on and once,
2420 @c but if the locale is the global locale, and another thread calls
2421 @c setlocale in a way that modifies the pointer to the LC_CTYPE locale
2422 @c category, the behavior of e.g. IS*, TOUPPER will vary throughout the
2423 @c execution of the function, because they re-read the locale data from
2424 @c the given locale pointer.  We solved this by documenting setlocale as
2425 @c MT-Unsafe.
2426 The @code{strtol} (``string-to-long'') function converts the initial
2427 part of @var{string} to a signed integer, which is returned as a value
2428 of type @code{long int}.
2430 This function attempts to decompose @var{string} as follows:
2432 @itemize @bullet
2433 @item
2434 A (possibly empty) sequence of whitespace characters.  Which characters
2435 are whitespace is determined by the @code{isspace} function
2436 (@pxref{Classification of Characters}).  These are discarded.
2438 @item
2439 An optional plus or minus sign (@samp{+} or @samp{-}).
2441 @item
2442 A nonempty sequence of digits in the radix specified by @var{base}.
2444 If @var{base} is zero, decimal radix is assumed unless the series of
2445 digits begins with @samp{0} (specifying octal radix), or @samp{0x} or
2446 @samp{0X} (specifying hexadecimal radix); in other words, the same
2447 syntax used for integer constants in C.
2449 Otherwise @var{base} must have a value between @code{2} and @code{36}.
2450 If @var{base} is @code{16}, the digits may optionally be preceded by
2451 @samp{0x} or @samp{0X}.  If base has no legal value the value returned
2452 is @code{0l} and the global variable @code{errno} is set to @code{EINVAL}.
2454 @item
2455 Any remaining characters in the string.  If @var{tailptr} is not a null
2456 pointer, @code{strtol} stores a pointer to this tail in
2457 @code{*@var{tailptr}}.
2458 @end itemize
2460 If the string is empty, contains only whitespace, or does not contain an
2461 initial substring that has the expected syntax for an integer in the
2462 specified @var{base}, no conversion is performed.  In this case,
2463 @code{strtol} returns a value of zero and the value stored in
2464 @code{*@var{tailptr}} is the value of @var{string}.
2466 In a locale other than the standard @code{"C"} locale, this function
2467 may recognize additional implementation-dependent syntax.
2469 If the string has valid syntax for an integer but the value is not
2470 representable because of overflow, @code{strtol} returns either
2471 @code{LONG_MAX} or @code{LONG_MIN} (@pxref{Range of Type}), as
2472 appropriate for the sign of the value.  It also sets @code{errno}
2473 to @code{ERANGE} to indicate there was overflow.
2475 You should not check for errors by examining the return value of
2476 @code{strtol}, because the string might be a valid representation of
2477 @code{0l}, @code{LONG_MAX}, or @code{LONG_MIN}.  Instead, check whether
2478 @var{tailptr} points to what you expect after the number
2479 (e.g. @code{'\0'} if the string should end after the number).  You also
2480 need to clear @var{errno} before the call and check it afterward, in
2481 case there was overflow.
2483 There is an example at the end of this section.
2484 @end deftypefun
2486 @deftypefun {long int} wcstol (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base})
2487 @standards{ISO, wchar.h}
2488 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
2489 The @code{wcstol} function is equivalent to the @code{strtol} function
2490 in nearly all aspects but handles wide character strings.
2492 The @code{wcstol} function was introduced in @w{Amendment 1} of @w{ISO C90}.
2493 @end deftypefun
2495 @deftypefun {unsigned long int} strtoul (const char *retrict @var{string}, char **restrict @var{tailptr}, int @var{base})
2496 @standards{ISO, stdlib.h}
2497 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
2498 The @code{strtoul} (``string-to-unsigned-long'') function is like
2499 @code{strtol} except it converts to an @code{unsigned long int} value.
2500 The syntax is the same as described above for @code{strtol}.  The value
2501 returned on overflow is @code{ULONG_MAX} (@pxref{Range of Type}).
2503 If @var{string} depicts a negative number, @code{strtoul} acts the same
2504 as @var{strtol} but casts the result to an unsigned integer.  That means
2505 for example that @code{strtoul} on @code{"-1"} returns @code{ULONG_MAX}
2506 and an input more negative than @code{LONG_MIN} returns
2507 (@code{ULONG_MAX} + 1) / 2.
2509 @code{strtoul} sets @var{errno} to @code{EINVAL} if @var{base} is out of
2510 range, or @code{ERANGE} on overflow.
2511 @end deftypefun
2513 @deftypefun {unsigned long int} wcstoul (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base})
2514 @standards{ISO, wchar.h}
2515 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
2516 The @code{wcstoul} function is equivalent to the @code{strtoul} function
2517 in nearly all aspects but handles wide character strings.
2519 The @code{wcstoul} function was introduced in @w{Amendment 1} of @w{ISO C90}.
2520 @end deftypefun
2522 @deftypefun {long long int} strtoll (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base})
2523 @standards{ISO, stdlib.h}
2524 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
2525 The @code{strtoll} function is like @code{strtol} except that it returns
2526 a @code{long long int} value, and accepts numbers with a correspondingly
2527 larger range.
2529 If the string has valid syntax for an integer but the value is not
2530 representable because of overflow, @code{strtoll} returns either
2531 @code{LLONG_MAX} or @code{LLONG_MIN} (@pxref{Range of Type}), as
2532 appropriate for the sign of the value.  It also sets @code{errno} to
2533 @code{ERANGE} to indicate there was overflow.
2535 The @code{strtoll} function was introduced in @w{ISO C99}.
2536 @end deftypefun
2538 @deftypefun {long long int} wcstoll (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base})
2539 @standards{ISO, wchar.h}
2540 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
2541 The @code{wcstoll} function is equivalent to the @code{strtoll} function
2542 in nearly all aspects but handles wide character strings.
2544 The @code{wcstoll} function was introduced in @w{Amendment 1} of @w{ISO C90}.
2545 @end deftypefun
2547 @deftypefun {long long int} strtoq (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base})
2548 @standards{BSD, stdlib.h}
2549 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
2550 @code{strtoq} (``string-to-quad-word'') is the BSD name for @code{strtoll}.
2551 @end deftypefun
2553 @deftypefun {long long int} wcstoq (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base})
2554 @standards{GNU, wchar.h}
2555 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
2556 The @code{wcstoq} function is equivalent to the @code{strtoq} function
2557 in nearly all aspects but handles wide character strings.
2559 The @code{wcstoq} function is a GNU extension.
2560 @end deftypefun
2562 @deftypefun {unsigned long long int} strtoull (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base})
2563 @standards{ISO, stdlib.h}
2564 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
2565 The @code{strtoull} function is related to @code{strtoll} the same way
2566 @code{strtoul} is related to @code{strtol}.
2568 The @code{strtoull} function was introduced in @w{ISO C99}.
2569 @end deftypefun
2571 @deftypefun {unsigned long long int} wcstoull (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base})
2572 @standards{ISO, wchar.h}
2573 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
2574 The @code{wcstoull} function is equivalent to the @code{strtoull} function
2575 in nearly all aspects but handles wide character strings.
2577 The @code{wcstoull} function was introduced in @w{Amendment 1} of @w{ISO C90}.
2578 @end deftypefun
2580 @deftypefun {unsigned long long int} strtouq (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base})
2581 @standards{BSD, stdlib.h}
2582 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
2583 @code{strtouq} is the BSD name for @code{strtoull}.
2584 @end deftypefun
2586 @deftypefun {unsigned long long int} wcstouq (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base})
2587 @standards{GNU, wchar.h}
2588 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
2589 The @code{wcstouq} function is equivalent to the @code{strtouq} function
2590 in nearly all aspects but handles wide character strings.
2592 The @code{wcstouq} function is a GNU extension.
2593 @end deftypefun
2595 @deftypefun intmax_t strtoimax (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base})
2596 @standards{ISO, inttypes.h}
2597 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
2598 The @code{strtoimax} function is like @code{strtol} except that it returns
2599 a @code{intmax_t} value, and accepts numbers of a corresponding range.
2601 If the string has valid syntax for an integer but the value is not
2602 representable because of overflow, @code{strtoimax} returns either
2603 @code{INTMAX_MAX} or @code{INTMAX_MIN} (@pxref{Integers}), as
2604 appropriate for the sign of the value.  It also sets @code{errno} to
2605 @code{ERANGE} to indicate there was overflow.
2607 See @ref{Integers} for a description of the @code{intmax_t} type.  The
2608 @code{strtoimax} function was introduced in @w{ISO C99}.
2609 @end deftypefun
2611 @deftypefun intmax_t wcstoimax (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base})
2612 @standards{ISO, wchar.h}
2613 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
2614 The @code{wcstoimax} function is equivalent to the @code{strtoimax} function
2615 in nearly all aspects but handles wide character strings.
2617 The @code{wcstoimax} function was introduced in @w{ISO C99}.
2618 @end deftypefun
2620 @deftypefun uintmax_t strtoumax (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base})
2621 @standards{ISO, inttypes.h}
2622 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
2623 The @code{strtoumax} function is related to @code{strtoimax}
2624 the same way that @code{strtoul} is related to @code{strtol}.
2626 See @ref{Integers} for a description of the @code{intmax_t} type.  The
2627 @code{strtoumax} function was introduced in @w{ISO C99}.
2628 @end deftypefun
2630 @deftypefun uintmax_t wcstoumax (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base})
2631 @standards{ISO, wchar.h}
2632 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
2633 The @code{wcstoumax} function is equivalent to the @code{strtoumax} function
2634 in nearly all aspects but handles wide character strings.
2636 The @code{wcstoumax} function was introduced in @w{ISO C99}.
2637 @end deftypefun
2639 @deftypefun {long int} atol (const char *@var{string})
2640 @standards{ISO, stdlib.h}
2641 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
2642 This function is similar to the @code{strtol} function with a @var{base}
2643 argument of @code{10}, except that it need not detect overflow errors.
2644 The @code{atol} function is provided mostly for compatibility with
2645 existing code; using @code{strtol} is more robust.
2646 @end deftypefun
2648 @deftypefun int atoi (const char *@var{string})
2649 @standards{ISO, stdlib.h}
2650 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
2651 This function is like @code{atol}, except that it returns an @code{int}.
2652 The @code{atoi} function is also considered obsolete; use @code{strtol}
2653 instead.
2654 @end deftypefun
2656 @deftypefun {long long int} atoll (const char *@var{string})
2657 @standards{ISO, stdlib.h}
2658 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
2659 This function is similar to @code{atol}, except it returns a @code{long
2660 long int}.
2662 The @code{atoll} function was introduced in @w{ISO C99}.  It too is
2663 obsolete (despite having just been added); use @code{strtoll} instead.
2664 @end deftypefun
2666 All the functions mentioned in this section so far do not handle
2667 alternative representations of characters as described in the locale
2668 data.  Some locales specify thousands separator and the way they have to
2669 be used which can help to make large numbers more readable.  To read
2670 such numbers one has to use the @code{scanf} functions with the @samp{'}
2671 flag.
2673 Here is a function which parses a string as a sequence of integers and
2674 returns the sum of them:
2676 @smallexample
2678 sum_ints_from_string (char *string)
2680   int sum = 0;
2682   while (1) @{
2683     char *tail;
2684     int next;
2686     /* @r{Skip whitespace by hand, to detect the end.}  */
2687     while (isspace (*string)) string++;
2688     if (*string == 0)
2689       break;
2691     /* @r{There is more nonwhitespace,}  */
2692     /* @r{so it ought to be another number.}  */
2693     errno = 0;
2694     /* @r{Parse it.}  */
2695     next = strtol (string, &tail, 0);
2696     /* @r{Add it in, if not overflow.}  */
2697     if (errno)
2698       printf ("Overflow\n");
2699     else
2700       sum += next;
2701     /* @r{Advance past it.}  */
2702     string = tail;
2703   @}
2705   return sum;
2707 @end smallexample
2709 @node Parsing of Floats
2710 @subsection Parsing of Floats
2712 @pindex stdlib.h
2713 The @samp{str} functions are declared in @file{stdlib.h} and those
2714 beginning with @samp{wcs} are declared in @file{wchar.h}.  One might
2715 wonder about the use of @code{restrict} in the prototypes of the
2716 functions in this section.  It is seemingly useless but the @w{ISO C}
2717 standard uses it (for the functions defined there) so we have to do it
2718 as well.
2720 @deftypefun double strtod (const char *restrict @var{string}, char **restrict @var{tailptr})
2721 @standards{ISO, stdlib.h}
2722 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
2723 @c Besides the unsafe-but-ruled-safe locale uses, this uses a lot of
2724 @c mpn, but it's all safe.
2726 @c round_and_return
2727 @c   get_rounding_mode ok
2728 @c   mpn_add_1 ok
2729 @c   mpn_rshift ok
2730 @c   MPN_ZERO ok
2731 @c   MPN2FLOAT -> mpn_construct_(float|double|long_double) ok
2732 @c str_to_mpn
2733 @c   mpn_mul_1 -> umul_ppmm ok
2734 @c   mpn_add_1 ok
2735 @c mpn_lshift_1 -> mpn_lshift ok
2736 @c STRTOF_INTERNAL
2737 @c   MPN_VAR ok
2738 @c   SET_MANTISSA ok
2739 @c   STRNCASECMP ok, wide and narrow
2740 @c   round_and_return ok
2741 @c   mpn_mul ok
2742 @c     mpn_addmul_1 ok
2743 @c     ... mpn_sub
2744 @c   mpn_lshift ok
2745 @c   udiv_qrnnd ok
2746 @c   count_leading_zeros ok
2747 @c   add_ssaaaa ok
2748 @c   sub_ddmmss ok
2749 @c   umul_ppmm ok
2750 @c   mpn_submul_1 ok
2751 The @code{strtod} (``string-to-double'') function converts the initial
2752 part of @var{string} to a floating-point number, which is returned as a
2753 value of type @code{double}.
2755 This function attempts to decompose @var{string} as follows:
2757 @itemize @bullet
2758 @item
2759 A (possibly empty) sequence of whitespace characters.  Which characters
2760 are whitespace is determined by the @code{isspace} function
2761 (@pxref{Classification of Characters}).  These are discarded.
2763 @item
2764 An optional plus or minus sign (@samp{+} or @samp{-}).
2766 @item A floating point number in decimal or hexadecimal format.  The
2767 decimal format is:
2768 @itemize @minus
2770 @item
2771 A nonempty sequence of digits optionally containing a decimal-point
2772 character---normally @samp{.}, but it depends on the locale
2773 (@pxref{General Numeric}).
2775 @item
2776 An optional exponent part, consisting of a character @samp{e} or
2777 @samp{E}, an optional sign, and a sequence of digits.
2779 @end itemize
2781 The hexadecimal format is as follows:
2782 @itemize @minus
2784 @item
2785 A 0x or 0X followed by a nonempty sequence of hexadecimal digits
2786 optionally containing a decimal-point character---normally @samp{.}, but
2787 it depends on the locale (@pxref{General Numeric}).
2789 @item
2790 An optional binary-exponent part, consisting of a character @samp{p} or
2791 @samp{P}, an optional sign, and a sequence of digits.
2793 @end itemize
2795 @item
2796 Any remaining characters in the string.  If @var{tailptr} is not a null
2797 pointer, a pointer to this tail of the string is stored in
2798 @code{*@var{tailptr}}.
2799 @end itemize
2801 If the string is empty, contains only whitespace, or does not contain an
2802 initial substring that has the expected syntax for a floating-point
2803 number, no conversion is performed.  In this case, @code{strtod} returns
2804 a value of zero and the value returned in @code{*@var{tailptr}} is the
2805 value of @var{string}.
2807 In a locale other than the standard @code{"C"} or @code{"POSIX"} locales,
2808 this function may recognize additional locale-dependent syntax.
2810 If the string has valid syntax for a floating-point number but the value
2811 is outside the range of a @code{double}, @code{strtod} will signal
2812 overflow or underflow as described in @ref{Math Error Reporting}.
2814 @code{strtod} recognizes four special input strings.  The strings
2815 @code{"inf"} and @code{"infinity"} are converted to @math{@infinity{}},
2816 or to the largest representable value if the floating-point format
2817 doesn't support infinities.  You can prepend a @code{"+"} or @code{"-"}
2818 to specify the sign.  Case is ignored when scanning these strings.
2820 The strings @code{"nan"} and @code{"nan(@var{chars@dots{}})"} are converted
2821 to NaN.  Again, case is ignored.  If @var{chars@dots{}} are provided, they
2822 are used in some unspecified fashion to select a particular
2823 representation of NaN (there can be several).
2825 Since zero is a valid result as well as the value returned on error, you
2826 should check for errors in the same way as for @code{strtol}, by
2827 examining @var{errno} and @var{tailptr}.
2828 @end deftypefun
2830 @deftypefun float strtof (const char *@var{string}, char **@var{tailptr})
2831 @deftypefunx {long double} strtold (const char *@var{string}, char **@var{tailptr})
2832 @standards{ISO, stdlib.h}
2833 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
2834 @comment See safety comments for strtod.
2835 These functions are analogous to @code{strtod}, but return @code{float}
2836 and @code{long double} values respectively.  They report errors in the
2837 same way as @code{strtod}.  @code{strtof} can be substantially faster
2838 than @code{strtod}, but has less precision; conversely, @code{strtold}
2839 can be much slower but has more precision (on systems where @code{long
2840 double} is a separate type).
2842 These functions have been GNU extensions and are new to @w{ISO C99}.
2843 @end deftypefun
2845 @deftypefun _FloatN strtofN (const char *@var{string}, char **@var{tailptr})
2846 @deftypefunx _FloatNx strtofNx (const char *@var{string}, char **@var{tailptr})
2847 @standards{ISO/IEC TS 18661-3, stdlib.h}
2848 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
2849 @comment See safety comments for strtod.
2850 These functions are like @code{strtod}, except for the return type.
2852 They were introduced in @w{ISO/IEC TS 18661-3} and are available on machines
2853 that support the related types; @pxref{Mathematics}.
2854 @end deftypefun
2856 @deftypefun double wcstod (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr})
2857 @deftypefunx float wcstof (const wchar_t *@var{string}, wchar_t **@var{tailptr})
2858 @deftypefunx {long double} wcstold (const wchar_t *@var{string}, wchar_t **@var{tailptr})
2859 @deftypefunx _FloatN wcstofN (const wchar_t *@var{string}, wchar_t **@var{tailptr})
2860 @deftypefunx _FloatNx wcstofNx (const wchar_t *@var{string}, wchar_t **@var{tailptr})
2861 @standards{ISO, wchar.h}
2862 @standardsx{wcstofN, GNU, wchar.h}
2863 @standardsx{wcstofNx, GNU, wchar.h}
2864 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
2865 @comment See safety comments for strtod.
2866 The @code{wcstod}, @code{wcstof}, @code{wcstol}, @code{wcstof@var{N}},
2867 and @code{wcstof@var{N}x} functions are equivalent in nearly all aspects
2868 to the @code{strtod}, @code{strtof}, @code{strtold},
2869 @code{strtof@var{N}}, and @code{strtof@var{N}x} functions, but they
2870 handle wide character strings.
2872 The @code{wcstod} function was introduced in @w{Amendment 1} of @w{ISO
2873 C90}.  The @code{wcstof} and @code{wcstold} functions were introduced in
2874 @w{ISO C99}.
2876 The @code{wcstof@var{N}} and @code{wcstof@var{N}x} functions are not in
2877 any standard, but are added to provide completeness for the
2878 non-deprecated interface of wide character string to floating-point
2879 conversion functions.  They are only available on machines that support
2880 the related types; @pxref{Mathematics}.
2881 @end deftypefun
2883 @deftypefun double atof (const char *@var{string})
2884 @standards{ISO, stdlib.h}
2885 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
2886 This function is similar to the @code{strtod} function, except that it
2887 need not detect overflow and underflow errors.  The @code{atof} function
2888 is provided mostly for compatibility with existing code; using
2889 @code{strtod} is more robust.
2890 @end deftypefun
2892 @Theglibc{} also provides @samp{_l} versions of these functions,
2893 which take an additional argument, the locale to use in conversion.
2895 See also @ref{Parsing of Integers}.
2897 @node Printing of Floats
2898 @section Printing of Floats
2900 @pindex stdlib.h
2901 The @samp{strfrom} functions are declared in @file{stdlib.h}.
2903 @deftypefun int strfromd (char *restrict @var{string}, size_t @var{size}, const char *restrict @var{format}, double @var{value})
2904 @deftypefunx int strfromf (char *restrict @var{string}, size_t @var{size}, const char *restrict @var{format}, float @var{value})
2905 @deftypefunx int strfroml (char *restrict @var{string}, size_t @var{size}, const char *restrict @var{format}, long double @var{value})
2906 @standards{ISO/IEC TS 18661-1, stdlib.h}
2907 @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
2908 @comment All these functions depend on both __printf_fp and __printf_fphex,
2909 @comment which are both AS-unsafe (ascuheap) and AC-unsafe (acsmem).
2910 The functions @code{strfromd} (``string-from-double''), @code{strfromf}
2911 (``string-from-float''), and @code{strfroml} (``string-from-long-double'')
2912 convert the floating-point number @var{value} to a string of characters and
2913 stores them into the area pointed to by @var{string}.  The conversion
2914 writes at most @var{size} characters and respects the format specified by
2915 @var{format}.
2917 The format string must start with the character @samp{%}.  An optional
2918 precision follows, which starts with a period, @samp{.}, and may be
2919 followed by a decimal integer, representing the precision.  If a decimal
2920 integer is not specified after the period, the precision is taken to be
2921 zero.  The character @samp{*} is not allowed.  Finally, the format string
2922 ends with one of the following conversion specifiers: @samp{a}, @samp{A},
2923 @samp{e}, @samp{E}, @samp{f}, @samp{F}, @samp{g} or @samp{G} (@pxref{Table
2924 of Output Conversions}).  Invalid format strings result in undefined
2925 behavior.
2927 These functions return the number of characters that would have been
2928 written to @var{string} had @var{size} been sufficiently large, not
2929 counting the terminating null character.  Thus, the null-terminated output
2930 has been completely written if and only if the returned value is less than
2931 @var{size}.
2933 These functions were introduced by ISO/IEC TS 18661-1.
2934 @end deftypefun
2936 @deftypefun int strfromfN (char *restrict @var{string}, size_t @var{size}, const char *restrict @var{format}, _Float@var{N} @var{value})
2937 @deftypefunx int strfromfNx (char *restrict @var{string}, size_t @var{size}, const char *restrict @var{format}, _Float@var{N}x @var{value})
2938 @standards{ISO/IEC TS 18661-3, stdlib.h}
2939 @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
2940 @comment See safety comments for strfromd.
2941 These functions are like @code{strfromd}, except for the type of
2942 @code{value}.
2944 They were introduced in @w{ISO/IEC TS 18661-3} and are available on machines
2945 that support the related types; @pxref{Mathematics}.
2946 @end deftypefun
2948 @node System V Number Conversion
2949 @section Old-fashioned System V number-to-string functions
2951 The old @w{System V} C library provided three functions to convert
2952 numbers to strings, with unusual and hard-to-use semantics.  @Theglibc{}
2953 also provides these functions and some natural extensions.
2955 These functions are only available in @theglibc{} and on systems descended
2956 from AT&T Unix.  Therefore, unless these functions do precisely what you
2957 need, it is better to use @code{sprintf}, which is standard.
2959 All these functions are defined in @file{stdlib.h}.
2961 @deftypefun {char *} ecvt (double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg})
2962 @standards{SVID, stdlib.h}
2963 @standards{Unix98, stdlib.h}
2964 @safety{@prelim{}@mtunsafe{@mtasurace{:ecvt}}@asunsafe{}@acsafe{}}
2965 The function @code{ecvt} converts the floating-point number @var{value}
2966 to a string with at most @var{ndigit} decimal digits.  The
2967 returned string contains no decimal point or sign.  The first digit of
2968 the string is non-zero (unless @var{value} is actually zero) and the
2969 last digit is rounded to nearest.  @code{*@var{decpt}} is set to the
2970 index in the string of the first digit after the decimal point.
2971 @code{*@var{neg}} is set to a nonzero value if @var{value} is negative,
2972 zero otherwise.
2974 If @var{ndigit} decimal digits would exceed the precision of a
2975 @code{double} it is reduced to a system-specific value.
2977 The returned string is statically allocated and overwritten by each call
2978 to @code{ecvt}.
2980 If @var{value} is zero, it is implementation defined whether
2981 @code{*@var{decpt}} is @code{0} or @code{1}.
2983 For example: @code{ecvt (12.3, 5, &d, &n)} returns @code{"12300"}
2984 and sets @var{d} to @code{2} and @var{n} to @code{0}.
2985 @end deftypefun
2987 @deftypefun {char *} fcvt (double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg})
2988 @standards{SVID, stdlib.h}
2989 @standards{Unix98, stdlib.h}
2990 @safety{@prelim{}@mtunsafe{@mtasurace{:fcvt}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
2991 The function @code{fcvt} is like @code{ecvt}, but @var{ndigit} specifies
2992 the number of digits after the decimal point.  If @var{ndigit} is less
2993 than zero, @var{value} is rounded to the @math{@var{ndigit}+1}'th place to the
2994 left of the decimal point.  For example, if @var{ndigit} is @code{-1},
2995 @var{value} will be rounded to the nearest 10.  If @var{ndigit} is
2996 negative and larger than the number of digits to the left of the decimal
2997 point in @var{value}, @var{value} will be rounded to one significant digit.
2999 If @var{ndigit} decimal digits would exceed the precision of a
3000 @code{double} it is reduced to a system-specific value.
3002 The returned string is statically allocated and overwritten by each call
3003 to @code{fcvt}.
3004 @end deftypefun
3006 @deftypefun {char *} gcvt (double @var{value}, int @var{ndigit}, char *@var{buf})
3007 @standards{SVID, stdlib.h}
3008 @standards{Unix98, stdlib.h}
3009 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3010 @c gcvt calls sprintf, that ultimately calls vfprintf, which malloc()s
3011 @c args_value if it's too large, but gcvt never exercises this path.
3012 @code{gcvt} is functionally equivalent to @samp{sprintf(buf, "%*g",
3013 ndigit, value}.  It is provided only for compatibility's sake.  It
3014 returns @var{buf}.
3016 If @var{ndigit} decimal digits would exceed the precision of a
3017 @code{double} it is reduced to a system-specific value.
3018 @end deftypefun
3020 As extensions, @theglibc{} provides versions of these three
3021 functions that take @code{long double} arguments.
3023 @deftypefun {char *} qecvt (long double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg})
3024 @standards{GNU, stdlib.h}
3025 @safety{@prelim{}@mtunsafe{@mtasurace{:qecvt}}@asunsafe{}@acsafe{}}
3026 This function is equivalent to @code{ecvt} except that it takes a
3027 @code{long double} for the first parameter and that @var{ndigit} is
3028 restricted by the precision of a @code{long double}.
3029 @end deftypefun
3031 @deftypefun {char *} qfcvt (long double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg})
3032 @standards{GNU, stdlib.h}
3033 @safety{@prelim{}@mtunsafe{@mtasurace{:qfcvt}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
3034 This function is equivalent to @code{fcvt} except that it
3035 takes a @code{long double} for the first parameter and that @var{ndigit} is
3036 restricted by the precision of a @code{long double}.
3037 @end deftypefun
3039 @deftypefun {char *} qgcvt (long double @var{value}, int @var{ndigit}, char *@var{buf})
3040 @standards{GNU, stdlib.h}
3041 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3042 This function is equivalent to @code{gcvt} except that it takes a
3043 @code{long double} for the first parameter and that @var{ndigit} is
3044 restricted by the precision of a @code{long double}.
3045 @end deftypefun
3048 @cindex gcvt_r
3049 The @code{ecvt} and @code{fcvt} functions, and their @code{long double}
3050 equivalents, all return a string located in a static buffer which is
3051 overwritten by the next call to the function.  @Theglibc{}
3052 provides another set of extended functions which write the converted
3053 string into a user-supplied buffer.  These have the conventional
3054 @code{_r} suffix.
3056 @code{gcvt_r} is not necessary, because @code{gcvt} already uses a
3057 user-supplied buffer.
3059 @deftypefun int ecvt_r (double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg}, char *@var{buf}, size_t @var{len})
3060 @standards{GNU, stdlib.h}
3061 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3062 The @code{ecvt_r} function is the same as @code{ecvt}, except
3063 that it places its result into the user-specified buffer pointed to by
3064 @var{buf}, with length @var{len}.  The return value is @code{-1} in
3065 case of an error and zero otherwise.
3067 This function is a GNU extension.
3068 @end deftypefun
3070 @deftypefun int fcvt_r (double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg}, char *@var{buf}, size_t @var{len})
3071 @standards{SVID, stdlib.h}
3072 @standards{Unix98, stdlib.h}
3073 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3074 The @code{fcvt_r} function is the same as @code{fcvt}, except that it
3075 places its result into the user-specified buffer pointed to by
3076 @var{buf}, with length @var{len}.  The return value is @code{-1} in
3077 case of an error and zero otherwise.
3079 This function is a GNU extension.
3080 @end deftypefun
3082 @deftypefun int qecvt_r (long double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg}, char *@var{buf}, size_t @var{len})
3083 @standards{GNU, stdlib.h}
3084 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3085 The @code{qecvt_r} function is the same as @code{qecvt}, except
3086 that it places its result into the user-specified buffer pointed to by
3087 @var{buf}, with length @var{len}.  The return value is @code{-1} in
3088 case of an error and zero otherwise.
3090 This function is a GNU extension.
3091 @end deftypefun
3093 @deftypefun int qfcvt_r (long double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg}, char *@var{buf}, size_t @var{len})
3094 @standards{GNU, stdlib.h}
3095 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3096 The @code{qfcvt_r} function is the same as @code{qfcvt}, except
3097 that it places its result into the user-specified buffer pointed to by
3098 @var{buf}, with length @var{len}.  The return value is @code{-1} in
3099 case of an error and zero otherwise.
3101 This function is a GNU extension.
3102 @end deftypefun