]> git.ipfire.org Git - thirdparty/glibc.git/blame - manual/arith.texi
Update.
[thirdparty/glibc.git] / manual / arith.texi
CommitLineData
28f540f4
RM
1@node Arithmetic, Date and Time, Mathematics, Top
2@chapter Low-Level Arithmetic Functions
3
4This chapter contains information about functions for doing basic
5arithmetic operations, such as splitting a float into its integer and
b4012b75
UD
6fractional parts or retrieving the imaginary part of a complex value.
7These functions are declared in the header files @file{math.h} and
8@file{complex.h}.
28f540f4
RM
9
10@menu
b4012b75 11* Infinity:: What is Infinity and how to test for it.
28f540f4 12* Not a Number:: Making NaNs and testing for NaNs.
b4012b75 13* Imaginary Unit:: Constructing complex Numbers.
28f540f4 14* Predicates on Floats:: Testing for infinity and for NaNs.
b4012b75
UD
15* Floating-Point Classes:: Classifiy floating-point numbers.
16* Operations on Complex:: Projections, Conjugates, and Decomposing.
28f540f4
RM
17* Absolute Value:: Absolute value functions.
18* Normalization Functions:: Hacks for radix-2 representations.
6d52618b 19* Rounding and Remainders:: Determining the integer and
28f540f4
RM
20 fractional parts of a float.
21* Integer Division:: Functions for performing integer
22 division.
23* Parsing of Numbers:: Functions for ``reading'' numbers
24 from strings.
25@end menu
26
b4012b75
UD
27@node Infinity
28@section Infinity Values
29@cindex Infinity
30@cindex IEEE floating point
31
32Mathematical operations easily can produce as the result values which
33are not representable by the floating-point format. The functions in
34the mathematics library also have this problem. The situation is
35generally solved by raising an overflow exception and by returning a
36huge value.
37
38The @w{IEEE 754} floating-point defines a special value to be used in
39these situations. There is a special value for infinity.
40
41@comment math.h
42@comment ISO
43@deftypevr Macro float_t INFINITY
44A expression representing the inifite value. @code{INFINITY} values are
45produce by mathematical operations like @code{1.0 / 0.0}. It is
46possible to continue the computations with this value since the basic
47operations as well as the mathematical library functions are prepared to
48handle values like this.
49
50Beside @code{INFINITY} also the value @code{-INIFITY} is representable
51and it is handled differently if needed. It is possible to test a
52variables for infinite value using a simple comparison but the
53recommended way is to use the the @code{isinf} function.
54
55This macro was introduced in the @w{ISO C 9X} standard.
56@end deftypevr
57
58@vindex HUGE_VAL
59The macros @code{HUGE_VAL}, @code{HUGE_VALF} and @code{HUGE_VALL} are
60defined in a similar way but they are not required to represent the
61infinite value, only a very large value (@pxref{Domain and Range Errors}).
62If actually infinity is wanted, @code{INFINITY} should be used.
63
64
28f540f4
RM
65@node Not a Number
66@section ``Not a Number'' Values
67@cindex NaN
68@cindex not a number
69@cindex IEEE floating point
70
71The IEEE floating point format used by most modern computers supports
72values that are ``not a number''. These values are called @dfn{NaNs}.
73``Not a number'' values result from certain operations which have no
74meaningful numeric result, such as zero divided by zero or infinity
75divided by infinity.
76
77One noteworthy property of NaNs is that they are not equal to
78themselves. Thus, @code{x == x} can be 0 if the value of @code{x} is a
79NaN. You can use this to test whether a value is a NaN or not: if it is
80not equal to itself, then it is a NaN. But the recommended way to test
81for a NaN is with the @code{isnan} function (@pxref{Predicates on Floats}).
82
83Almost any arithmetic operation in which one argument is a NaN returns
84a NaN.
85
86@comment math.h
87@comment GNU
88@deftypevr Macro double NAN
89An expression representing a value which is ``not a number''. This
90macro is a GNU extension, available only on machines that support ``not
91a number'' values---that is to say, on all machines that support IEEE
92floating point.
93
94You can use @samp{#ifdef NAN} to test whether the machine supports
95NaNs. (Of course, you must arrange for GNU extensions to be visible,
96such as by defining @code{_GNU_SOURCE}, and then you must include
97@file{math.h}.)
98@end deftypevr
99
b4012b75
UD
100@node Imaginary Unit
101@section Constructing complex Numbers
102
103@pindex complex.h
104To construct complex numbers it is necessary have a way to express the
105imaginary part of the numbers. In mathematics one uses the symbol ``i''
106to mark a number as imaginary. For convenienve the @file{complex.h}
107header defines two macros which allow to use a similar easy notation.
108
109@deftypevr Macro float_t _Imaginary_I
110This macro is a (compiler specific) representation of the value ``1i''.
111I.e., it is the value for which
112
113@smallexample
114_Imaginary_I * _Imaginary_I = -1
115@end smallexample
116
117@noindent
118One can use it to easily construct complex number like in
119
120@smallexample
1213.0 - _Imaginary_I * 4.0
122@end smallexample
123
124@noindent
125which results in the complex number with a real part of 3.0 and a
126imaginary part -4.0.
127@end deftypevr
128
129@noindent
130A more intuitive approach is to use the following macro.
131
132@deftypevr Macro float_t I
133This macro has exactly the same value as @code{_Imaginary_I}. The
134problem is that the name @code{I} very easily can clash with macros or
135variables in programs and so it might be a good idea to avoid this name
136and stay at the safe side by using @code{_Imaginary_I}.
137@end deftypevr
138
139
28f540f4
RM
140@node Predicates on Floats
141@section Predicates on Floats
142
143@pindex math.h
144This section describes some miscellaneous test functions on doubles.
145Prototypes for these functions appear in @file{math.h}. These are BSD
146functions, and thus are available if you define @code{_BSD_SOURCE} or
147@code{_GNU_SOURCE}.
148
149@comment math.h
150@comment BSD
151@deftypefun int isinf (double @var{x})
779ae82e
UD
152@deftypefunx int isinff (float @var{x})
153@deftypefunx int isinfl (long double @var{x})
28f540f4
RM
154This function returns @code{-1} if @var{x} represents negative infinity,
155@code{1} if @var{x} represents positive infinity, and @code{0} otherwise.
156@end deftypefun
157
158@comment math.h
159@comment BSD
160@deftypefun int isnan (double @var{x})
779ae82e
UD
161@deftypefunx int isnanf (float @var{x})
162@deftypefunx int isnanl (long double @var{x})
28f540f4
RM
163This function returns a nonzero value if @var{x} is a ``not a number''
164value, and zero otherwise. (You can just as well use @code{@var{x} !=
165@var{x}} to get the same result).
166@end deftypefun
167
168@comment math.h
169@comment BSD
170@deftypefun int finite (double @var{x})
779ae82e
UD
171@deftypefunx int finitef (float @var{x})
172@deftypefunx int finitel (long double @var{x})
28f540f4
RM
173This function returns a nonzero value if @var{x} is finite or a ``not a
174number'' value, and zero otherwise.
175@end deftypefun
176
177@comment math.h
178@comment BSD
179@deftypefun double infnan (int @var{error})
180This function is provided for compatibility with BSD. The other
181mathematical functions use @code{infnan} to decide what to return on
182occasion of an error. Its argument is an error code, @code{EDOM} or
183@code{ERANGE}; @code{infnan} returns a suitable value to indicate this
184with. @code{-ERANGE} is also acceptable as an argument, and corresponds
185to @code{-HUGE_VAL} as a value.
186
187In the BSD library, on certain machines, @code{infnan} raises a fatal
188signal in all cases. The GNU library does not do likewise, because that
f65fd747 189does not fit the @w{ISO C} specification.
28f540f4
RM
190@end deftypefun
191
192@strong{Portability Note:} The functions listed in this section are BSD
193extensions.
194
b4012b75
UD
195@node Floating-Point Classes
196@section Floating-Point Number Classification Functions
197
198Instead of using the BSD specific functions from the last section it is
714a562f 199better to use those in this section which are introduced in the @w{ISO C
b4012b75
UD
2009X} standard and are therefore widely available.
201
202@comment math.h
203@comment ISO
55c14926 204@deftypefn {Macro} int fpclassify (@emph{float-type} @var{x})
b4012b75
UD
205This is a generic macro which works on all floating-point types and
206which returns a value of type @code{int}. The possible values are:
207
208@vtable @code
209@item FP_NAN
779ae82e 210The floating-point number @var{x} is ``Not a Number'' (@pxref{Not a Number})
b4012b75 211@item FP_INFINITE
779ae82e 212The value of @var{x} is either plus or minus infinity (@pxref{Infinity})
b4012b75 213@item FP_ZERO
779ae82e
UD
214The value of @var{x} is zero. In floating-point formats like @w{IEEE
215754} where the zero value can be signed this value is also returned if
216@var{x} is minus zero.
b4012b75 217@item FP_SUBNORMAL
779ae82e
UD
218Some floating-point formats (such as @w{IEEE 754}) allow floating-point
219numbers to be represented in a denormalized format. This happens if the
220absolute value of the number is too small to be represented in the
221normal format. @code{FP_SUBNORMAL} is returned for such values of @var{x}.
b4012b75 222@item FP_NORMAL
779ae82e
UD
223This value is returned for all other cases which means the number is a
224plain floating-point number without special meaning.
b4012b75
UD
225@end vtable
226
227This macro is useful if more than property of a number must be
228tested. If one only has to test for, e.g., a NaN value, there are
229function which are faster.
55c14926 230@end deftypefn
b4012b75
UD
231
232The remainder of this section introduces some more specific functions.
233They might be implemented faster than the call to @code{fpclassify} and
234if the actual need in the program is covered be these functions they
235should be used (and not @code{fpclassify}).
236
237@comment math.h
238@comment ISO
55c14926 239@deftypefn {Macro} int isfinite (@emph{float-type} @var{x})
b4012b75
UD
240The value returned by this macro is nonzero if the value of @var{x} is
241not plus or minus infinity and not NaN. I.e., it could be implemented as
242
243@smallexample
244(fpclassify (x) != FP_NAN && fpclassify (x) != FP_INFINITE)
245@end smallexample
246
247@code{isfinite} is also implemented as a macro which can handle all
248floating-point types. Programs should use this function instead of
249@var{finite} (@pxref{Predicates on Floats}).
55c14926 250@end deftypefn
b4012b75
UD
251
252@comment math.h
253@comment ISO
55c14926 254@deftypefn {Macro} int isnormal (@emph{float-type} @var{x})
b4012b75
UD
255If @code{isnormal} returns a nonzero value the value or @var{x} is
256neither a NaN, infinity, zero, nor a denormalized number. I.e., it
257could be implemented as
258
259@smallexample
260(fpclassify (x) == FP_NORMAL)
261@end smallexample
55c14926 262@end deftypefn
b4012b75
UD
263
264@comment math.h
265@comment ISO
55c14926 266@deftypefn {Macro} int isnan (@emph{float-type} @var{x})
b4012b75
UD
267The situation with this macro is a bit complicated. Here @code{isnan}
268is a macro which can handle all kinds of floating-point types. It
269returns a nonzero value is @var{x} does not represent a NaN value and
270could be written like this
271
272@smallexample
273(fpclassify (x) == FP_NAN)
274@end smallexample
275
276The complication is that there is a function of the same name and the
277same semantic defined for compatibility with BSD (@pxref{Predicates on
278Floats}). Fortunately this should not yield to problems in most cases
279since the macro and the function have the same semantic. Should in a
280situation the function be absolutely necessary one can use
281
282@smallexample
283(isnan) (x)
284@end smallexample
285
286@noindent
287to avoid the macro expansion. Using the macro has two big adavantages:
288it is more portable and one does not have to choose the right function
289among @code{isnan}, @code{isnanf}, and @code{isnanl}.
55c14926 290@end deftypefn
b4012b75
UD
291
292
293@node Operations on Complex
294@section Projections, Conjugates, and Decomposing of Complex Numbers
295@cindex project complex numbers
296@cindex conjugate complex numbers
297@cindex decompose complex numbers
298
299This section lists functions performing some of the simple mathematical
300operations on complex numbers. Using any of the function requries that
301the C compiler understands the @code{complex} keyword, introduced to the
302C language in the @w{ISO C 9X} standard.
303
304@pindex complex.h
305The prototypes for all functions in this section can be found in
306@file{complex.h}. All functions are available in three variants, one
307for each of the three floating-point types.
308
309The easiest operation on complex numbers is the decomposition in the
310real part and the imaginary part. This is done by the next two
311functions.
312
313@comment complex.h
314@comment ISO
315@deftypefun double creal (complex double @var{z})
779ae82e
UD
316@deftypefunx float crealf (complex float @var{z})
317@deftypefunx {long double} creall (complex long double @var{z})
b4012b75
UD
318These functions return the real part of the complex number @var{z}.
319@end deftypefun
320
321@comment complex.h
322@comment ISO
323@deftypefun double cimag (complex double @var{z})
779ae82e
UD
324@deftypefunx float cimagf (complex float @var{z})
325@deftypefunx {long double} cimagl (complex long double @var{z})
b4012b75
UD
326These functions return the imaginary part of the complex number @var{z}.
327@end deftypefun
328
329
330The conjugate complex value of a given complex number has the same value
331for the real part but the complex part is negated.
332
333@comment complex.h
334@comment ISO
335@deftypefun {complex double} conj (complex double @var{z})
779ae82e
UD
336@deftypefunx {complex float} conjf (complex float @var{z})
337@deftypefunx {complex long double} conjl (complex long double @var{z})
b4012b75
UD
338These functions return the conjugate complex value of the complex number
339@var{z}.
340@end deftypefun
341
342@comment complex.h
343@comment ISO
344@deftypefun double carg (complex double @var{z})
779ae82e
UD
345@deftypefunx float cargf (complex float @var{z})
346@deftypefunx {long double} cargl (complex long double @var{z})
b4012b75
UD
347These functions return argument of the complex number @var{z}.
348
349Mathematically, the argument is the phase angle of @var{z} with a branch
350cut along the negative real axis.
351@end deftypefun
352
353@comment complex.h
354@comment ISO
355@deftypefun {complex double} cproj (complex double @var{z})
779ae82e
UD
356@deftypefunx {complex float} cprojf (complex float @var{z})
357@deftypefunx {complex long double} cprojl (complex long double @var{z})
b4012b75
UD
358Return the projection of the complex value @var{z} on the Riemann
359sphere. Values with a infinite complex part (even if the real part
360is NaN) are projected to positive infinte on the real axis. If the real part is infinite, the result is equivalent to
361
362@smallexample
363INFINITY + I * copysign (0.0, cimag (z))
364@end smallexample
365@end deftypefun
366
367
28f540f4
RM
368@node Absolute Value
369@section Absolute Value
370@cindex absolute value functions
371
372These functions are provided for obtaining the @dfn{absolute value} (or
373@dfn{magnitude}) of a number. The absolute value of a real number
374@var{x} is @var{x} is @var{x} is positive, @minus{}@var{x} if @var{x} is
375negative. For a complex number @var{z}, whose real part is @var{x} and
376whose imaginary part is @var{y}, the absolute value is @w{@code{sqrt
377(@var{x}*@var{x} + @var{y}*@var{y})}}.
378
379@pindex math.h
380@pindex stdlib.h
381Prototypes for @code{abs} and @code{labs} are in @file{stdlib.h};
b4012b75
UD
382@code{fabs}, @code{fabsf} and @code{fabsl} are declared in @file{math.h};
383@code{cabs}, @code{cabsf} and @code{cabsl} are declared in @file{complex.h}.
28f540f4
RM
384
385@comment stdlib.h
f65fd747 386@comment ISO
28f540f4
RM
387@deftypefun int abs (int @var{number})
388This function returns the absolute value of @var{number}.
389
390Most computers use a two's complement integer representation, in which
391the absolute value of @code{INT_MIN} (the smallest possible @code{int})
392cannot be represented; thus, @w{@code{abs (INT_MIN)}} is not defined.
393@end deftypefun
394
395@comment stdlib.h
f65fd747 396@comment ISO
28f540f4
RM
397@deftypefun {long int} labs (long int @var{number})
398This is similar to @code{abs}, except that both the argument and result
399are of type @code{long int} rather than @code{int}.
400@end deftypefun
401
402@comment math.h
f65fd747 403@comment ISO
28f540f4 404@deftypefun double fabs (double @var{number})
779ae82e
UD
405@deftypefunx float fabsf (float @var{number})
406@deftypefunx {long double} fabsl (long double @var{number})
28f540f4
RM
407This function returns the absolute value of the floating-point number
408@var{number}.
409@end deftypefun
410
b4012b75
UD
411@comment complex.h
412@comment ISO
413@deftypefun double cabs (complex double @var{z})
779ae82e
UD
414@deftypefunx float cabsf (complex float @var{z})
415@deftypefunx {long double} cabsl (complex long double @var{z})
b4012b75
UD
416These functions return the absolute value of the complex number @var{z}.
417The compiler must support complex numbers to use these functions. (See
418also the function @code{hypot} in @ref{Exponents and Logarithms}.) The
419value is:
28f540f4
RM
420
421@smallexample
b4012b75 422sqrt (creal (@var{z}) * creal (@var{z}) + cimag (@var{z}) * cimag (@var{z}))
28f540f4
RM
423@end smallexample
424@end deftypefun
425
426@node Normalization Functions
427@section Normalization Functions
428@cindex normalization functions (floating-point)
429
430The functions described in this section are primarily provided as a way
431to efficiently perform certain low-level manipulations on floating point
432numbers that are represented internally using a binary radix;
433see @ref{Floating Point Concepts}. These functions are required to
434have equivalent behavior even if the representation does not use a radix
435of 2, but of course they are unlikely to be particularly efficient in
436those cases.
437
438@pindex math.h
439All these functions are declared in @file{math.h}.
440
441@comment math.h
f65fd747 442@comment ISO
28f540f4 443@deftypefun double frexp (double @var{value}, int *@var{exponent})
779ae82e
UD
444@deftypefunx float frexpf (float @var{value}, int *@var{exponent})
445@deftypefunx {long double} frexpl (long double @var{value}, int *@var{exponent})
b4012b75 446These functions are used to split the number @var{value}
28f540f4
RM
447into a normalized fraction and an exponent.
448
449If the argument @var{value} is not zero, the return value is @var{value}
450times a power of two, and is always in the range 1/2 (inclusive) to 1
451(exclusive). The corresponding exponent is stored in
452@code{*@var{exponent}}; the return value multiplied by 2 raised to this
453exponent equals the original number @var{value}.
454
455For example, @code{frexp (12.8, &exponent)} returns @code{0.8} and
456stores @code{4} in @code{exponent}.
457
458If @var{value} is zero, then the return value is zero and
459zero is stored in @code{*@var{exponent}}.
460@end deftypefun
461
462@comment math.h
f65fd747 463@comment ISO
28f540f4 464@deftypefun double ldexp (double @var{value}, int @var{exponent})
779ae82e
UD
465@deftypefunx float ldexpf (float @var{value}, int @var{exponent})
466@deftypefunx {long double} ldexpl (long double @var{value}, int @var{exponent})
b4012b75 467These functions return the result of multiplying the floating-point
28f540f4
RM
468number @var{value} by 2 raised to the power @var{exponent}. (It can
469be used to reassemble floating-point numbers that were taken apart
470by @code{frexp}.)
471
472For example, @code{ldexp (0.8, 4)} returns @code{12.8}.
473@end deftypefun
474
475The following functions which come from BSD provide facilities
476equivalent to those of @code{ldexp} and @code{frexp}:
477
478@comment math.h
479@comment BSD
480@deftypefun double scalb (double @var{value}, int @var{exponent})
779ae82e
UD
481@deftypefunx float scalbf (float @var{value}, int @var{exponent})
482@deftypefunx {long double} scalbl (long double @var{value}, int @var{exponent})
28f540f4
RM
483The @code{scalb} function is the BSD name for @code{ldexp}.
484@end deftypefun
485
486@comment math.h
487@comment BSD
488@deftypefun double logb (double @var{x})
779ae82e
UD
489@deftypefunx float logbf (float @var{x})
490@deftypefunx {long double} logbl (long double @var{x})
b4012b75 491These BSD functions return the integer part of the base-2 logarithm of
28f540f4
RM
492@var{x}, an integer value represented in type @code{double}. This is
493the highest integer power of @code{2} contained in @var{x}. The sign of
494@var{x} is ignored. For example, @code{logb (3.5)} is @code{1.0} and
495@code{logb (4.0)} is @code{2.0}.
496
497When @code{2} raised to this power is divided into @var{x}, it gives a
498quotient between @code{1} (inclusive) and @code{2} (exclusive).
499
500If @var{x} is zero, the value is minus infinity (if the machine supports
501such a value), or else a very small number. If @var{x} is infinity, the
502value is infinity.
503
504The value returned by @code{logb} is one less than the value that
505@code{frexp} would store into @code{*@var{exponent}}.
506@end deftypefun
507
508@comment math.h
b4012b75 509@comment ISO
28f540f4 510@deftypefun double copysign (double @var{value}, double @var{sign})
779ae82e
UD
511@deftypefunx float copysignf (float @var{value}, float @var{sign})
512@deftypefunx {long double} copysignl (long double @var{value}, long double @var{sign})
b4012b75 513These functions return a value whose absolute value is the
28f540f4 514same as that of @var{value}, and whose sign matches that of @var{sign}.
b4012b75
UD
515This function appears in BSD and was standardized in @w{ISO C 9X}.
516@end deftypefun
517
518@comment math.h
519@comment ISO
520@deftypefun int signbit (@emph{float-type} @var{x})
521@code{signbit} is a generic macro which can work on all floating-point
522types. It returns a nonzero value if the value of @var{x} has its sign
523bit set.
524
525This is not the same as @code{x < 0.0} since in some floating-point
526formats (e.g., @w{IEEE 754}) the zero value is optionally signed. The
527comparison @code{-0.0 < 0.0} will not be true while @code{signbit
528(-0.0)} will return a nonzeri value.
28f540f4
RM
529@end deftypefun
530
531@node Rounding and Remainders
532@section Rounding and Remainder Functions
533@cindex rounding functions
534@cindex remainder functions
535@cindex converting floats to integers
536
537@pindex math.h
538The functions listed here perform operations such as rounding,
539truncation, and remainder in division of floating point numbers. Some
540of these functions convert floating point numbers to integer values.
541They are all declared in @file{math.h}.
542
543You can also convert floating-point numbers to integers simply by
544casting them to @code{int}. This discards the fractional part,
545effectively rounding towards zero. However, this only works if the
546result can actually be represented as an @code{int}---for very large
547numbers, this is impossible. The functions listed here return the
548result as a @code{double} instead to get around this problem.
549
550@comment math.h
f65fd747 551@comment ISO
28f540f4 552@deftypefun double ceil (double @var{x})
779ae82e
UD
553@deftypefunx float ceilf (float @var{x})
554@deftypefunx {long double} ceill (long double @var{x})
b4012b75 555These functions round @var{x} upwards to the nearest integer,
28f540f4
RM
556returning that value as a @code{double}. Thus, @code{ceil (1.5)}
557is @code{2.0}.
558@end deftypefun
559
560@comment math.h
f65fd747 561@comment ISO
28f540f4 562@deftypefun double floor (double @var{x})
779ae82e
UD
563@deftypefunx float floorf (float @var{x})
564@deftypefunx {long double} floorl (long double @var{x})
b4012b75 565These functions round @var{x} downwards to the nearest
28f540f4
RM
566integer, returning that value as a @code{double}. Thus, @code{floor
567(1.5)} is @code{1.0} and @code{floor (-1.5)} is @code{-2.0}.
568@end deftypefun
569
570@comment math.h
b4012b75 571@comment ISO
28f540f4 572@deftypefun double rint (double @var{x})
779ae82e
UD
573@deftypefunx float rintf (float @var{x})
574@deftypefunx {long double} rintl (long double @var{x})
b4012b75 575These functions round @var{x} to an integer value according to the
28f540f4
RM
576current rounding mode. @xref{Floating Point Parameters}, for
577information about the various rounding modes. The default
578rounding mode is to round to the nearest integer; some machines
579support other modes, but round-to-nearest is always used unless
580you explicit select another.
581@end deftypefun
582
b4012b75
UD
583@comment math.h
584@comment ISO
585@deftypefun double nearbyint (double @var{x})
779ae82e
UD
586@deftypefunx float nearbyintf (float @var{x})
587@deftypefunx {long double} nearbyintl (long double @var{x})
b4012b75
UD
588These functions return the same value as the @code{rint} functions but
589even some rounding actually takes place @code{nearbyint} does @emph{not}
590raise the inexact exception.
591@end deftypefun
592
28f540f4 593@comment math.h
f65fd747 594@comment ISO
28f540f4 595@deftypefun double modf (double @var{value}, double *@var{integer-part})
779ae82e
UD
596@deftypefunx float modff (flaot @var{value}, float *@var{integer-part})
597@deftypefunx {long double} modfl (long double @var{value}, long double *@var{integer-part})
b4012b75 598These functions break the argument @var{value} into an integer part and a
28f540f4
RM
599fractional part (between @code{-1} and @code{1}, exclusive). Their sum
600equals @var{value}. Each of the parts has the same sign as @var{value},
601so the rounding of the integer part is towards zero.
602
603@code{modf} stores the integer part in @code{*@var{integer-part}}, and
604returns the fractional part. For example, @code{modf (2.5, &intpart)}
605returns @code{0.5} and stores @code{2.0} into @code{intpart}.
606@end deftypefun
607
608@comment math.h
f65fd747 609@comment ISO
28f540f4 610@deftypefun double fmod (double @var{numerator}, double @var{denominator})
779ae82e
UD
611@deftypefunx float fmodf (float @var{numerator}, float @var{denominator})
612@deftypefunx {long double} fmodl (long double @var{numerator}, long double @var{denominator})
b4012b75 613These functions compute the remainder from the division of
28f540f4
RM
614@var{numerator} by @var{denominator}. Specifically, the return value is
615@code{@var{numerator} - @w{@var{n} * @var{denominator}}}, where @var{n}
616is the quotient of @var{numerator} divided by @var{denominator}, rounded
617towards zero to an integer. Thus, @w{@code{fmod (6.5, 2.3)}} returns
618@code{1.9}, which is @code{6.5} minus @code{4.6}.
619
620The result has the same sign as the @var{numerator} and has magnitude
621less than the magnitude of the @var{denominator}.
622
623If @var{denominator} is zero, @code{fmod} fails and sets @code{errno} to
624@code{EDOM}.
625@end deftypefun
626
627@comment math.h
628@comment BSD
629@deftypefun double drem (double @var{numerator}, double @var{denominator})
779ae82e
UD
630@deftypefunx float dremf (float @var{numerator}, float @var{denominator})
631@deftypefunx {long double} dreml (long double @var{numerator}, long double @var{denominator})
b4012b75 632These functions are like @code{fmod} etc except that it rounds the
28f540f4
RM
633internal quotient @var{n} to the nearest integer instead of towards zero
634to an integer. For example, @code{drem (6.5, 2.3)} returns @code{-0.4},
635which is @code{6.5} minus @code{6.9}.
636
637The absolute value of the result is less than or equal to half the
638absolute value of the @var{denominator}. The difference between
639@code{fmod (@var{numerator}, @var{denominator})} and @code{drem
640(@var{numerator}, @var{denominator})} is always either
641@var{denominator}, minus @var{denominator}, or zero.
642
643If @var{denominator} is zero, @code{drem} fails and sets @code{errno} to
644@code{EDOM}.
645@end deftypefun
646
647
648@node Integer Division
649@section Integer Division
650@cindex integer division functions
651
652This section describes functions for performing integer division. These
653functions are redundant in the GNU C library, since in GNU C the @samp{/}
654operator always rounds towards zero. But in other C implementations,
655@samp{/} may round differently with negative arguments. @code{div} and
656@code{ldiv} are useful because they specify how to round the quotient:
657towards zero. The remainder has the same sign as the numerator.
658
659These functions are specified to return a result @var{r} such that the value
660@code{@var{r}.quot*@var{denominator} + @var{r}.rem} equals
661@var{numerator}.
662
663@pindex stdlib.h
664To use these facilities, you should include the header file
665@file{stdlib.h} in your program.
666
667@comment stdlib.h
f65fd747 668@comment ISO
28f540f4
RM
669@deftp {Data Type} div_t
670This is a structure type used to hold the result returned by the @code{div}
671function. It has the following members:
672
673@table @code
674@item int quot
675The quotient from the division.
676
677@item int rem
678The remainder from the division.
679@end table
680@end deftp
681
682@comment stdlib.h
f65fd747 683@comment ISO
28f540f4
RM
684@deftypefun div_t div (int @var{numerator}, int @var{denominator})
685This function @code{div} computes the quotient and remainder from
686the division of @var{numerator} by @var{denominator}, returning the
687result in a structure of type @code{div_t}.
688
689If the result cannot be represented (as in a division by zero), the
690behavior is undefined.
691
692Here is an example, albeit not a very useful one.
693
694@smallexample
695div_t result;
696result = div (20, -6);
697@end smallexample
698
699@noindent
700Now @code{result.quot} is @code{-3} and @code{result.rem} is @code{2}.
701@end deftypefun
702
703@comment stdlib.h
f65fd747 704@comment ISO
28f540f4
RM
705@deftp {Data Type} ldiv_t
706This is a structure type used to hold the result returned by the @code{ldiv}
707function. It has the following members:
708
709@table @code
710@item long int quot
711The quotient from the division.
712
713@item long int rem
714The remainder from the division.
715@end table
716
717(This is identical to @code{div_t} except that the components are of
718type @code{long int} rather than @code{int}.)
719@end deftp
720
721@comment stdlib.h
f65fd747 722@comment ISO
28f540f4
RM
723@deftypefun ldiv_t ldiv (long int @var{numerator}, long int @var{denominator})
724The @code{ldiv} function is similar to @code{div}, except that the
725arguments are of type @code{long int} and the result is returned as a
fe7bdd63
UD
726structure of type @code{ldiv_t}.
727@end deftypefun
728
729@comment stdlib.h
730@comment GNU
731@deftp {Data Type} lldiv_t
732This is a structure type used to hold the result returned by the @code{lldiv}
733function. It has the following members:
734
735@table @code
736@item long long int quot
737The quotient from the division.
738
739@item long long int rem
740The remainder from the division.
741@end table
742
743(This is identical to @code{div_t} except that the components are of
744type @code{long long int} rather than @code{int}.)
745@end deftp
746
747@comment stdlib.h
748@comment GNU
749@deftypefun lldiv_t lldiv (long long int @var{numerator}, long long int @var{denominator})
750The @code{lldiv} function is like the @code{div} function, but the
751arguments are of type @code{long long int} and the result is returned as
752a structure of type @code{lldiv_t}.
753
754The @code{lldiv} function is a GNU extension but it will eventually be
755part of the next ISO C standard.
28f540f4
RM
756@end deftypefun
757
758
759@node Parsing of Numbers
760@section Parsing of Numbers
761@cindex parsing numbers (in formatted input)
762@cindex converting strings to numbers
763@cindex number syntax, parsing
764@cindex syntax, for reading numbers
765
766This section describes functions for ``reading'' integer and
767floating-point numbers from a string. It may be more convenient in some
768cases to use @code{sscanf} or one of the related functions; see
769@ref{Formatted Input}. But often you can make a program more robust by
770finding the tokens in the string by hand, then converting the numbers
771one by one.
772
773@menu
774* Parsing of Integers:: Functions for conversion of integer values.
775* Parsing of Floats:: Functions for conversion of floating-point
776 values.
777@end menu
778
779@node Parsing of Integers
780@subsection Parsing of Integers
781
782@pindex stdlib.h
783These functions are declared in @file{stdlib.h}.
784
785@comment stdlib.h
f65fd747 786@comment ISO
28f540f4
RM
787@deftypefun {long int} strtol (const char *@var{string}, char **@var{tailptr}, int @var{base})
788The @code{strtol} (``string-to-long'') function converts the initial
789part of @var{string} to a signed integer, which is returned as a value
b8fe19fa 790of type @code{long int}.
28f540f4
RM
791
792This function attempts to decompose @var{string} as follows:
793
794@itemize @bullet
b8fe19fa 795@item
28f540f4
RM
796A (possibly empty) sequence of whitespace characters. Which characters
797are whitespace is determined by the @code{isspace} function
798(@pxref{Classification of Characters}). These are discarded.
799
b8fe19fa 800@item
28f540f4
RM
801An optional plus or minus sign (@samp{+} or @samp{-}).
802
b8fe19fa 803@item
28f540f4
RM
804A nonempty sequence of digits in the radix specified by @var{base}.
805
806If @var{base} is zero, decimal radix is assumed unless the series of
807digits begins with @samp{0} (specifying octal radix), or @samp{0x} or
808@samp{0X} (specifying hexadecimal radix); in other words, the same
809syntax used for integer constants in C.
810
811Otherwise @var{base} must have a value between @code{2} and @code{35}.
812If @var{base} is @code{16}, the digits may optionally be preceded by
2c6fe0bd
UD
813@samp{0x} or @samp{0X}. If base has no legal value the value returned
814is @code{0l} and the global variable @code{errno} is set to @code{EINVAL}.
28f540f4 815
b8fe19fa 816@item
28f540f4
RM
817Any remaining characters in the string. If @var{tailptr} is not a null
818pointer, @code{strtol} stores a pointer to this tail in
819@code{*@var{tailptr}}.
820@end itemize
821
822If the string is empty, contains only whitespace, or does not contain an
823initial substring that has the expected syntax for an integer in the
824specified @var{base}, no conversion is performed. In this case,
825@code{strtol} returns a value of zero and the value stored in
826@code{*@var{tailptr}} is the value of @var{string}.
827
828In a locale other than the standard @code{"C"} locale, this function
829may recognize additional implementation-dependent syntax.
830
831If the string has valid syntax for an integer but the value is not
832representable because of overflow, @code{strtol} returns either
833@code{LONG_MAX} or @code{LONG_MIN} (@pxref{Range of Type}), as
834appropriate for the sign of the value. It also sets @code{errno}
835to @code{ERANGE} to indicate there was overflow.
836
2c6fe0bd
UD
837Because the value @code{0l} is a correct result for @code{strtol} the
838user who is interested in handling errors should set the global variable
6d52618b
UD
839@code{errno} to @code{0} before calling this function, so that the program
840can later test whether an error occurred.
2c6fe0bd 841
28f540f4
RM
842There is an example at the end of this section.
843@end deftypefun
844
845@comment stdlib.h
f65fd747 846@comment ISO
28f540f4
RM
847@deftypefun {unsigned long int} strtoul (const char *@var{string}, char **@var{tailptr}, int @var{base})
848The @code{strtoul} (``string-to-unsigned-long'') function is like
b8fe19fa
RM
849@code{strtol} except it deals with unsigned numbers, and returns its
850value with type @code{unsigned long int}. No @samp{+} or @samp{-} sign
851may appear before the number, but the syntax is otherwise the same as
852described above for @code{strtol}. The value returned in case of
853overflow is @code{ULONG_MAX} (@pxref{Range of Type}).
2c6fe0bd
UD
854
855Like @code{strtol} this function sets @code{errno} and returns the value
856@code{0ul} in case the value for @var{base} is not in the legal range.
857For @code{strtoul} this can happen in another situation. In case the
858number to be converted is negative @code{strtoul} also sets @code{errno}
859to @code{EINVAL} and returns @code{0ul}.
860@end deftypefun
861
862@comment stdlib.h
fe7bdd63
UD
863@comment GNU
864@deftypefun {long long int} strtoll (const char *@var{string}, char **@var{tailptr}, int @var{base})
865The @code{strtoll} function is like @code{strtol} except that is deals
866with extra long numbers and it returns its value with type @code{long
867long int}.
2c6fe0bd
UD
868
869If the string has valid syntax for an integer but the value is not
fe7bdd63 870representable because of overflow, @code{strtoll} returns either
2c6fe0bd
UD
871@code{LONG_LONG_MAX} or @code{LONG_LONG_MIN} (@pxref{Range of Type}), as
872appropriate for the sign of the value. It also sets @code{errno} to
873@code{ERANGE} to indicate there was overflow.
2c6fe0bd 874
fe7bdd63
UD
875The @code{strtoll} function is a GNU extension but it will eventually be
876part of the next ISO C standard.
2c6fe0bd
UD
877@end deftypefun
878
879@comment stdlib.h
880@comment BSD
fe7bdd63
UD
881@deftypefun {long long int} strtoq (const char *@var{string}, char **@var{tailptr}, int @var{base})
882@code{strtoq} (``string-to-quad-word'') is only an commonly used other
883name for the @code{strtoll} function. Everything said for
884@code{strtoll} applies to @code{strtoq} as well.
2c6fe0bd
UD
885@end deftypefun
886
887@comment stdlib.h
888@comment GNU
889@deftypefun {unsigned long long int} strtoull (const char *@var{string}, char **@var{tailptr}, int @var{base})
fe7bdd63
UD
890The @code{strtoull} function is like @code{strtoul} except that is deals
891with extra long numbers and it returns its value with type
892@code{unsigned long long int}. The value returned in case of overflow
893is @code{ULONG_LONG_MAX} (@pxref{Range of Type}).
894
895The @code{strtoull} function is a GNU extension but it will eventually be
896part of the next ISO C standard.
897@end deftypefun
898
899@comment stdlib.h
900@comment BSD
901@deftypefun {unsigned long long int} strtouq (const char *@var{string}, char **@var{tailptr}, int @var{base})
902@code{strtouq} (``string-to-unsigned-quad-word'') is only an commonly
903used other name for the @code{strtoull} function. Everything said for
904@code{strtoull} applies to @code{strtouq} as well.
28f540f4
RM
905@end deftypefun
906
907@comment stdlib.h
f65fd747 908@comment ISO
28f540f4
RM
909@deftypefun {long int} atol (const char *@var{string})
910This function is similar to the @code{strtol} function with a @var{base}
911argument of @code{10}, except that it need not detect overflow errors.
912The @code{atol} function is provided mostly for compatibility with
913existing code; using @code{strtol} is more robust.
914@end deftypefun
915
916@comment stdlib.h
f65fd747 917@comment ISO
28f540f4
RM
918@deftypefun int atoi (const char *@var{string})
919This function is like @code{atol}, except that it returns an @code{int}
920value rather than @code{long int}. The @code{atoi} function is also
921considered obsolete; use @code{strtol} instead.
922@end deftypefun
923
fe7bdd63
UD
924@comment stdlib.h
925@comment GNU
926@deftypefun {long long int} atoll (const char *@var{string})
927This function is similar to @code{atol}, except it returns a @code{long
928long int} value rather than @code{long int}.
929
930The @code{atoll} function is a GNU extension but it will eventually be
931part of the next ISO C standard.
932@end deftypefun
933
2c6fe0bd
UD
934The POSIX locales contain some information about how to format numbers
935(@pxref{General Numeric}). This mainly deals with representing numbers
936for better readability for humans. The functions present so far in this
937section cannot handle numbers in this form.
938
939If this functionality is needed in a program one can use the functions
940from the @code{scanf} family which know about the flag @samp{'} for
941parsing numeric input (@pxref{Numeric Input Conversions}). Sometimes it
942is more desirable to have finer control.
943
944In these situation one could use the function
945@code{__strto@var{XXX}_internal}. @var{XXX} here stands for any of the
946above forms. All numeric conversion functions (including the functions
947to process floating-point numbers) have such a counterpart. The
26761c28 948difference to the normal form is the extra argument at the end of the
2c6fe0bd 949parameter list. If this value has an non-zero value the handling of
26761c28 950number grouping is enabled. The advantage of using these functions is
2c6fe0bd
UD
951that the @var{tailptr} parameters allow to determine which part of the
952input is processed. The @code{scanf} functions don't provide this
953information. The drawback of using these functions is that they are not
954portable. They only exist in the GNU C library.
955
956
28f540f4
RM
957Here is a function which parses a string as a sequence of integers and
958returns the sum of them:
959
960@smallexample
961int
962sum_ints_from_string (char *string)
963@{
964 int sum = 0;
965
966 while (1) @{
967 char *tail;
968 int next;
969
970 /* @r{Skip whitespace by hand, to detect the end.} */
971 while (isspace (*string)) string++;
972 if (*string == 0)
973 break;
974
975 /* @r{There is more nonwhitespace,} */
976 /* @r{so it ought to be another number.} */
977 errno = 0;
978 /* @r{Parse it.} */
979 next = strtol (string, &tail, 0);
980 /* @r{Add it in, if not overflow.} */
981 if (errno)
982 printf ("Overflow\n");
983 else
984 sum += next;
985 /* @r{Advance past it.} */
986 string = tail;
987 @}
988
989 return sum;
990@}
991@end smallexample
992
993@node Parsing of Floats
994@subsection Parsing of Floats
995
996@pindex stdlib.h
997These functions are declared in @file{stdlib.h}.
998
999@comment stdlib.h
f65fd747 1000@comment ISO
28f540f4
RM
1001@deftypefun double strtod (const char *@var{string}, char **@var{tailptr})
1002The @code{strtod} (``string-to-double'') function converts the initial
1003part of @var{string} to a floating-point number, which is returned as a
b8fe19fa 1004value of type @code{double}.
28f540f4
RM
1005
1006This function attempts to decompose @var{string} as follows:
1007
1008@itemize @bullet
b8fe19fa 1009@item
28f540f4
RM
1010A (possibly empty) sequence of whitespace characters. Which characters
1011are whitespace is determined by the @code{isspace} function
1012(@pxref{Classification of Characters}). These are discarded.
1013
1014@item
1015An optional plus or minus sign (@samp{+} or @samp{-}).
1016
1017@item
1018A nonempty sequence of digits optionally containing a decimal-point
1019character---normally @samp{.}, but it depends on the locale
1020(@pxref{Numeric Formatting}).
1021
1022@item
1023An optional exponent part, consisting of a character @samp{e} or
1024@samp{E}, an optional sign, and a sequence of digits.
1025
1026@item
1027Any remaining characters in the string. If @var{tailptr} is not a null
1028pointer, a pointer to this tail of the string is stored in
1029@code{*@var{tailptr}}.
1030@end itemize
1031
1032If the string is empty, contains only whitespace, or does not contain an
1033initial substring that has the expected syntax for a floating-point
1034number, no conversion is performed. In this case, @code{strtod} returns
1035a value of zero and the value returned in @code{*@var{tailptr}} is the
1036value of @var{string}.
1037
26761c28 1038In a locale other than the standard @code{"C"} or @code{"POSIX"} locales,
2c6fe0bd 1039this function may recognize additional locale-dependent syntax.
28f540f4
RM
1040
1041If the string has valid syntax for a floating-point number but the value
1042is not representable because of overflow, @code{strtod} returns either
1043positive or negative @code{HUGE_VAL} (@pxref{Mathematics}), depending on
1044the sign of the value. Similarly, if the value is not representable
1045because of underflow, @code{strtod} returns zero. It also sets @code{errno}
1046to @code{ERANGE} if there was overflow or underflow.
2c6fe0bd 1047
fe7bdd63
UD
1048There are two more special inputs which are recognized by @code{strtod}.
1049The string @code{"inf"} or @code{"infinity"} (without consideration of
1050case and optionally preceded by a @code{"+"} or @code{"-"} sign) is
1051changed to the floating-point value for infinity if the floating-point
1052format supports this; and to the largest representable value otherwise.
1053
1054If the input string is @code{"nan"} or
1055@code{"nan(@var{n-char-sequence})"} the return value of @code{strtod} is
1056the representation of the NaN (not a number) value (if the
1057flaoting-point formats supports this. The form with the
1058@var{n-char-sequence} enables in an implementation specific way to
1059specify the form of the NaN value. When using the @w{IEEE 754}
1060floating-point format, the NaN value can have a lot of forms since only
1061at least one bit in the mantissa must be set. In the GNU C library
1062implementation of @code{strtod} the @var{n-char-sequence} is interpreted
1063as a number (as recognized by @code{strtol}, @pxref{Parsing of Integers})
1064The mantissa of the return value corresponds to this given number.
1065
2c6fe0bd 1066Since the value zero which is returned in the error case is also a valid
26761c28 1067result the user should set the global variable @code{errno} to zero
2c6fe0bd
UD
1068before calling this function. So one can test for failures after the
1069call since all failures set @code{errno} to a non-zero value.
28f540f4
RM
1070@end deftypefun
1071
2c6fe0bd
UD
1072@comment stdlib.h
1073@comment GNU
1074@deftypefun float strtof (const char *@var{string}, char **@var{tailptr})
1075This function is similar to the @code{strtod} function but it returns a
1076@code{float} value instead of a @code{double} value. If the precision
6d52618b 1077of a @code{float} value is sufficient this function should be used since
2c6fe0bd
UD
1078it is much faster than @code{strtod} on some architectures. The reasons
1079are obvious: @w{IEEE 754} defines @code{float} to have a mantissa of 23
1080bits while @code{double} has 53 bits and every additional bit of
1081precision can require additional computation.
1082
1083If the string has valid syntax for a floating-point number but the value
1084is not representable because of overflow, @code{strtof} returns either
fe7bdd63 1085positive or negative @code{HUGE_VALF} (@pxref{Mathematics}), depending on
2c6fe0bd
UD
1086the sign of the value.
1087
1088This function is a GNU extension.
1089@end deftypefun
1090
1091@comment stdlib.h
1092@comment GNU
1093@deftypefun {long double} strtold (const char *@var{string}, char **@var{tailptr})
1094This function is similar to the @code{strtod} function but it returns a
1095@code{long double} value instead of a @code{double} value. It should be
6d52618b 1096used when high precision is needed. On systems which define a @code{long
2c6fe0bd 1097double} type (i.e., on which it is not the same as @code{double})
6d52618b 1098running this function might take significantly more time since more bits
2c6fe0bd
UD
1099of precision are required.
1100
1101If the string has valid syntax for a floating-point number but the value
1102is not representable because of overflow, @code{strtold} returns either
fe7bdd63 1103positive or negative @code{HUGE_VALL} (@pxref{Mathematics}), depending on
2c6fe0bd
UD
1104the sign of the value.
1105
1106This function is a GNU extension.
1107@end deftypefun
1108
1109As for the integer parsing functions there are additional functions
1110which will handle numbers represented using the grouping scheme of the
1111current locale (@pxref{Parsing of Integers}).
1112
28f540f4 1113@comment stdlib.h
f65fd747 1114@comment ISO
28f540f4
RM
1115@deftypefun double atof (const char *@var{string})
1116This function is similar to the @code{strtod} function, except that it
1117need not detect overflow and underflow errors. The @code{atof} function
1118is provided mostly for compatibility with existing code; using
1119@code{strtod} is more robust.
1120@end deftypefun