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