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