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