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