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