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