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