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.  These functions are declared in the header file
7 @file{math.h}.
8
9 @menu
10 * Not a Number::                Making NaNs and testing for NaNs.
11 * Predicates on Floats::        Testing for infinity and for NaNs.
12 * Absolute Value::              Absolute value functions.
13 * Normalization Functions::     Hacks for radix-2 representations.
14 * Rounding and Remainders::     Determining the integer and
15                                  fractional parts of a float.
16 * Integer Division::            Functions for performing integer
17                                  division.
18 * Parsing of Numbers::          Functions for ``reading'' numbers
19                                  from strings.
20 @end menu
21
22 @node Not a Number
23 @section ``Not a Number'' Values
24 @cindex NaN
25 @cindex not a number
26 @cindex IEEE floating point
27
28 The IEEE floating point format used by most modern computers supports
29 values that are ``not a number''.  These values are called @dfn{NaNs}.
30 ``Not a number'' values result from certain operations which have no
31 meaningful numeric result, such as zero divided by zero or infinity
32 divided by infinity.
33
34 One noteworthy property of NaNs is that they are not equal to
35 themselves.  Thus, @code{x == x} can be 0 if the value of @code{x} is a
36 NaN.  You can use this to test whether a value is a NaN or not: if it is
37 not equal to itself, then it is a NaN.  But the recommended way to test
38 for a NaN is with the @code{isnan} function (@pxref{Predicates on Floats}).
39
40 Almost any arithmetic operation in which one argument is a NaN returns
41 a NaN.
42
43 @comment math.h
44 @comment GNU
45 @deftypevr Macro double NAN
46 An expression representing a value which is ``not a number''.  This
47 macro is a GNU extension, available only on machines that support ``not
48 a number'' values---that is to say, on all machines that support IEEE
49 floating point.
50
51 You can use @samp{#ifdef NAN} to test whether the machine supports
52 NaNs.  (Of course, you must arrange for GNU extensions to be visible,
53 such as by defining @code{_GNU_SOURCE}, and then you must include
54 @file{math.h}.)
55 @end deftypevr
56
57 @node Predicates on Floats
58 @section Predicates on Floats
59
60 @pindex math.h
61 This section describes some miscellaneous test functions on doubles.
62 Prototypes for these functions appear in @file{math.h}.  These are BSD
63 functions, and thus are available if you define @code{_BSD_SOURCE} or
64 @code{_GNU_SOURCE}.
65
66 @comment math.h
67 @comment BSD
68 @deftypefun int isinf (double @var{x})
69 This function returns @code{-1} if @var{x} represents negative infinity,
70 @code{1} if @var{x} represents positive infinity, and @code{0} otherwise.
71 @end deftypefun
72
73 @comment math.h
74 @comment BSD
75 @deftypefun int isnan (double @var{x})
76 This function returns a nonzero value if @var{x} is a ``not a number''
77 value, and zero otherwise.  (You can just as well use @code{@var{x} !=
78 @var{x}} to get the same result).
79 @end deftypefun
80
81 @comment math.h
82 @comment BSD
83 @deftypefun int finite (double @var{x})
84 This function returns a nonzero value if @var{x} is finite or a ``not a
85 number'' value, and zero otherwise.
86 @end deftypefun
87
88 @comment math.h
89 @comment BSD
90 @deftypefun double infnan (int @var{error})
91 This function is provided for compatibility with BSD.  The other
92 mathematical functions use @code{infnan} to decide what to return on
93 occasion of an error.  Its argument is an error code, @code{EDOM} or
94 @code{ERANGE}; @code{infnan} returns a suitable value to indicate this
95 with.  @code{-ERANGE} is also acceptable as an argument, and corresponds
96 to @code{-HUGE_VAL} as a value.
97
98 In the BSD library, on certain machines, @code{infnan} raises a fatal
99 signal in all cases.  The GNU library does not do likewise, because that
100 does not fit the @w{ISO C} specification.
101 @end deftypefun
102
103 @strong{Portability Note:} The functions listed in this section are BSD
104 extensions.
105
106 @node Absolute Value
107 @section Absolute Value
108 @cindex absolute value functions
109
110 These functions are provided for obtaining the @dfn{absolute value} (or
111 @dfn{magnitude}) of a number.  The absolute value of a real number
112 @var{x} is @var{x} is @var{x} is positive, @minus{}@var{x} if @var{x} is
113 negative.  For a complex number @var{z}, whose real part is @var{x} and
114 whose imaginary part is @var{y}, the absolute value is @w{@code{sqrt
115 (@var{x}*@var{x} + @var{y}*@var{y})}}.
116
117 @pindex math.h
118 @pindex stdlib.h
119 Prototypes for @code{abs} and @code{labs} are in @file{stdlib.h};
120 @code{fabs} and @code{cabs} are declared in @file{math.h}.
121
122 @comment stdlib.h
123 @comment ISO
124 @deftypefun int abs (int @var{number})
125 This function returns the absolute value of @var{number}.
126
127 Most computers use a two's complement integer representation, in which
128 the absolute value of @code{INT_MIN} (the smallest possible @code{int})
129 cannot be represented; thus, @w{@code{abs (INT_MIN)}} is not defined.
130 @end deftypefun
131
132 @comment stdlib.h
133 @comment ISO
134 @deftypefun {long int} labs (long int @var{number})
135 This is similar to @code{abs}, except that both the argument and result
136 are of type @code{long int} rather than @code{int}.
137 @end deftypefun
138
139 @comment math.h
140 @comment ISO
141 @deftypefun double fabs (double @var{number})
142 This function returns the absolute value of the floating-point number
143 @var{number}.
144 @end deftypefun
145
146 @comment math.h
147 @comment BSD
148 @deftypefun double cabs (struct @{ double real, imag; @} @var{z})
149 The @code{cabs} function returns the absolute value of the complex
150 number @var{z}, whose real part is @code{@var{z}.real} and whose
151 imaginary part is @code{@var{z}.imag}.  (See also the function
152 @code{hypot} in @ref{Exponents and Logarithms}.)  The value is:
153
154 @smallexample
155 sqrt (@var{z}.real*@var{z}.real + @var{z}.imag*@var{z}.imag)
156 @end smallexample
157 @end deftypefun
158
159 @node Normalization Functions
160 @section Normalization Functions
161 @cindex normalization functions (floating-point)
162
163 The functions described in this section are primarily provided as a way
164 to efficiently perform certain low-level manipulations on floating point
165 numbers that are represented internally using a binary radix;
166 see @ref{Floating Point Concepts}.  These functions are required to
167 have equivalent behavior even if the representation does not use a radix
168 of 2, but of course they are unlikely to be particularly efficient in
169 those cases.
170
171 @pindex math.h
172 All these functions are declared in @file{math.h}.
173
174 @comment math.h
175 @comment ISO
176 @deftypefun double frexp (double @var{value}, int *@var{exponent})
177 The @code{frexp} function is used to split the number @var{value}
178 into a normalized fraction and an exponent.
179
180 If the argument @var{value} is not zero, the return value is @var{value}
181 times a power of two, and is always in the range 1/2 (inclusive) to 1
182 (exclusive).  The corresponding exponent is stored in
183 @code{*@var{exponent}}; the return value multiplied by 2 raised to this
184 exponent equals the original number @var{value}.
185
186 For example, @code{frexp (12.8, &exponent)} returns @code{0.8} and
187 stores @code{4} in @code{exponent}.
188
189 If @var{value} is zero, then the return value is zero and
190 zero is stored in @code{*@var{exponent}}.
191 @end deftypefun
192
193 @comment math.h
194 @comment ISO
195 @deftypefun double ldexp (double @var{value}, int @var{exponent})
196 This function returns the result of multiplying the floating-point
197 number @var{value} by 2 raised to the power @var{exponent}.  (It can
198 be used to reassemble floating-point numbers that were taken apart
199 by @code{frexp}.)
200
201 For example, @code{ldexp (0.8, 4)} returns @code{12.8}.
202 @end deftypefun
203
204 The following functions which come from BSD provide facilities
205 equivalent to those of @code{ldexp} and @code{frexp}:
206
207 @comment math.h
208 @comment BSD
209 @deftypefun double scalb (double @var{value}, int @var{exponent})
210 The @code{scalb} function is the BSD name for @code{ldexp}.
211 @end deftypefun
212
213 @comment math.h
214 @comment BSD
215 @deftypefun double logb (double @var{x})
216 This BSD function returns the integer part of the base-2 logarithm of
217 @var{x}, an integer value represented in type @code{double}.  This is
218 the highest integer power of @code{2} contained in @var{x}.  The sign of
219 @var{x} is ignored.  For example, @code{logb (3.5)} is @code{1.0} and
220 @code{logb (4.0)} is @code{2.0}.
221
222 When @code{2} raised to this power is divided into @var{x}, it gives a
223 quotient between @code{1} (inclusive) and @code{2} (exclusive).
224
225 If @var{x} is zero, the value is minus infinity (if the machine supports
226 such a value), or else a very small number.  If @var{x} is infinity, the
227 value is infinity.
228
229 The value returned by @code{logb} is one less than the value that
230 @code{frexp} would store into @code{*@var{exponent}}.
231 @end deftypefun
232
233 @comment math.h
234 @comment BSD
235 @deftypefun double copysign (double @var{value}, double @var{sign})
236 The @code{copysign} function returns a value whose absolute value is the
237 same as that of @var{value}, and whose sign matches that of @var{sign}.
238 This is a BSD function.
239 @end deftypefun
240
241 @node Rounding and Remainders
242 @section Rounding and Remainder Functions
243 @cindex rounding functions
244 @cindex remainder functions
245 @cindex converting floats to integers
246
247 @pindex math.h
248 The functions listed here perform operations such as rounding,
249 truncation, and remainder in division of floating point numbers.  Some
250 of these functions convert floating point numbers to integer values.
251 They are all declared in @file{math.h}.
252
253 You can also convert floating-point numbers to integers simply by
254 casting them to @code{int}.  This discards the fractional part,
255 effectively rounding towards zero.  However, this only works if the
256 result can actually be represented as an @code{int}---for very large
257 numbers, this is impossible.  The functions listed here return the
258 result as a @code{double} instead to get around this problem.
259
260 @comment math.h
261 @comment ISO
262 @deftypefun double ceil (double @var{x})
263 The @code{ceil} function rounds @var{x} upwards to the nearest integer,
264 returning that value as a @code{double}.  Thus, @code{ceil (1.5)}
265 is @code{2.0}.
266 @end deftypefun
267
268 @comment math.h
269 @comment ISO
270 @deftypefun double floor (double @var{x})
271 The @code{ceil} function rounds @var{x} downwards to the nearest
272 integer, returning that value as a @code{double}.  Thus, @code{floor
273 (1.5)} is @code{1.0} and @code{floor (-1.5)} is @code{-2.0}.
274 @end deftypefun
275
276 @comment math.h
277 @comment BSD
278 @deftypefun double rint (double @var{x})
279 This function rounds @var{x} to an integer value according to the
280 current rounding mode.  @xref{Floating Point Parameters}, for
281 information about the various rounding modes.  The default
282 rounding mode is to round to the nearest integer; some machines
283 support other modes, but round-to-nearest is always used unless
284 you explicit select another.
285 @end deftypefun
286
287 @comment math.h
288 @comment ISO
289 @deftypefun double modf (double @var{value}, double *@var{integer-part})
290 This function breaks the argument @var{value} into an integer part and a
291 fractional part (between @code{-1} and @code{1}, exclusive).  Their sum
292 equals @var{value}.  Each of the parts has the same sign as @var{value},
293 so the rounding of the integer part is towards zero.
294
295 @code{modf} stores the integer part in @code{*@var{integer-part}}, and
296 returns the fractional part.  For example, @code{modf (2.5, &intpart)}
297 returns @code{0.5} and stores @code{2.0} into @code{intpart}.
298 @end deftypefun
299
300 @comment math.h
301 @comment ISO
302 @deftypefun double fmod (double @var{numerator}, double @var{denominator})
303 This function computes the remainder from the division of
304 @var{numerator} by @var{denominator}.  Specifically, the return value is
305 @code{@var{numerator} - @w{@var{n} * @var{denominator}}}, where @var{n}
306 is the quotient of @var{numerator} divided by @var{denominator}, rounded
307 towards zero to an integer.  Thus, @w{@code{fmod (6.5, 2.3)}} returns
308 @code{1.9}, which is @code{6.5} minus @code{4.6}.
309
310 The result has the same sign as the @var{numerator} and has magnitude
311 less than the magnitude of the @var{denominator}.
312
313 If @var{denominator} is zero, @code{fmod} fails and sets @code{errno} to
314 @code{EDOM}.
315 @end deftypefun
316
317 @comment math.h
318 @comment BSD
319 @deftypefun double drem (double @var{numerator}, double @var{denominator})
320 The function @code{drem} is like @code{fmod} except that it rounds the
321 internal quotient @var{n} to the nearest integer instead of towards zero
322 to an integer.  For example, @code{drem (6.5, 2.3)} returns @code{-0.4},
323 which is @code{6.5} minus @code{6.9}.
324
325 The absolute value of the result is less than or equal to half the
326 absolute value of the @var{denominator}.  The difference between
327 @code{fmod (@var{numerator}, @var{denominator})} and @code{drem
328 (@var{numerator}, @var{denominator})} is always either
329 @var{denominator}, minus @var{denominator}, or zero.
330
331 If @var{denominator} is zero, @code{drem} fails and sets @code{errno} to
332 @code{EDOM}.
333 @end deftypefun
334
335
336 @node Integer Division
337 @section Integer Division
338 @cindex integer division functions
339
340 This section describes functions for performing integer division.  These
341 functions are redundant in the GNU C library, since in GNU C the @samp{/}
342 operator always rounds towards zero.  But in other C implementations,
343 @samp{/} may round differently with negative arguments.  @code{div} and
344 @code{ldiv} are useful because they specify how to round the quotient:
345 towards zero.  The remainder has the same sign as the numerator.
346
347 These functions are specified to return a result @var{r} such that the value
348 @code{@var{r}.quot*@var{denominator} + @var{r}.rem} equals
349 @var{numerator}.
350
351 @pindex stdlib.h
352 To use these facilities, you should include the header file
353 @file{stdlib.h} in your program.
354
355 @comment stdlib.h
356 @comment ISO
357 @deftp {Data Type} div_t
358 This is a structure type used to hold the result returned by the @code{div}
359 function.  It has the following members:
360
361 @table @code
362 @item int quot
363 The quotient from the division.
364
365 @item int rem
366 The remainder from the division.
367 @end table
368 @end deftp
369
370 @comment stdlib.h
371 @comment ISO
372 @deftypefun div_t div (int @var{numerator}, int @var{denominator})
373 This function @code{div} computes the quotient and remainder from
374 the division of @var{numerator} by @var{denominator}, returning the
375 result in a structure of type @code{div_t}.
376
377 If the result cannot be represented (as in a division by zero), the
378 behavior is undefined.
379
380 Here is an example, albeit not a very useful one.
381
382 @smallexample
383 div_t result;
384 result = div (20, -6);
385 @end smallexample
386
387 @noindent
388 Now @code{result.quot} is @code{-3} and @code{result.rem} is @code{2}.
389 @end deftypefun
390
391 @comment stdlib.h
392 @comment ISO
393 @deftp {Data Type} ldiv_t
394 This is a structure type used to hold the result returned by the @code{ldiv}
395 function.  It has the following members:
396
397 @table @code
398 @item long int quot
399 The quotient from the division.
400
401 @item long int rem
402 The remainder from the division.
403 @end table
404
405 (This is identical to @code{div_t} except that the components are of
406 type @code{long int} rather than @code{int}.)
407 @end deftp
408
409 @comment stdlib.h
410 @comment ISO
411 @deftypefun ldiv_t ldiv (long int @var{numerator}, long int @var{denominator})
412 The @code{ldiv} function is similar to @code{div}, except that the
413 arguments are of type @code{long int} and the result is returned as a
414 structure of type @code{ldiv_t}.
415 @end deftypefun
416
417 @comment stdlib.h
418 @comment GNU
419 @deftp {Data Type} lldiv_t
420 This is a structure type used to hold the result returned by the @code{lldiv}
421 function.  It has the following members:
422
423 @table @code
424 @item long long int quot
425 The quotient from the division.
426
427 @item long long int rem
428 The remainder from the division.
429 @end table
430
431 (This is identical to @code{div_t} except that the components are of
432 type @code{long long int} rather than @code{int}.)
433 @end deftp
434
435 @comment stdlib.h
436 @comment GNU
437 @deftypefun lldiv_t lldiv (long long int @var{numerator}, long long int @var{denominator})
438 The @code{lldiv} function is like the @code{div} function, but the
439 arguments are of type @code{long long int} and the result is returned as
440 a structure of type @code{lldiv_t}.
441
442 The @code{lldiv} function is a GNU extension but it will eventually be
443 part of the next ISO C standard.
444 @end deftypefun
445
446
447 @node Parsing of Numbers
448 @section Parsing of Numbers
449 @cindex parsing numbers (in formatted input)
450 @cindex converting strings to numbers
451 @cindex number syntax, parsing
452 @cindex syntax, for reading numbers
453
454 This section describes functions for ``reading'' integer and
455 floating-point numbers from a string.  It may be more convenient in some
456 cases to use @code{sscanf} or one of the related functions; see
457 @ref{Formatted Input}.  But often you can make a program more robust by
458 finding the tokens in the string by hand, then converting the numbers
459 one by one.
460
461 @menu
462 * Parsing of Integers::         Functions for conversion of integer values.
463 * Parsing of Floats::           Functions for conversion of floating-point
464                                  values.
465 @end menu
466
467 @node Parsing of Integers
468 @subsection Parsing of Integers
469
470 @pindex stdlib.h
471 These functions are declared in @file{stdlib.h}.
472
473 @comment stdlib.h
474 @comment ISO
475 @deftypefun {long int} strtol (const char *@var{string}, char **@var{tailptr}, int @var{base})
476 The @code{strtol} (``string-to-long'') function converts the initial
477 part of @var{string} to a signed integer, which is returned as a value
478 of type @code{long int}.
479
480 This function attempts to decompose @var{string} as follows:
481
482 @itemize @bullet
483 @item
484 A (possibly empty) sequence of whitespace characters.  Which characters
485 are whitespace is determined by the @code{isspace} function
486 (@pxref{Classification of Characters}).  These are discarded.
487
488 @item
489 An optional plus or minus sign (@samp{+} or @samp{-}).
490
491 @item
492 A nonempty sequence of digits in the radix specified by @var{base}.
493
494 If @var{base} is zero, decimal radix is assumed unless the series of
495 digits begins with @samp{0} (specifying octal radix), or @samp{0x} or
496 @samp{0X} (specifying hexadecimal radix); in other words, the same
497 syntax used for integer constants in C.
498
499 Otherwise @var{base} must have a value between @code{2} and @code{35}.
500 If @var{base} is @code{16}, the digits may optionally be preceded by
501 @samp{0x} or @samp{0X}.  If base has no legal value the value returned
502 is @code{0l} and the global variable @code{errno} is set to @code{EINVAL}.
503
504 @item
505 Any remaining characters in the string.  If @var{tailptr} is not a null
506 pointer, @code{strtol} stores a pointer to this tail in
507 @code{*@var{tailptr}}.
508 @end itemize
509
510 If the string is empty, contains only whitespace, or does not contain an
511 initial substring that has the expected syntax for an integer in the
512 specified @var{base}, no conversion is performed.  In this case,
513 @code{strtol} returns a value of zero and the value stored in
514 @code{*@var{tailptr}} is the value of @var{string}.
515
516 In a locale other than the standard @code{"C"} locale, this function
517 may recognize additional implementation-dependent syntax.
518
519 If the string has valid syntax for an integer but the value is not
520 representable because of overflow, @code{strtol} returns either
521 @code{LONG_MAX} or @code{LONG_MIN} (@pxref{Range of Type}), as
522 appropriate for the sign of the value.  It also sets @code{errno}
523 to @code{ERANGE} to indicate there was overflow.
524
525 Because the value @code{0l} is a correct result for @code{strtol} the
526 user who is interested in handling errors should set the global variable
527 @code{errno} to @code{0} before calling this function, so that the program
528 can later test whether an error occurred.
529
530 There is an example at the end of this section.
531 @end deftypefun
532
533 @comment stdlib.h
534 @comment ISO
535 @deftypefun {unsigned long int} strtoul (const char *@var{string}, char **@var{tailptr}, int @var{base})
536 The @code{strtoul} (``string-to-unsigned-long'') function is like
537 @code{strtol} except it deals with unsigned numbers, and returns its
538 value with type @code{unsigned long int}.  No @samp{+} or @samp{-} sign
539 may appear before the number, but the syntax is otherwise the same as
540 described above for @code{strtol}.  The value returned in case of
541 overflow is @code{ULONG_MAX} (@pxref{Range of Type}).
542
543 Like @code{strtol} this function sets @code{errno} and returns the value
544 @code{0ul} in case the value for @var{base} is not in the legal range.
545 For @code{strtoul} this can happen in another situation.  In case the
546 number to be converted is negative @code{strtoul} also sets @code{errno}
547 to @code{EINVAL} and returns @code{0ul}.
548 @end deftypefun
549
550 @comment stdlib.h
551 @comment GNU
552 @deftypefun {long long int} strtoll (const char *@var{string}, char **@var{tailptr}, int @var{base})
553 The @code{strtoll} function is like @code{strtol} except that is deals
554 with extra long numbers and it returns its value with type @code{long
555 long int}.
556
557 If the string has valid syntax for an integer but the value is not
558 representable because of overflow, @code{strtoll} returns either
559 @code{LONG_LONG_MAX} or @code{LONG_LONG_MIN} (@pxref{Range of Type}), as
560 appropriate for the sign of the value.  It also sets @code{errno} to
561 @code{ERANGE} to indicate there was overflow.
562
563 The @code{strtoll} function is a GNU extension but it will eventually be
564 part of the next ISO C standard.
565 @end deftypefun
566
567 @comment stdlib.h
568 @comment BSD
569 @deftypefun {long long int} strtoq (const char *@var{string}, char **@var{tailptr}, int @var{base})
570 @code{strtoq} (``string-to-quad-word'') is only an commonly used other
571 name for the @code{strtoll} function.  Everything said for
572 @code{strtoll} applies to @code{strtoq} as well.
573 @end deftypefun
574
575 @comment stdlib.h
576 @comment GNU
577 @deftypefun {unsigned long long int} strtoull (const char *@var{string}, char **@var{tailptr}, int @var{base})
578 The @code{strtoull} function is like @code{strtoul} except that is deals
579 with extra long numbers and it returns its value with type
580 @code{unsigned long long int}.  The value returned in case of overflow
581 is @code{ULONG_LONG_MAX} (@pxref{Range of Type}).
582
583 The @code{strtoull} function is a GNU extension but it will eventually be
584 part of the next ISO C standard.
585 @end deftypefun
586
587 @comment stdlib.h
588 @comment BSD
589 @deftypefun {unsigned long long int} strtouq (const char *@var{string}, char **@var{tailptr}, int @var{base})
590 @code{strtouq} (``string-to-unsigned-quad-word'') is only an commonly
591 used other name for the @code{strtoull} function.  Everything said for
592 @code{strtoull} applies to @code{strtouq} as well.
593 @end deftypefun
594
595 @comment stdlib.h
596 @comment ISO
597 @deftypefun {long int} atol (const char *@var{string})
598 This function is similar to the @code{strtol} function with a @var{base}
599 argument of @code{10}, except that it need not detect overflow errors.
600 The @code{atol} function is provided mostly for compatibility with
601 existing code; using @code{strtol} is more robust.
602 @end deftypefun
603
604 @comment stdlib.h
605 @comment ISO
606 @deftypefun int atoi (const char *@var{string})
607 This function is like @code{atol}, except that it returns an @code{int}
608 value rather than @code{long int}.  The @code{atoi} function is also
609 considered obsolete; use @code{strtol} instead.
610 @end deftypefun
611
612 @comment stdlib.h
613 @comment GNU
614 @deftypefun {long long int} atoll (const char *@var{string})
615 This function is similar to @code{atol}, except it returns a @code{long
616 long int} value rather than @code{long int}.
617
618 The @code{atoll} function is a GNU extension but it will eventually be
619 part of the next ISO C standard.
620 @end deftypefun
621
622 The POSIX locales contain some information about how to format numbers
623 (@pxref{General Numeric}).  This mainly deals with representing numbers
624 for better readability for humans.  The functions present so far in this
625 section cannot handle numbers in this form.
626
627 If this functionality is needed in a program one can use the functions
628 from the @code{scanf} family which know about the flag @samp{'} for
629 parsing numeric input (@pxref{Numeric Input Conversions}).  Sometimes it
630 is more desirable to have finer control.
631
632 In these situation one could use the function
633 @code{__strto@var{XXX}_internal}.  @var{XXX} here stands for any of the
634 above forms.  All numeric conversion functions (including the functions
635 to process floating-point numbers) have such a counterpart.  The
636 difference to the normal form is the extra argument at the end of the
637 parameter list.  If this value has an non-zero value the handling of
638 number grouping is enabled.  The advantage of using these functions is
639 that the @var{tailptr} parameters allow to determine which part of the
640 input is processed.  The @code{scanf} functions don't provide this
641 information.  The drawback of using these functions is that they are not
642 portable.  They only exist in the GNU C library.
643
644
645 Here is a function which parses a string as a sequence of integers and
646 returns the sum of them:
647
648 @smallexample
649 int
650 sum_ints_from_string (char *string)
651 @{
652   int sum = 0;
653
654   while (1) @{
655     char *tail;
656     int next;
657
658     /* @r{Skip whitespace by hand, to detect the end.}  */
659     while (isspace (*string)) string++;
660     if (*string == 0)
661       break;
662
663     /* @r{There is more nonwhitespace,}  */
664     /* @r{so it ought to be another number.}  */
665     errno = 0;
666     /* @r{Parse it.}  */
667     next = strtol (string, &tail, 0);
668     /* @r{Add it in, if not overflow.}  */
669     if (errno)
670       printf ("Overflow\n");
671     else
672       sum += next;
673     /* @r{Advance past it.}  */
674     string = tail;
675   @}
676
677   return sum;
678 @}
679 @end smallexample
680
681 @node Parsing of Floats
682 @subsection Parsing of Floats
683
684 @pindex stdlib.h
685 These functions are declared in @file{stdlib.h}.
686
687 @comment stdlib.h
688 @comment ISO
689 @deftypefun double strtod (const char *@var{string}, char **@var{tailptr})
690 The @code{strtod} (``string-to-double'') function converts the initial
691 part of @var{string} to a floating-point number, which is returned as a
692 value of type @code{double}.
693
694 This function attempts to decompose @var{string} as follows:
695
696 @itemize @bullet
697 @item
698 A (possibly empty) sequence of whitespace characters.  Which characters
699 are whitespace is determined by the @code{isspace} function
700 (@pxref{Classification of Characters}).  These are discarded.
701
702 @item
703 An optional plus or minus sign (@samp{+} or @samp{-}).
704
705 @item
706 A nonempty sequence of digits optionally containing a decimal-point
707 character---normally @samp{.}, but it depends on the locale
708 (@pxref{Numeric Formatting}).
709
710 @item
711 An optional exponent part, consisting of a character @samp{e} or
712 @samp{E}, an optional sign, and a sequence of digits.
713
714 @item
715 Any remaining characters in the string.  If @var{tailptr} is not a null
716 pointer, a pointer to this tail of the string is stored in
717 @code{*@var{tailptr}}.
718 @end itemize
719
720 If the string is empty, contains only whitespace, or does not contain an
721 initial substring that has the expected syntax for a floating-point
722 number, no conversion is performed.  In this case, @code{strtod} returns
723 a value of zero and the value returned in @code{*@var{tailptr}} is the
724 value of @var{string}.
725
726 In a locale other than the standard @code{"C"} or @code{"POSIX"} locales,
727 this function may recognize additional locale-dependent syntax.
728
729 If the string has valid syntax for a floating-point number but the value
730 is not representable because of overflow, @code{strtod} returns either
731 positive or negative @code{HUGE_VAL} (@pxref{Mathematics}), depending on
732 the sign of the value.  Similarly, if the value is not representable
733 because of underflow, @code{strtod} returns zero.  It also sets @code{errno}
734 to @code{ERANGE} if there was overflow or underflow.
735
736 There are two more special inputs which are recognized by @code{strtod}.
737 The string @code{"inf"} or @code{"infinity"} (without consideration of
738 case and optionally preceded by a @code{"+"} or @code{"-"} sign) is
739 changed to the floating-point value for infinity if the floating-point
740 format supports this; and to the largest representable value otherwise.
741
742 If the input string is @code{"nan"} or
743 @code{"nan(@var{n-char-sequence})"} the return value of @code{strtod} is
744 the representation of the NaN (not a number) value (if the
745 flaoting-point formats supports this.  The form with the
746 @var{n-char-sequence} enables in an implementation specific way to
747 specify the form of the NaN value.  When using the @w{IEEE 754}
748 floating-point format, the NaN value can have a lot of forms since only
749 at least one bit in the mantissa must be set.  In the GNU C library
750 implementation of @code{strtod} the @var{n-char-sequence} is interpreted
751 as a number (as recognized by @code{strtol}, @pxref{Parsing of Integers})
752 The mantissa of the return value corresponds to this given number.
753
754 Since the value zero which is returned in the error case is also a valid
755 result the user should set the global variable @code{errno} to zero
756 before calling this function.  So one can test for failures after the
757 call since all failures set @code{errno} to a non-zero value.
758 @end deftypefun
759
760 @comment stdlib.h
761 @comment GNU
762 @deftypefun float strtof (const char *@var{string}, char **@var{tailptr})
763 This function is similar to the @code{strtod} function but it returns a
764 @code{float} value instead of a @code{double} value.  If the precision
765 of a @code{float} value is sufficient this function should be used since
766 it is much faster than @code{strtod} on some architectures.  The reasons
767 are obvious: @w{IEEE 754} defines @code{float} to have a mantissa of 23
768 bits while @code{double} has 53 bits and every additional bit of
769 precision can require additional computation.
770
771 If the string has valid syntax for a floating-point number but the value
772 is not representable because of overflow, @code{strtof} returns either
773 positive or negative @code{HUGE_VALF} (@pxref{Mathematics}), depending on
774 the sign of the value.
775
776 This function is a GNU extension.
777 @end deftypefun
778
779 @comment stdlib.h
780 @comment GNU
781 @deftypefun {long double} strtold (const char *@var{string}, char **@var{tailptr})
782 This function is similar to the @code{strtod} function but it returns a
783 @code{long double} value instead of a @code{double} value.  It should be
784 used when high precision is needed.  On systems which define a @code{long
785 double} type (i.e., on which it is not the same as @code{double})
786 running this function might take significantly more time since more bits
787 of precision are required.
788
789 If the string has valid syntax for a floating-point number but the value
790 is not representable because of overflow, @code{strtold} returns either
791 positive or negative @code{HUGE_VALL} (@pxref{Mathematics}), depending on
792 the sign of the value.
793
794 This function is a GNU extension.
795 @end deftypefun
796
797 As for the integer parsing functions there are additional functions
798 which will handle numbers represented using the grouping scheme of the
799 current locale (@pxref{Parsing of Integers}).
800
801 @comment stdlib.h
802 @comment ISO
803 @deftypefun double atof (const char *@var{string})
804 This function is similar to the @code{strtod} function, except that it
805 need not detect overflow and underflow errors.  The @code{atof} function
806 is provided mostly for compatibility with existing code; using
807 @code{strtod} is more robust.
808 @end deftypefun