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