]> git.ipfire.org Git - thirdparty/glibc.git/blame - manual/arith.texi
Update.
[thirdparty/glibc.git] / manual / arith.texi
CommitLineData
28f540f4 1@node Arithmetic, Date and Time, Mathematics, Top
7a68c94a
UD
2@c %MENU% Low level arithmetic functions
3@chapter Arithmetic Functions
28f540f4
RM
4
5This chapter contains information about functions for doing basic
6arithmetic operations, such as splitting a float into its integer and
b4012b75
UD
7fractional parts or retrieving the imaginary part of a complex value.
8These functions are declared in the header files @file{math.h} and
9@file{complex.h}.
28f540f4
RM
10
11@menu
7a68c94a
UD
12* Floating Point Numbers:: Basic concepts. IEEE 754.
13* Floating Point Classes:: The five kinds of floating-point number.
14* Floating Point Errors:: When something goes wrong in a calculation.
15* Rounding:: Controlling how results are rounded.
16* Control Functions:: Saving and restoring the FPU's state.
17* Arithmetic Functions:: Fundamental operations provided by the library.
18* Complex Numbers:: The types. Writing complex constants.
19* Operations on Complex:: Projection, conjugation, decomposition.
20* Integer Division:: Integer division with guaranteed rounding.
21* Parsing of Numbers:: Converting strings to numbers.
22* System V Number Conversion:: An archaic way to convert numbers to strings.
28f540f4
RM
23@end menu
24
7a68c94a
UD
25@node Floating Point Numbers
26@section Floating Point Numbers
27@cindex floating point
28@cindex IEEE 754
b4012b75
UD
29@cindex IEEE floating point
30
7a68c94a
UD
31Most computer hardware has support for two different kinds of numbers:
32integers (@math{@dots{}-3, -2, -1, 0, 1, 2, 3@dots{}}) and
33floating-point numbers. Floating-point numbers have three parts: the
34@dfn{mantissa}, the @dfn{exponent}, and the @dfn{sign bit}. The real
35number represented by a floating-point value is given by
36@tex
37$(s \mathrel? -1 \mathrel: 1) \cdot 2^e \cdot M$
38@end tex
39@ifnottex
40@math{(s ? -1 : 1) @mul{} 2^e @mul{} M}
41@end ifnottex
42where @math{s} is the sign bit, @math{e} the exponent, and @math{M}
43the mantissa. @xref{Floating Point Concepts}, for details. (It is
44possible to have a different @dfn{base} for the exponent, but all modern
45hardware uses @math{2}.)
46
47Floating-point numbers can represent a finite subset of the real
48numbers. While this subset is large enough for most purposes, it is
49important to remember that the only reals that can be represented
50exactly are rational numbers that have a terminating binary expansion
51shorter than the width of the mantissa. Even simple fractions such as
52@math{1/5} can only be approximated by floating point.
53
54Mathematical operations and functions frequently need to produce values
55that are not representable. Often these values can be approximated
56closely enough for practical purposes, but sometimes they can't.
57Historically there was no way to tell when the results of a calculation
58were inaccurate. Modern computers implement the @w{IEEE 754} standard
59for numerical computations, which defines a framework for indicating to
60the program when the results of calculation are not trustworthy. This
61framework consists of a set of @dfn{exceptions} that indicate why a
62result could not be represented, and the special values @dfn{infinity}
63and @dfn{not a number} (NaN).
64
65@node Floating Point Classes
66@section Floating-Point Number Classification Functions
67@cindex floating-point classes
68@cindex classes, floating-point
69@pindex math.h
b4012b75 70
7a68c94a
UD
71@w{ISO C 9x} defines macros that let you determine what sort of
72floating-point number a variable holds.
b4012b75
UD
73
74@comment math.h
75@comment ISO
7a68c94a
UD
76@deftypefn {Macro} int fpclassify (@emph{float-type} @var{x})
77This is a generic macro which works on all floating-point types and
78which returns a value of type @code{int}. The possible values are:
28f540f4 79
7a68c94a
UD
80@vtable @code
81@item FP_NAN
82The floating-point number @var{x} is ``Not a Number'' (@pxref{Infinity
83and NaN})
84@item FP_INFINITE
85The value of @var{x} is either plus or minus infinity (@pxref{Infinity
86and NaN})
87@item FP_ZERO
88The value of @var{x} is zero. In floating-point formats like @w{IEEE
89754}, where zero can be signed, this value is also returned if
90@var{x} is negative zero.
91@item FP_SUBNORMAL
92Numbers whose absolute value is too small to be represented in the
93normal format are represented in an alternate, @dfn{denormalized} format
94(@pxref{Floating Point Concepts}). This format is less precise but can
95represent values closer to zero. @code{fpclassify} returns this value
96for values of @var{x} in this alternate format.
97@item FP_NORMAL
98This value is returned for all other values of @var{x}. It indicates
99that there is nothing special about the number.
100@end vtable
28f540f4 101
7a68c94a 102@end deftypefn
28f540f4 103
7a68c94a
UD
104@code{fpclassify} is most useful if more than one property of a number
105must be tested. There are more specific macros which only test one
106property at a time. Generally these macros execute faster than
107@code{fpclassify}, since there is special hardware support for them.
108You should therefore use the specific macros whenever possible.
28f540f4
RM
109
110@comment math.h
7a68c94a
UD
111@comment ISO
112@deftypefn {Macro} int isfinite (@emph{float-type} @var{x})
113This macro returns a nonzero value if @var{x} is finite: not plus or
114minus infinity, and not NaN. It is equivalent to
fe0ec73e
UD
115
116@smallexample
7a68c94a 117(fpclassify (x) != FP_NAN && fpclassify (x) != FP_INFINITE)
fe0ec73e
UD
118@end smallexample
119
7a68c94a
UD
120@code{isfinite} is implemented as a macro which accepts any
121floating-point type.
122@end deftypefn
fe0ec73e 123
7a68c94a
UD
124@comment math.h
125@comment ISO
126@deftypefn {Macro} int isnormal (@emph{float-type} @var{x})
127This macro returns a nonzero value if @var{x} is finite and normalized.
128It is equivalent to
b4012b75
UD
129
130@smallexample
7a68c94a 131(fpclassify (x) == FP_NORMAL)
b4012b75 132@end smallexample
7a68c94a 133@end deftypefn
b4012b75 134
7a68c94a
UD
135@comment math.h
136@comment ISO
137@deftypefn {Macro} int isnan (@emph{float-type} @var{x})
138This macro returns a nonzero value if @var{x} is NaN. It is equivalent
139to
b4012b75
UD
140
141@smallexample
7a68c94a 142(fpclassify (x) == FP_NAN)
b4012b75 143@end smallexample
7a68c94a 144@end deftypefn
b4012b75 145
7a68c94a
UD
146Another set of floating-point classification functions was provided by
147BSD. The GNU C library also supports these functions; however, we
148recommend that you use the C9x macros in new code. Those are standard
149and will be available more widely. Also, since they are macros, you do
150not have to worry about the type of their argument.
28f540f4
RM
151
152@comment math.h
153@comment BSD
154@deftypefun int isinf (double @var{x})
4260bc74
UD
155@comment math.h
156@comment BSD
779ae82e 157@deftypefunx int isinff (float @var{x})
4260bc74
UD
158@comment math.h
159@comment BSD
779ae82e 160@deftypefunx int isinfl (long double @var{x})
28f540f4
RM
161This function returns @code{-1} if @var{x} represents negative infinity,
162@code{1} if @var{x} represents positive infinity, and @code{0} otherwise.
163@end deftypefun
164
165@comment math.h
166@comment BSD
167@deftypefun int isnan (double @var{x})
4260bc74
UD
168@comment math.h
169@comment BSD
779ae82e 170@deftypefunx int isnanf (float @var{x})
4260bc74
UD
171@comment math.h
172@comment BSD
779ae82e 173@deftypefunx int isnanl (long double @var{x})
28f540f4 174This function returns a nonzero value if @var{x} is a ``not a number''
7a68c94a 175value, and zero otherwise.
b9b49b44 176
7a68c94a
UD
177@strong{Note:} The @code{isnan} macro defined by @w{ISO C 9x} overrides
178the BSD function. This is normally not a problem, because the two
179routines behave identically. However, if you really need to get the BSD
180function for some reason, you can write
b9b49b44 181
7a68c94a
UD
182@smallexample
183(isnan) (x)
184@end smallexample
28f540f4
RM
185@end deftypefun
186
187@comment math.h
188@comment BSD
189@deftypefun int finite (double @var{x})
4260bc74
UD
190@comment math.h
191@comment BSD
779ae82e 192@deftypefunx int finitef (float @var{x})
4260bc74
UD
193@comment math.h
194@comment BSD
779ae82e 195@deftypefunx int finitel (long double @var{x})
28f540f4
RM
196This function returns a nonzero value if @var{x} is finite or a ``not a
197number'' value, and zero otherwise.
198@end deftypefun
199
200@comment math.h
201@comment BSD
202@deftypefun double infnan (int @var{error})
7a68c94a
UD
203This function is provided for compatibility with BSD. Its argument is
204an error code, @code{EDOM} or @code{ERANGE}; @code{infnan} returns the
205value that a math function would return if it set @code{errno} to that
206value. @xref{Math Error Reporting}. @code{-ERANGE} is also acceptable
207as an argument, and corresponds to @code{-HUGE_VAL} as a value.
28f540f4
RM
208
209In the BSD library, on certain machines, @code{infnan} raises a fatal
210signal in all cases. The GNU library does not do likewise, because that
f65fd747 211does not fit the @w{ISO C} specification.
28f540f4
RM
212@end deftypefun
213
214@strong{Portability Note:} The functions listed in this section are BSD
215extensions.
216
b4012b75 217
7a68c94a
UD
218@node Floating Point Errors
219@section Errors in Floating-Point Calculations
220
221@menu
222* FP Exceptions:: IEEE 754 math exceptions and how to detect them.
223* Infinity and NaN:: Special values returned by calculations.
224* Status bit operations:: Checking for exceptions after the fact.
225* Math Error Reporting:: How the math functions report errors.
226@end menu
227
228@node FP Exceptions
229@subsection FP Exceptions
230@cindex exception
231@cindex signal
232@cindex zero divide
233@cindex division by zero
234@cindex inexact exception
235@cindex invalid exception
236@cindex overflow exception
237@cindex underflow exception
238
239The @w{IEEE 754} standard defines five @dfn{exceptions} that can occur
240during a calculation. Each corresponds to a particular sort of error,
241such as overflow.
242
243When exceptions occur (when exceptions are @dfn{raised}, in the language
244of the standard), one of two things can happen. By default the
245exception is simply noted in the floating-point @dfn{status word}, and
246the program continues as if nothing had happened. The operation
247produces a default value, which depends on the exception (see the table
248below). Your program can check the status word to find out which
249exceptions happened.
250
251Alternatively, you can enable @dfn{traps} for exceptions. In that case,
252when an exception is raised, your program will receive the @code{SIGFPE}
253signal. The default action for this signal is to terminate the
8b7fb588 254program. @xref{Signal Handling}, for how you can change the effect of
7a68c94a
UD
255the signal.
256
257@findex matherr
258In the System V math library, the user-defined function @code{matherr}
259is called when certain exceptions occur inside math library functions.
260However, the Unix98 standard deprecates this interface. We support it
261for historical compatibility, but recommend that you do not use it in
262new programs.
263
264@noindent
265The exceptions defined in @w{IEEE 754} are:
266
267@table @samp
268@item Invalid Operation
269This exception is raised if the given operands are invalid for the
270operation to be performed. Examples are
271(see @w{IEEE 754}, @w{section 7}):
272@enumerate
273@item
274Addition or subtraction: @math{@infinity{} - @infinity{}}. (But
275@math{@infinity{} + @infinity{} = @infinity{}}).
276@item
277Multiplication: @math{0 @mul{} @infinity{}}.
278@item
279Division: @math{0/0} or @math{@infinity{}/@infinity{}}.
280@item
281Remainder: @math{x} REM @math{y}, where @math{y} is zero or @math{x} is
282infinite.
283@item
284Square root if the operand is less then zero. More generally, any
285mathematical function evaluated outside its domain produces this
286exception.
287@item
288Conversion of a floating-point number to an integer or decimal
289string, when the number cannot be represented in the target format (due
290to overflow, infinity, or NaN).
291@item
292Conversion of an unrecognizable input string.
293@item
294Comparison via predicates involving @math{<} or @math{>}, when one or
295other of the operands is NaN. You can prevent this exception by using
296the unordered comparison functions instead; see @ref{FP Comparison Functions}.
297@end enumerate
298
299If the exception does not trap, the result of the operation is NaN.
300
301@item Division by Zero
302This exception is raised when a finite nonzero number is divided
303by zero. If no trap occurs the result is either @math{+@infinity{}} or
304@math{-@infinity{}}, depending on the signs of the operands.
305
306@item Overflow
307This exception is raised whenever the result cannot be represented
308as a finite value in the precision format of the destination. If no trap
309occurs the result depends on the sign of the intermediate result and the
310current rounding mode (@w{IEEE 754}, @w{section 7.3}):
311@enumerate
312@item
313Round to nearest carries all overflows to @math{@infinity{}}
314with the sign of the intermediate result.
315@item
316Round toward @math{0} carries all overflows to the largest representable
317finite number with the sign of the intermediate result.
318@item
319Round toward @math{-@infinity{}} carries positive overflows to the
320largest representable finite number and negative overflows to
321@math{-@infinity{}}.
322
323@item
324Round toward @math{@infinity{}} carries negative overflows to the
325most negative representable finite number and positive overflows
326to @math{@infinity{}}.
327@end enumerate
328
329Whenever the overflow exception is raised, the inexact exception is also
330raised.
331
332@item Underflow
333The underflow exception is raised when an intermediate result is too
334small to be calculated accurately, or if the operation's result rounded
335to the destination precision is too small to be normalized.
336
337When no trap is installed for the underflow exception, underflow is
338signaled (via the underflow flag) only when both tininess and loss of
339accuracy have been detected. If no trap handler is installed the
340operation continues with an imprecise small value, or zero if the
341destination precision cannot hold the small exact result.
342
343@item Inexact
344This exception is signalled if a rounded result is not exact (such as
345when calculating the square root of two) or a result overflows without
346an overflow trap.
347@end table
348
349@node Infinity and NaN
350@subsection Infinity and NaN
351@cindex infinity
352@cindex not a number
353@cindex NaN
354
355@w{IEEE 754} floating point numbers can represent positive or negative
356infinity, and @dfn{NaN} (not a number). These three values arise from
357calculations whose result is undefined or cannot be represented
358accurately. You can also deliberately set a floating-point variable to
359any of them, which is sometimes useful. Some examples of calculations
360that produce infinity or NaN:
361
362@ifnottex
363@smallexample
364@math{1/0 = @infinity{}}
365@math{log (0) = -@infinity{}}
366@math{sqrt (-1) = NaN}
367@end smallexample
368@end ifnottex
369@tex
370$${1\over0} = \infty$$
371$$\log 0 = -\infty$$
372$$\sqrt{-1} = \hbox{NaN}$$
373@end tex
374
375When a calculation produces any of these values, an exception also
376occurs; see @ref{FP Exceptions}.
377
378The basic operations and math functions all accept infinity and NaN and
379produce sensible output. Infinities propagate through calculations as
380one would expect: for example, @math{2 + @infinity{} = @infinity{}},
381@math{4/@infinity{} = 0}, atan @math{(@infinity{}) = @pi{}/2}. NaN, on
382the other hand, infects any calculation that involves it. Unless the
383calculation would produce the same result no matter what real value
384replaced NaN, the result is NaN.
385
386In comparison operations, positive infinity is larger than all values
387except itself and NaN, and negative infinity is smaller than all values
388except itself and NaN. NaN is @dfn{unordered}: it is not equal to,
389greater than, or less than anything, @emph{including itself}. @code{x ==
390x} is false if the value of @code{x} is NaN. You can use this to test
391whether a value is NaN or not, but the recommended way to test for NaN
392is with the @code{isnan} function (@pxref{Floating Point Classes}). In
393addition, @code{<}, @code{>}, @code{<=}, and @code{>=} will raise an
394exception when applied to NaNs.
395
396@file{math.h} defines macros that allow you to explicitly set a variable
397to infinity or NaN.
b4012b75
UD
398
399@comment math.h
400@comment ISO
7a68c94a
UD
401@deftypevr Macro float INFINITY
402An expression representing positive infinity. It is equal to the value
403produced by mathematical operations like @code{1.0 / 0.0}.
404@code{-INFINITY} represents negative infinity.
405
406You can test whether a floating-point value is infinite by comparing it
407to this macro. However, this is not recommended; you should use the
408@code{isfinite} macro instead. @xref{Floating Point Classes}.
409
410This macro was introduced in the @w{ISO C 9X} standard.
411@end deftypevr
412
413@comment math.h
414@comment GNU
415@deftypevr Macro float NAN
416An expression representing a value which is ``not a number''. This
417macro is a GNU extension, available only on machines that support the
418``not a number'' value---that is to say, on all machines that support
419IEEE floating point.
420
421You can use @samp{#ifdef NAN} to test whether the machine supports
422NaN. (Of course, you must arrange for GNU extensions to be visible,
423such as by defining @code{_GNU_SOURCE}, and then you must include
424@file{math.h}.)
425@end deftypevr
426
427@w{IEEE 754} also allows for another unusual value: negative zero. This
428value is produced when you divide a positive number by negative
429infinity, or when a negative result is smaller than the limits of
430representation. Negative zero behaves identically to zero in all
431calculations, unless you explicitly test the sign bit with
432@code{signbit} or @code{copysign}.
433
434@node Status bit operations
435@subsection Examining the FPU status word
436
437@w{ISO C 9x} defines functions to query and manipulate the
438floating-point status word. You can use these functions to check for
439untrapped exceptions when it's convenient, rather than worrying about
440them in the middle of a calculation.
441
442These constants represent the various @w{IEEE 754} exceptions. Not all
443FPUs report all the different exceptions. Each constant is defined if
444and only if the FPU you are compiling for supports that exception, so
445you can test for FPU support with @samp{#ifdef}. They are defined in
446@file{fenv.h}.
b4012b75
UD
447
448@vtable @code
7a68c94a
UD
449@comment fenv.h
450@comment ISO
451@item FE_INEXACT
452 The inexact exception.
453@comment fenv.h
454@comment ISO
455@item FE_DIVBYZERO
456 The divide by zero exception.
457@comment fenv.h
458@comment ISO
459@item FE_UNDERFLOW
460 The underflow exception.
461@comment fenv.h
462@comment ISO
463@item FE_OVERFLOW
464 The overflow exception.
465@comment fenv.h
466@comment ISO
467@item FE_INVALID
468 The invalid exception.
b4012b75
UD
469@end vtable
470
7a68c94a
UD
471The macro @code{FE_ALL_EXCEPT} is the bitwise OR of all exception macros
472which are supported by the FP implementation.
b4012b75 473
7a68c94a
UD
474These functions allow you to clear exception flags, test for exceptions,
475and save and restore the set of exceptions flagged.
b4012b75 476
7a68c94a 477@comment fenv.h
b4012b75 478@comment ISO
7a68c94a
UD
479@deftypefun void feclearexcept (int @var{excepts})
480This function clears all of the supported exception flags indicated by
481@var{excepts}.
482@end deftypefun
483
484@comment fenv.h
485@comment ISO
486@deftypefun int fetestexcept (int @var{excepts})
487Test whether the exception flags indicated by the parameter @var{except}
488are currently set. If any of them are, a nonzero value is returned
489which specifies which exceptions are set. Otherwise the result is zero.
490@end deftypefun
491
492To understand these functions, imagine that the status word is an
493integer variable named @var{status}. @code{feclearexcept} is then
494equivalent to @samp{status &= ~excepts} and @code{fetestexcept} is
495equivalent to @samp{(status & excepts)}. The actual implementation may
496be very different, of course.
497
498Exception flags are only cleared when the program explicitly requests it,
499by calling @code{feclearexcept}. If you want to check for exceptions
500from a set of calculations, you should clear all the flags first. Here
501is a simple example of the way to use @code{fetestexcept}:
b4012b75
UD
502
503@smallexample
7a68c94a
UD
504@{
505 double f;
506 int raised;
507 feclearexcept (FE_ALL_EXCEPT);
508 f = compute ();
509 raised = fetestexcept (FE_OVERFLOW | FE_INVALID);
510 if (raised & FE_OVERFLOW) @{ /* ... */ @}
511 if (raised & FE_INVALID) @{ /* ... */ @}
512 /* ... */
513@}
b4012b75
UD
514@end smallexample
515
7a68c94a
UD
516You cannot explicitly set bits in the status word. You can, however,
517save the entire status word and restore it later. This is done with the
518following functions:
b4012b75 519
7a68c94a 520@comment fenv.h
b4012b75 521@comment ISO
7a68c94a
UD
522@deftypefun void fegetexceptflag (fexcept_t *@var{flagp}, int @var{excepts})
523This function stores in the variable pointed to by @var{flagp} an
524implementation-defined value representing the current setting of the
525exception flags indicated by @var{excepts}.
526@end deftypefun
b4012b75 527
7a68c94a
UD
528@comment fenv.h
529@comment ISO
530@deftypefun void fesetexceptflag (const fexcept_t *@var{flagp}, int
531@var{excepts})
532This function restores the flags for the exceptions indicated by
533@var{excepts} to the values stored in the variable pointed to by
534@var{flagp}.
535@end deftypefun
536
537Note that the value stored in @code{fexcept_t} bears no resemblance to
538the bit mask returned by @code{fetestexcept}. The type may not even be
539an integer. Do not attempt to modify an @code{fexcept_t} variable.
540
541@node Math Error Reporting
542@subsection Error Reporting by Mathematical Functions
543@cindex errors, mathematical
544@cindex domain error
545@cindex range error
546
547Many of the math functions are defined only over a subset of the real or
548complex numbers. Even if they are mathematically defined, their result
549may be larger or smaller than the range representable by their return
550type. These are known as @dfn{domain errors}, @dfn{overflows}, and
551@dfn{underflows}, respectively. Math functions do several things when
552one of these errors occurs. In this manual we will refer to the
553complete response as @dfn{signalling} a domain error, overflow, or
554underflow.
555
556When a math function suffers a domain error, it raises the invalid
557exception and returns NaN. It also sets @var{errno} to @code{EDOM};
558this is for compatibility with old systems that do not support @w{IEEE
559754} exception handling. Likewise, when overflow occurs, math
560functions raise the overflow exception and return @math{@infinity{}} or
561@math{-@infinity{}} as appropriate. They also set @var{errno} to
562@code{ERANGE}. When underflow occurs, the underflow exception is
563raised, and zero (appropriately signed) is returned. @var{errno} may be
564set to @code{ERANGE}, but this is not guaranteed.
565
566Some of the math functions are defined mathematically to result in a
567complex value over parts of their domains. The most familiar example of
568this is taking the square root of a negative number. The complex math
569functions, such as @code{csqrt}, will return the appropriate complex value
570in this case. The real-valued functions, such as @code{sqrt}, will
571signal a domain error.
572
573Some older hardware does not support infinities. On that hardware,
574overflows instead return a particular very large number (usually the
575largest representable number). @file{math.h} defines macros you can use
576to test for overflow on both old and new hardware.
b4012b75
UD
577
578@comment math.h
579@comment ISO
7a68c94a 580@deftypevr Macro double HUGE_VAL
4260bc74
UD
581@comment math.h
582@comment ISO
7a68c94a 583@deftypevrx Macro float HUGE_VALF
4260bc74
UD
584@comment math.h
585@comment ISO
7a68c94a
UD
586@deftypevrx Macro {long double} HUGE_VALL
587An expression representing a particular very large number. On machines
588that use @w{IEEE 754} floating point format, @code{HUGE_VAL} is infinity.
589On other machines, it's typically the largest positive number that can
590be represented.
591
592Mathematical functions return the appropriately typed version of
593@code{HUGE_VAL} or @code{@minus{}HUGE_VAL} when the result is too large
594to be represented.
595@end deftypevr
b4012b75 596
7a68c94a
UD
597@node Rounding
598@section Rounding Modes
599
600Floating-point calculations are carried out internally with extra
601precision, and then rounded to fit into the destination type. This
602ensures that results are as precise as the input data. @w{IEEE 754}
603defines four possible rounding modes:
604
605@table @asis
606@item Round to nearest.
607This is the default mode. It should be used unless there is a specific
608need for one of the others. In this mode results are rounded to the
609nearest representable value. If the result is midway between two
610representable values, the even representable is chosen. @dfn{Even} here
611means the lowest-order bit is zero. This rounding mode prevents
612statistical bias and guarantees numeric stability: round-off errors in a
613lengthy calculation will remain smaller than half of @code{FLT_EPSILON}.
614
615@c @item Round toward @math{+@infinity{}}
616@item Round toward plus Infinity.
617All results are rounded to the smallest representable value
618which is greater than the result.
619
620@c @item Round toward @math{-@infinity{}}
621@item Round toward minus Infinity.
622All results are rounded to the largest representable value which is less
623than the result.
624
625@item Round toward zero.
626All results are rounded to the largest representable value whose
627magnitude is less than that of the result. In other words, if the
628result is negative it is rounded up; if it is positive, it is rounded
629down.
630@end table
b4012b75 631
7a68c94a
UD
632@noindent
633@file{fenv.h} defines constants which you can use to refer to the
634various rounding modes. Each one will be defined if and only if the FPU
635supports the corresponding rounding mode.
b4012b75 636
7a68c94a
UD
637@table @code
638@comment fenv.h
639@comment ISO
640@vindex FE_TONEAREST
641@item FE_TONEAREST
642Round to nearest.
b4012b75 643
7a68c94a
UD
644@comment fenv.h
645@comment ISO
646@vindex FE_UPWARD
647@item FE_UPWARD
648Round toward @math{+@infinity{}}.
b4012b75 649
7a68c94a
UD
650@comment fenv.h
651@comment ISO
652@vindex FE_DOWNWARD
653@item FE_DOWNWARD
654Round toward @math{-@infinity{}}.
b4012b75 655
7a68c94a
UD
656@comment fenv.h
657@comment ISO
658@vindex FE_TOWARDZERO
659@item FE_TOWARDZERO
660Round toward zero.
661@end table
b4012b75 662
7a68c94a
UD
663Underflow is an unusual case. Normally, @w{IEEE 754} floating point
664numbers are always normalized (@pxref{Floating Point Concepts}).
665Numbers smaller than @math{2^r} (where @math{r} is the minimum exponent,
666@code{FLT_MIN_RADIX-1} for @var{float}) cannot be represented as
667normalized numbers. Rounding all such numbers to zero or @math{2^r}
668would cause some algorithms to fail at 0. Therefore, they are left in
669denormalized form. That produces loss of precision, since some bits of
670the mantissa are stolen to indicate the decimal point.
671
672If a result is too small to be represented as a denormalized number, it
673is rounded to zero. However, the sign of the result is preserved; if
674the calculation was negative, the result is @dfn{negative zero}.
675Negative zero can also result from some operations on infinity, such as
676@math{4/-@infinity{}}. Negative zero behaves identically to zero except
677when the @code{copysign} or @code{signbit} functions are used to check
678the sign bit directly.
679
680At any time one of the above four rounding modes is selected. You can
681find out which one with this function:
682
683@comment fenv.h
684@comment ISO
685@deftypefun int fegetround (void)
686Returns the currently selected rounding mode, represented by one of the
687values of the defined rounding mode macros.
688@end deftypefun
b4012b75 689
7a68c94a
UD
690@noindent
691To change the rounding mode, use this function:
b4012b75 692
7a68c94a
UD
693@comment fenv.h
694@comment ISO
695@deftypefun int fesetround (int @var{round})
696Changes the currently selected rounding mode to @var{round}. If
697@var{round} does not correspond to one of the supported rounding modes
698nothing is changed. @code{fesetround} returns a nonzero value if it
699changed the rounding mode, zero if the mode is not supported.
700@end deftypefun
b4012b75 701
7a68c94a
UD
702You should avoid changing the rounding mode if possible. It can be an
703expensive operation; also, some hardware requires you to compile your
704program differently for it to work. The resulting code may run slower.
705See your compiler documentation for details.
706@c This section used to claim that functions existed to round one number
707@c in a specific fashion. I can't find any functions in the library
708@c that do that. -zw
709
710@node Control Functions
711@section Floating-Point Control Functions
712
713@w{IEEE 754} floating-point implementations allow the programmer to
714decide whether traps will occur for each of the exceptions, by setting
715bits in the @dfn{control word}. In C, traps result in the program
716receiving the @code{SIGFPE} signal; see @ref{Signal Handling}.
717
718@strong{Note:} @w{IEEE 754} says that trap handlers are given details of
719the exceptional situation, and can set the result value. C signals do
720not provide any mechanism to pass this information back and forth.
721Trapping exceptions in C is therefore not very useful.
722
723It is sometimes necessary to save the state of the floating-point unit
724while you perform some calculation. The library provides functions
725which save and restore the exception flags, the set of exceptions that
726generate traps, and the rounding mode. This information is known as the
727@dfn{floating-point environment}.
728
729The functions to save and restore the floating-point environment all use
730a variable of type @code{fenv_t} to store information. This type is
731defined in @file{fenv.h}. Its size and contents are
732implementation-defined. You should not attempt to manipulate a variable
733of this type directly.
734
735To save the state of the FPU, use one of these functions:
736
737@comment fenv.h
b4012b75 738@comment ISO
7a68c94a
UD
739@deftypefun void fegetenv (fenv_t *@var{envp})
740Store the floating-point environment in the variable pointed to by
741@var{envp}.
b4012b75
UD
742@end deftypefun
743
7a68c94a 744@comment fenv.h
b4012b75 745@comment ISO
7a68c94a
UD
746@deftypefun int feholdexcept (fenv_t *@var{envp})
747Store the current floating-point environment in the object pointed to by
748@var{envp}. Then clear all exception flags, and set the FPU to trap no
749exceptions. Not all FPUs support trapping no exceptions; if
750@code{feholdexcept} cannot set this mode, it returns zero. If it
751succeeds, it returns a nonzero value.
b4012b75
UD
752@end deftypefun
753
7a68c94a
UD
754The functions which restore the floating-point environment can take two
755kinds of arguments:
b4012b75 756
7a68c94a
UD
757@itemize @bullet
758@item
759Pointers to @code{fenv_t} objects, which were initialized previously by a
760call to @code{fegetenv} or @code{feholdexcept}.
761@item
762@vindex FE_DFL_ENV
763The special macro @code{FE_DFL_ENV} which represents the floating-point
764environment as it was available at program start.
765@item
766Implementation defined macros with names starting with @code{FE_}.
b4012b75 767
7a68c94a
UD
768@vindex FE_NOMASK_ENV
769If possible, the GNU C Library defines a macro @code{FE_NOMASK_ENV}
770which represents an environment where every exception raised causes a
771trap to occur. You can test for this macro using @code{#ifdef}. It is
772only defined if @code{_GNU_SOURCE} is defined.
773
774Some platforms might define other predefined environments.
775@end itemize
776
777@noindent
778To set the floating-point environment, you can use either of these
779functions:
780
781@comment fenv.h
b4012b75 782@comment ISO
7a68c94a
UD
783@deftypefun void fesetenv (const fenv_t *@var{envp})
784Set the floating-point environment to that described by @var{envp}.
b4012b75
UD
785@end deftypefun
786
7a68c94a 787@comment fenv.h
b4012b75 788@comment ISO
7a68c94a
UD
789@deftypefun void feupdateenv (const fenv_t *@var{envp})
790Like @code{fesetenv}, this function sets the floating-point environment
791to that described by @var{envp}. However, if any exceptions were
792flagged in the status word before @code{feupdateenv} was called, they
793remain flagged after the call. In other words, after @code{feupdateenv}
794is called, the status word is the bitwise OR of the previous status word
795and the one saved in @var{envp}.
b4012b75
UD
796@end deftypefun
797
7a68c94a
UD
798@node Arithmetic Functions
799@section Arithmetic Functions
b4012b75 800
7a68c94a
UD
801The C library provides functions to do basic operations on
802floating-point numbers. These include absolute value, maximum and minimum,
803normalization, bit twiddling, rounding, and a few others.
b4012b75 804
7a68c94a
UD
805@menu
806* Absolute Value:: Absolute values of integers and floats.
807* Normalization Functions:: Extracting exponents and putting them back.
808* Rounding Functions:: Rounding floats to integers.
809* Remainder Functions:: Remainders on division, precisely defined.
810* FP Bit Twiddling:: Sign bit adjustment. Adding epsilon.
811* FP Comparison Functions:: Comparisons without risk of exceptions.
812* Misc FP Arithmetic:: Max, min, positive difference, multiply-add.
813@end menu
b4012b75 814
28f540f4 815@node Absolute Value
7a68c94a 816@subsection Absolute Value
28f540f4
RM
817@cindex absolute value functions
818
819These functions are provided for obtaining the @dfn{absolute value} (or
820@dfn{magnitude}) of a number. The absolute value of a real number
2d26e9eb 821@var{x} is @var{x} if @var{x} is positive, @minus{}@var{x} if @var{x} is
28f540f4
RM
822negative. For a complex number @var{z}, whose real part is @var{x} and
823whose imaginary part is @var{y}, the absolute value is @w{@code{sqrt
824(@var{x}*@var{x} + @var{y}*@var{y})}}.
825
826@pindex math.h
827@pindex stdlib.h
fe0ec73e 828Prototypes for @code{abs}, @code{labs} and @code{llabs} are in @file{stdlib.h};
e518937a 829@code{imaxabs} is declared in @file{inttypes.h};
7a68c94a 830@code{fabs}, @code{fabsf} and @code{fabsl} are declared in @file{math.h}.
b4012b75 831@code{cabs}, @code{cabsf} and @code{cabsl} are declared in @file{complex.h}.
28f540f4
RM
832
833@comment stdlib.h
f65fd747 834@comment ISO
28f540f4 835@deftypefun int abs (int @var{number})
4260bc74
UD
836@comment stdlib.h
837@comment ISO
7a68c94a 838@deftypefunx {long int} labs (long int @var{number})
4260bc74
UD
839@comment stdlib.h
840@comment ISO
7a68c94a 841@deftypefunx {long long int} llabs (long long int @var{number})
e518937a
UD
842@comment inttypes.h
843@comment ISO
844@deftypefunx intmax_t imaxabs (intmax_t @var{number})
7a68c94a 845These functions return the absolute value of @var{number}.
28f540f4
RM
846
847Most computers use a two's complement integer representation, in which
848the absolute value of @code{INT_MIN} (the smallest possible @code{int})
849cannot be represented; thus, @w{@code{abs (INT_MIN)}} is not defined.
28f540f4 850
e518937a 851@code{llabs} and @code{imaxdiv} are new to @w{ISO C 9x}.
fe0ec73e
UD
852@end deftypefun
853
28f540f4 854@comment math.h
f65fd747 855@comment ISO
28f540f4 856@deftypefun double fabs (double @var{number})
4260bc74
UD
857@comment math.h
858@comment ISO
779ae82e 859@deftypefunx float fabsf (float @var{number})
4260bc74
UD
860@comment math.h
861@comment ISO
779ae82e 862@deftypefunx {long double} fabsl (long double @var{number})
28f540f4
RM
863This function returns the absolute value of the floating-point number
864@var{number}.
865@end deftypefun
866
b4012b75
UD
867@comment complex.h
868@comment ISO
869@deftypefun double cabs (complex double @var{z})
4260bc74
UD
870@comment complex.h
871@comment ISO
779ae82e 872@deftypefunx float cabsf (complex float @var{z})
4260bc74
UD
873@comment complex.h
874@comment ISO
779ae82e 875@deftypefunx {long double} cabsl (complex long double @var{z})
7a68c94a
UD
876These functions return the absolute value of the complex number @var{z}
877(@pxref{Complex Numbers}). The absolute value of a complex number is:
28f540f4
RM
878
879@smallexample
b4012b75 880sqrt (creal (@var{z}) * creal (@var{z}) + cimag (@var{z}) * cimag (@var{z}))
28f540f4 881@end smallexample
dfd2257a 882
7a68c94a
UD
883This function should always be used instead of the direct formula
884because it takes special care to avoid losing precision. It may also
885take advantage of hardware support for this operation. See @code{hypot}
8b7fb588 886in @ref{Exponents and Logarithms}.
28f540f4
RM
887@end deftypefun
888
889@node Normalization Functions
7a68c94a 890@subsection Normalization Functions
28f540f4
RM
891@cindex normalization functions (floating-point)
892
893The functions described in this section are primarily provided as a way
894to efficiently perform certain low-level manipulations on floating point
895numbers that are represented internally using a binary radix;
896see @ref{Floating Point Concepts}. These functions are required to
897have equivalent behavior even if the representation does not use a radix
898of 2, but of course they are unlikely to be particularly efficient in
899those cases.
900
901@pindex math.h
902All these functions are declared in @file{math.h}.
903
904@comment math.h
f65fd747 905@comment ISO
28f540f4 906@deftypefun double frexp (double @var{value}, int *@var{exponent})
4260bc74
UD
907@comment math.h
908@comment ISO
779ae82e 909@deftypefunx float frexpf (float @var{value}, int *@var{exponent})
4260bc74
UD
910@comment math.h
911@comment ISO
779ae82e 912@deftypefunx {long double} frexpl (long double @var{value}, int *@var{exponent})
b4012b75 913These functions are used to split the number @var{value}
28f540f4
RM
914into a normalized fraction and an exponent.
915
916If the argument @var{value} is not zero, the return value is @var{value}
917times a power of two, and is always in the range 1/2 (inclusive) to 1
918(exclusive). The corresponding exponent is stored in
919@code{*@var{exponent}}; the return value multiplied by 2 raised to this
920exponent equals the original number @var{value}.
921
922For example, @code{frexp (12.8, &exponent)} returns @code{0.8} and
923stores @code{4} in @code{exponent}.
924
925If @var{value} is zero, then the return value is zero and
926zero is stored in @code{*@var{exponent}}.
927@end deftypefun
928
929@comment math.h
f65fd747 930@comment ISO
28f540f4 931@deftypefun double ldexp (double @var{value}, int @var{exponent})
4260bc74
UD
932@comment math.h
933@comment ISO
779ae82e 934@deftypefunx float ldexpf (float @var{value}, int @var{exponent})
4260bc74
UD
935@comment math.h
936@comment ISO
779ae82e 937@deftypefunx {long double} ldexpl (long double @var{value}, int @var{exponent})
b4012b75 938These functions return the result of multiplying the floating-point
28f540f4
RM
939number @var{value} by 2 raised to the power @var{exponent}. (It can
940be used to reassemble floating-point numbers that were taken apart
941by @code{frexp}.)
942
943For example, @code{ldexp (0.8, 4)} returns @code{12.8}.
944@end deftypefun
945
7a68c94a
UD
946The following functions, which come from BSD, provide facilities
947equivalent to those of @code{ldexp} and @code{frexp}.
28f540f4
RM
948
949@comment math.h
950@comment BSD
951@deftypefun double logb (double @var{x})
4260bc74
UD
952@comment math.h
953@comment BSD
779ae82e 954@deftypefunx float logbf (float @var{x})
4260bc74
UD
955@comment math.h
956@comment BSD
779ae82e 957@deftypefunx {long double} logbl (long double @var{x})
7a68c94a 958These functions return the integer part of the base-2 logarithm of
28f540f4
RM
959@var{x}, an integer value represented in type @code{double}. This is
960the highest integer power of @code{2} contained in @var{x}. The sign of
961@var{x} is ignored. For example, @code{logb (3.5)} is @code{1.0} and
962@code{logb (4.0)} is @code{2.0}.
963
964When @code{2} raised to this power is divided into @var{x}, it gives a
965quotient between @code{1} (inclusive) and @code{2} (exclusive).
966
7a68c94a
UD
967If @var{x} is zero, the return value is minus infinity if the machine
968supports infinities, and a very small number if it does not. If @var{x}
969is infinity, the return value is infinity.
970
971For finite @var{x}, the value returned by @code{logb} is one less than
972the value that @code{frexp} would store into @code{*@var{exponent}}.
973@end deftypefun
974
975@comment math.h
976@comment BSD
977@deftypefun double scalb (double @var{value}, int @var{exponent})
4260bc74
UD
978@comment math.h
979@comment BSD
7a68c94a 980@deftypefunx float scalbf (float @var{value}, int @var{exponent})
4260bc74
UD
981@comment math.h
982@comment BSD
7a68c94a
UD
983@deftypefunx {long double} scalbl (long double @var{value}, int @var{exponent})
984The @code{scalb} function is the BSD name for @code{ldexp}.
985@end deftypefun
986
987@comment math.h
988@comment BSD
989@deftypefun {long long int} scalbn (double @var{x}, int n)
4260bc74
UD
990@comment math.h
991@comment BSD
7a68c94a 992@deftypefunx {long long int} scalbnf (float @var{x}, int n)
4260bc74
UD
993@comment math.h
994@comment BSD
7a68c94a
UD
995@deftypefunx {long long int} scalbnl (long double @var{x}, int n)
996@code{scalbn} is identical to @code{scalb}, except that the exponent
997@var{n} is an @code{int} instead of a floating-point number.
998@end deftypefun
999
1000@comment math.h
1001@comment BSD
1002@deftypefun {long long int} scalbln (double @var{x}, long int n)
4260bc74
UD
1003@comment math.h
1004@comment BSD
7a68c94a 1005@deftypefunx {long long int} scalblnf (float @var{x}, long int n)
4260bc74
UD
1006@comment math.h
1007@comment BSD
7a68c94a
UD
1008@deftypefunx {long long int} scalblnl (long double @var{x}, long int n)
1009@code{scalbln} is identical to @code{scalb}, except that the exponent
1010@var{n} is a @code{long int} instead of a floating-point number.
1011@end deftypefun
28f540f4 1012
7a68c94a
UD
1013@comment math.h
1014@comment BSD
1015@deftypefun {long long int} significand (double @var{x})
4260bc74
UD
1016@comment math.h
1017@comment BSD
7a68c94a 1018@deftypefunx {long long int} significandf (float @var{x})
4260bc74
UD
1019@comment math.h
1020@comment BSD
7a68c94a
UD
1021@deftypefunx {long long int} significandl (long double @var{x})
1022@code{significand} returns the mantissa of @var{x} scaled to the range
1023@math{[1, 2)}.
1024It is equivalent to @w{@code{scalb (@var{x}, (double) -ilogb (@var{x}))}}.
1025
1026This function exists mainly for use in certain standardized tests
1027of @w{IEEE 754} conformance.
28f540f4
RM
1028@end deftypefun
1029
7a68c94a
UD
1030@node Rounding Functions
1031@subsection Rounding Functions
28f540f4
RM
1032@cindex converting floats to integers
1033
1034@pindex math.h
7a68c94a
UD
1035The functions listed here perform operations such as rounding and
1036truncation of floating-point values. Some of these functions convert
1037floating point numbers to integer values. They are all declared in
1038@file{math.h}.
28f540f4
RM
1039
1040You can also convert floating-point numbers to integers simply by
1041casting them to @code{int}. This discards the fractional part,
1042effectively rounding towards zero. However, this only works if the
1043result can actually be represented as an @code{int}---for very large
1044numbers, this is impossible. The functions listed here return the
1045result as a @code{double} instead to get around this problem.
1046
1047@comment math.h
f65fd747 1048@comment ISO
28f540f4 1049@deftypefun double ceil (double @var{x})
4260bc74
UD
1050@comment math.h
1051@comment ISO
779ae82e 1052@deftypefunx float ceilf (float @var{x})
4260bc74
UD
1053@comment math.h
1054@comment ISO
779ae82e 1055@deftypefunx {long double} ceill (long double @var{x})
b4012b75 1056These functions round @var{x} upwards to the nearest integer,
28f540f4
RM
1057returning that value as a @code{double}. Thus, @code{ceil (1.5)}
1058is @code{2.0}.
1059@end deftypefun
1060
1061@comment math.h
f65fd747 1062@comment ISO
28f540f4 1063@deftypefun double floor (double @var{x})
4260bc74
UD
1064@comment math.h
1065@comment ISO
779ae82e 1066@deftypefunx float floorf (float @var{x})
4260bc74
UD
1067@comment math.h
1068@comment ISO
779ae82e 1069@deftypefunx {long double} floorl (long double @var{x})
b4012b75 1070These functions round @var{x} downwards to the nearest
28f540f4
RM
1071integer, returning that value as a @code{double}. Thus, @code{floor
1072(1.5)} is @code{1.0} and @code{floor (-1.5)} is @code{-2.0}.
1073@end deftypefun
1074
7a68c94a
UD
1075@comment math.h
1076@comment ISO
1077@deftypefun double trunc (double @var{x})
4260bc74
UD
1078@comment math.h
1079@comment ISO
7a68c94a 1080@deftypefunx float truncf (float @var{x})
4260bc74
UD
1081@comment math.h
1082@comment ISO
7a68c94a
UD
1083@deftypefunx {long double} truncl (long double @var{x})
1084@code{trunc} is another name for @code{floor}
1085@end deftypefun
1086
28f540f4 1087@comment math.h
b4012b75 1088@comment ISO
28f540f4 1089@deftypefun double rint (double @var{x})
4260bc74
UD
1090@comment math.h
1091@comment ISO
779ae82e 1092@deftypefunx float rintf (float @var{x})
4260bc74
UD
1093@comment math.h
1094@comment ISO
779ae82e 1095@deftypefunx {long double} rintl (long double @var{x})
b4012b75 1096These functions round @var{x} to an integer value according to the
28f540f4
RM
1097current rounding mode. @xref{Floating Point Parameters}, for
1098information about the various rounding modes. The default
1099rounding mode is to round to the nearest integer; some machines
1100support other modes, but round-to-nearest is always used unless
7a68c94a
UD
1101you explicitly select another.
1102
1103If @var{x} was not initially an integer, these functions raise the
1104inexact exception.
28f540f4
RM
1105@end deftypefun
1106
b4012b75
UD
1107@comment math.h
1108@comment ISO
1109@deftypefun double nearbyint (double @var{x})
4260bc74
UD
1110@comment math.h
1111@comment ISO
779ae82e 1112@deftypefunx float nearbyintf (float @var{x})
4260bc74
UD
1113@comment math.h
1114@comment ISO
779ae82e 1115@deftypefunx {long double} nearbyintl (long double @var{x})
7a68c94a
UD
1116These functions return the same value as the @code{rint} functions, but
1117do not raise the inexact exception if @var{x} is not an integer.
1118@end deftypefun
1119
1120@comment math.h
1121@comment ISO
1122@deftypefun double round (double @var{x})
4260bc74
UD
1123@comment math.h
1124@comment ISO
7a68c94a 1125@deftypefunx float roundf (float @var{x})
4260bc74
UD
1126@comment math.h
1127@comment ISO
7a68c94a
UD
1128@deftypefunx {long double} roundl (long double @var{x})
1129These functions are similar to @code{rint}, but they round halfway
1130cases away from zero instead of to the nearest even integer.
1131@end deftypefun
1132
1133@comment math.h
1134@comment ISO
1135@deftypefun {long int} lrint (double @var{x})
4260bc74
UD
1136@comment math.h
1137@comment ISO
7a68c94a 1138@deftypefunx {long int} lrintf (float @var{x})
4260bc74
UD
1139@comment math.h
1140@comment ISO
7a68c94a
UD
1141@deftypefunx {long int} lrintl (long double @var{x})
1142These functions are just like @code{rint}, but they return a
1143@code{long int} instead of a floating-point number.
1144@end deftypefun
1145
1146@comment math.h
1147@comment ISO
1148@deftypefun {long long int} llrint (double @var{x})
4260bc74
UD
1149@comment math.h
1150@comment ISO
7a68c94a 1151@deftypefunx {long long int} llrintf (float @var{x})
4260bc74
UD
1152@comment math.h
1153@comment ISO
7a68c94a
UD
1154@deftypefunx {long long int} llrintl (long double @var{x})
1155These functions are just like @code{rint}, but they return a
1156@code{long long int} instead of a floating-point number.
b4012b75
UD
1157@end deftypefun
1158
7a68c94a
UD
1159@comment math.h
1160@comment ISO
1161@deftypefun {long int} lround (double @var{x})
4260bc74
UD
1162@comment math.h
1163@comment ISO
7a68c94a 1164@deftypefunx {long int} lroundf (float @var{x})
4260bc74
UD
1165@comment math.h
1166@comment ISO
7a68c94a
UD
1167@deftypefunx {long int} lroundl (long double @var{x})
1168These functions are just like @code{round}, but they return a
1169@code{long int} instead of a floating-point number.
1170@end deftypefun
1171
1172@comment math.h
1173@comment ISO
1174@deftypefun {long long int} llround (double @var{x})
4260bc74
UD
1175@comment math.h
1176@comment ISO
7a68c94a 1177@deftypefunx {long long int} llroundf (float @var{x})
4260bc74
UD
1178@comment math.h
1179@comment ISO
7a68c94a
UD
1180@deftypefunx {long long int} llroundl (long double @var{x})
1181These functions are just like @code{round}, but they return a
1182@code{long long int} instead of a floating-point number.
1183@end deftypefun
1184
1185
28f540f4 1186@comment math.h
f65fd747 1187@comment ISO
28f540f4 1188@deftypefun double modf (double @var{value}, double *@var{integer-part})
4260bc74
UD
1189@comment math.h
1190@comment ISO
f2ea0f5b 1191@deftypefunx float modff (float @var{value}, float *@var{integer-part})
4260bc74
UD
1192@comment math.h
1193@comment ISO
779ae82e 1194@deftypefunx {long double} modfl (long double @var{value}, long double *@var{integer-part})
b4012b75 1195These functions break the argument @var{value} into an integer part and a
28f540f4
RM
1196fractional part (between @code{-1} and @code{1}, exclusive). Their sum
1197equals @var{value}. Each of the parts has the same sign as @var{value},
7a68c94a 1198and the integer part is always rounded toward zero.
28f540f4
RM
1199
1200@code{modf} stores the integer part in @code{*@var{integer-part}}, and
1201returns the fractional part. For example, @code{modf (2.5, &intpart)}
1202returns @code{0.5} and stores @code{2.0} into @code{intpart}.
1203@end deftypefun
1204
7a68c94a
UD
1205@node Remainder Functions
1206@subsection Remainder Functions
1207
1208The functions in this section compute the remainder on division of two
1209floating-point numbers. Each is a little different; pick the one that
1210suits your problem.
1211
28f540f4 1212@comment math.h
f65fd747 1213@comment ISO
28f540f4 1214@deftypefun double fmod (double @var{numerator}, double @var{denominator})
4260bc74
UD
1215@comment math.h
1216@comment ISO
779ae82e 1217@deftypefunx float fmodf (float @var{numerator}, float @var{denominator})
4260bc74
UD
1218@comment math.h
1219@comment ISO
779ae82e 1220@deftypefunx {long double} fmodl (long double @var{numerator}, long double @var{denominator})
b4012b75 1221These functions compute the remainder from the division of
28f540f4
RM
1222@var{numerator} by @var{denominator}. Specifically, the return value is
1223@code{@var{numerator} - @w{@var{n} * @var{denominator}}}, where @var{n}
1224is the quotient of @var{numerator} divided by @var{denominator}, rounded
1225towards zero to an integer. Thus, @w{@code{fmod (6.5, 2.3)}} returns
1226@code{1.9}, which is @code{6.5} minus @code{4.6}.
1227
1228The result has the same sign as the @var{numerator} and has magnitude
1229less than the magnitude of the @var{denominator}.
1230
7a68c94a 1231If @var{denominator} is zero, @code{fmod} signals a domain error.
28f540f4
RM
1232@end deftypefun
1233
1234@comment math.h
1235@comment BSD
1236@deftypefun double drem (double @var{numerator}, double @var{denominator})
4260bc74
UD
1237@comment math.h
1238@comment BSD
779ae82e 1239@deftypefunx float dremf (float @var{numerator}, float @var{denominator})
4260bc74
UD
1240@comment math.h
1241@comment BSD
779ae82e 1242@deftypefunx {long double} dreml (long double @var{numerator}, long double @var{denominator})
7a68c94a 1243These functions are like @code{fmod} except that they rounds the
28f540f4
RM
1244internal quotient @var{n} to the nearest integer instead of towards zero
1245to an integer. For example, @code{drem (6.5, 2.3)} returns @code{-0.4},
1246which is @code{6.5} minus @code{6.9}.
1247
1248The absolute value of the result is less than or equal to half the
1249absolute value of the @var{denominator}. The difference between
1250@code{fmod (@var{numerator}, @var{denominator})} and @code{drem
1251(@var{numerator}, @var{denominator})} is always either
1252@var{denominator}, minus @var{denominator}, or zero.
1253
7a68c94a 1254If @var{denominator} is zero, @code{drem} signals a domain error.
28f540f4
RM
1255@end deftypefun
1256
7a68c94a
UD
1257@comment math.h
1258@comment BSD
1259@deftypefun double remainder (double @var{numerator}, double @var{denominator})
4260bc74
UD
1260@comment math.h
1261@comment BSD
7a68c94a 1262@deftypefunx float remainderf (float @var{numerator}, float @var{denominator})
4260bc74
UD
1263@comment math.h
1264@comment BSD
7a68c94a
UD
1265@deftypefunx {long double} remainderl (long double @var{numerator}, long double @var{denominator})
1266This function is another name for @code{drem}.
1267@end deftypefun
28f540f4 1268
7a68c94a
UD
1269@node FP Bit Twiddling
1270@subsection Setting and modifying single bits of FP values
fe0ec73e
UD
1271@cindex FP arithmetic
1272
7a68c94a
UD
1273There are some operations that are too complicated or expensive to
1274perform by hand on floating-point numbers. @w{ISO C 9x} defines
1275functions to do these operations, which mostly involve changing single
1276bits.
fe0ec73e
UD
1277
1278@comment math.h
1279@comment ISO
1280@deftypefun double copysign (double @var{x}, double @var{y})
4260bc74
UD
1281@comment math.h
1282@comment ISO
fe0ec73e 1283@deftypefunx float copysignf (float @var{x}, float @var{y})
4260bc74
UD
1284@comment math.h
1285@comment ISO
fe0ec73e 1286@deftypefunx {long double} copysignl (long double @var{x}, long double @var{y})
7a68c94a
UD
1287These functions return @var{x} but with the sign of @var{y}. They work
1288even if @var{x} or @var{y} are NaN or zero. Both of these can carry a
1289sign (although not all implementations support it) and this is one of
1290the few operations that can tell the difference.
fe0ec73e 1291
7a68c94a
UD
1292@code{copysign} never raises an exception.
1293@c except signalling NaNs
fe0ec73e
UD
1294
1295This function is defined in @w{IEC 559} (and the appendix with
1296recommended functions in @w{IEEE 754}/@w{IEEE 854}).
1297@end deftypefun
1298
1299@comment math.h
1300@comment ISO
1301@deftypefun int signbit (@emph{float-type} @var{x})
1302@code{signbit} is a generic macro which can work on all floating-point
1303types. It returns a nonzero value if the value of @var{x} has its sign
1304bit set.
1305
7a68c94a
UD
1306This is not the same as @code{x < 0.0}, because @w{IEEE 754} floating
1307point allows zero to be signed. The comparison @code{-0.0 < 0.0} is
1308false, but @code{signbit (-0.0)} will return a nonzero value.
fe0ec73e
UD
1309@end deftypefun
1310
1311@comment math.h
1312@comment ISO
1313@deftypefun double nextafter (double @var{x}, double @var{y})
4260bc74
UD
1314@comment math.h
1315@comment ISO
fe0ec73e 1316@deftypefunx float nextafterf (float @var{x}, float @var{y})
4260bc74
UD
1317@comment math.h
1318@comment ISO
fe0ec73e
UD
1319@deftypefunx {long double} nextafterl (long double @var{x}, long double @var{y})
1320The @code{nextafter} function returns the next representable neighbor of
7a68c94a
UD
1321@var{x} in the direction towards @var{y}. The size of the step between
1322@var{x} and the result depends on the type of the result. If
1323@math{@var{x} = @var{y}} the function simply returns @var{x}. If either
1324value is @code{NaN}, @code{NaN} is returned. Otherwise
1325a value corresponding to the value of the least significant bit in the
1326mantissa is added or subtracted, depending on the direction.
1327@code{nextafter} will signal overflow or underflow if the result goes
1328outside of the range of normalized numbers.
fe0ec73e
UD
1329
1330This function is defined in @w{IEC 559} (and the appendix with
1331recommended functions in @w{IEEE 754}/@w{IEEE 854}).
1332@end deftypefun
1333
7a68c94a
UD
1334@comment math.h
1335@comment ISO
36fe9ac9 1336@deftypefun double nexttoward (double @var{x}, long double @var{y})
4260bc74
UD
1337@comment math.h
1338@comment ISO
36fe9ac9 1339@deftypefunx float nexttowardf (float @var{x}, long double @var{y})
4260bc74
UD
1340@comment math.h
1341@comment ISO
36fe9ac9 1342@deftypefunx {long double} nexttowardl (long double @var{x}, long double @var{y})
7a68c94a
UD
1343These functions are identical to the corresponding versions of
1344@code{nextafter} except that their second argument is a @code{long
1345double}.
1346@end deftypefun
1347
fe0ec73e
UD
1348@cindex NaN
1349@comment math.h
1350@comment ISO
1351@deftypefun double nan (const char *@var{tagp})
4260bc74
UD
1352@comment math.h
1353@comment ISO
fe0ec73e 1354@deftypefunx float nanf (const char *@var{tagp})
4260bc74
UD
1355@comment math.h
1356@comment ISO
fe0ec73e 1357@deftypefunx {long double} nanl (const char *@var{tagp})
7a68c94a
UD
1358The @code{nan} function returns a representation of NaN, provided that
1359NaN is supported by the target platform.
1360@code{nan ("@var{n-char-sequence}")} is equivalent to
1361@code{strtod ("NAN(@var{n-char-sequence})")}.
1362
1363The argument @var{tagp} is used in an unspecified manner. On @w{IEEE
1364754} systems, there are many representations of NaN, and @var{tagp}
1365selects one. On other systems it may do nothing.
fe0ec73e
UD
1366@end deftypefun
1367
7a68c94a
UD
1368@node FP Comparison Functions
1369@subsection Floating-Point Comparison Functions
1370@cindex unordered comparison
fe0ec73e 1371
7a68c94a
UD
1372The standard C comparison operators provoke exceptions when one or other
1373of the operands is NaN. For example,
1374
1375@smallexample
1376int v = a < 1.0;
1377@end smallexample
1378
1379@noindent
1380will raise an exception if @var{a} is NaN. (This does @emph{not}
1381happen with @code{==} and @code{!=}; those merely return false and true,
1382respectively, when NaN is examined.) Frequently this exception is
1383undesirable. @w{ISO C 9x} therefore defines comparison functions that
1384do not raise exceptions when NaN is examined. All of the functions are
1385implemented as macros which allow their arguments to be of any
1386floating-point type. The macros are guaranteed to evaluate their
1387arguments only once.
1388
1389@comment math.h
1390@comment ISO
1391@deftypefn Macro int isgreater (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
1392This macro determines whether the argument @var{x} is greater than
1393@var{y}. It is equivalent to @code{(@var{x}) > (@var{y})}, but no
1394exception is raised if @var{x} or @var{y} are NaN.
1395@end deftypefn
1396
1397@comment math.h
1398@comment ISO
1399@deftypefn Macro int isgreaterequal (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
1400This macro determines whether the argument @var{x} is greater than or
1401equal to @var{y}. It is equivalent to @code{(@var{x}) >= (@var{y})}, but no
1402exception is raised if @var{x} or @var{y} are NaN.
1403@end deftypefn
1404
1405@comment math.h
1406@comment ISO
1407@deftypefn Macro int isless (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
1408This macro determines whether the argument @var{x} is less than @var{y}.
1409It is equivalent to @code{(@var{x}) < (@var{y})}, but no exception is
1410raised if @var{x} or @var{y} are NaN.
1411@end deftypefn
1412
1413@comment math.h
1414@comment ISO
1415@deftypefn Macro int islessequal (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
1416This macro determines whether the argument @var{x} is less than or equal
1417to @var{y}. It is equivalent to @code{(@var{x}) <= (@var{y})}, but no
1418exception is raised if @var{x} or @var{y} are NaN.
1419@end deftypefn
1420
1421@comment math.h
1422@comment ISO
1423@deftypefn Macro int islessgreater (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
1424This macro determines whether the argument @var{x} is less or greater
1425than @var{y}. It is equivalent to @code{(@var{x}) < (@var{y}) ||
1426(@var{x}) > (@var{y})} (although it only evaluates @var{x} and @var{y}
1427once), but no exception is raised if @var{x} or @var{y} are NaN.
1428
1429This macro is not equivalent to @code{@var{x} != @var{y}}, because that
1430expression is true if @var{x} or @var{y} are NaN.
1431@end deftypefn
1432
1433@comment math.h
1434@comment ISO
1435@deftypefn Macro int isunordered (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
1436This macro determines whether its arguments are unordered. In other
1437words, it is true if @var{x} or @var{y} are NaN, and false otherwise.
1438@end deftypefn
1439
1440Not all machines provide hardware support for these operations. On
1441machines that don't, the macros can be very slow. Therefore, you should
1442not use these functions when NaN is not a concern.
1443
1444@strong{Note:} There are no macros @code{isequal} or @code{isunequal}.
1445They are unnecessary, because the @code{==} and @code{!=} operators do
1446@emph{not} throw an exception if one or both of the operands are NaN.
1447
1448@node Misc FP Arithmetic
1449@subsection Miscellaneous FP arithmetic functions
fe0ec73e
UD
1450@cindex minimum
1451@cindex maximum
7a68c94a
UD
1452@cindex positive difference
1453@cindex multiply-add
fe0ec73e 1454
7a68c94a
UD
1455The functions in this section perform miscellaneous but common
1456operations that are awkward to express with C operators. On some
1457processors these functions can use special machine instructions to
1458perform these operations faster than the equivalent C code.
fe0ec73e
UD
1459
1460@comment math.h
1461@comment ISO
1462@deftypefun double fmin (double @var{x}, double @var{y})
4260bc74
UD
1463@comment math.h
1464@comment ISO
fe0ec73e 1465@deftypefunx float fminf (float @var{x}, float @var{y})
4260bc74
UD
1466@comment math.h
1467@comment ISO
fe0ec73e 1468@deftypefunx {long double} fminl (long double @var{x}, long double @var{y})
7a68c94a
UD
1469The @code{fmin} function returns the lesser of the two values @var{x}
1470and @var{y}. It is similar to the expression
1471@smallexample
1472((x) < (y) ? (x) : (y))
1473@end smallexample
1474except that @var{x} and @var{y} are only evaluated once.
fe0ec73e 1475
7a68c94a
UD
1476If an argument is NaN, the other argument is returned. If both arguments
1477are NaN, NaN is returned.
fe0ec73e
UD
1478@end deftypefun
1479
1480@comment math.h
1481@comment ISO
1482@deftypefun double fmax (double @var{x}, double @var{y})
4260bc74
UD
1483@comment math.h
1484@comment ISO
fe0ec73e 1485@deftypefunx float fmaxf (float @var{x}, float @var{y})
4260bc74
UD
1486@comment math.h
1487@comment ISO
fe0ec73e 1488@deftypefunx {long double} fmaxl (long double @var{x}, long double @var{y})
7a68c94a
UD
1489The @code{fmax} function returns the greater of the two values @var{x}
1490and @var{y}.
fe0ec73e 1491
7a68c94a
UD
1492If an argument is NaN, the other argument is returned. If both arguments
1493are NaN, NaN is returned.
fe0ec73e
UD
1494@end deftypefun
1495
1496@comment math.h
1497@comment ISO
1498@deftypefun double fdim (double @var{x}, double @var{y})
4260bc74
UD
1499@comment math.h
1500@comment ISO
fe0ec73e 1501@deftypefunx float fdimf (float @var{x}, float @var{y})
4260bc74
UD
1502@comment math.h
1503@comment ISO
fe0ec73e 1504@deftypefunx {long double} fdiml (long double @var{x}, long double @var{y})
7a68c94a
UD
1505The @code{fdim} function returns the positive difference between
1506@var{x} and @var{y}. The positive difference is @math{@var{x} -
1507@var{y}} if @var{x} is greater than @var{y}, and @math{0} otherwise.
fe0ec73e 1508
7a68c94a 1509If @var{x}, @var{y}, or both are NaN, NaN is returned.
fe0ec73e
UD
1510@end deftypefun
1511
1512@comment math.h
1513@comment ISO
1514@deftypefun double fma (double @var{x}, double @var{y}, double @var{z})
4260bc74
UD
1515@comment math.h
1516@comment ISO
fe0ec73e 1517@deftypefunx float fmaf (float @var{x}, float @var{y}, float @var{z})
4260bc74
UD
1518@comment math.h
1519@comment ISO
fe0ec73e
UD
1520@deftypefunx {long double} fmal (long double @var{x}, long double @var{y}, long double @var{z})
1521@cindex butterfly
7a68c94a
UD
1522The @code{fma} function performs floating-point multiply-add. This is
1523the operation @math{(@var{x} @mul{} @var{y}) + @var{z}}, but the
1524intermediate result is not rounded to the destination type. This can
1525sometimes improve the precision of a calculation.
1526
1527This function was introduced because some processors have a special
1528instruction to perform multiply-add. The C compiler cannot use it
1529directly, because the expression @samp{x*y + z} is defined to round the
1530intermediate result. @code{fma} lets you choose when you want to round
1531only once.
fe0ec73e
UD
1532
1533@vindex FP_FAST_FMA
7a68c94a
UD
1534On processors which do not implement multiply-add in hardware,
1535@code{fma} can be very slow since it must avoid intermediate rounding.
1536@file{math.h} defines the symbols @code{FP_FAST_FMA},
1537@code{FP_FAST_FMAF}, and @code{FP_FAST_FMAL} when the corresponding
1538version of @code{fma} is no slower than the expression @samp{x*y + z}.
1539In the GNU C library, this always means the operation is implemented in
1540hardware.
fe0ec73e
UD
1541@end deftypefun
1542
7a68c94a
UD
1543@node Complex Numbers
1544@section Complex Numbers
1545@pindex complex.h
1546@cindex complex numbers
1547
1548@w{ISO C 9x} introduces support for complex numbers in C. This is done
1549with a new type qualifier, @code{complex}. It is a keyword if and only
1550if @file{complex.h} has been included. There are three complex types,
1551corresponding to the three real types: @code{float complex},
1552@code{double complex}, and @code{long double complex}.
1553
1554To construct complex numbers you need a way to indicate the imaginary
1555part of a number. There is no standard notation for an imaginary
1556floating point constant. Instead, @file{complex.h} defines two macros
1557that can be used to create complex numbers.
1558
1559@deftypevr Macro {const float complex} _Complex_I
1560This macro is a representation of the complex number ``@math{0+1i}''.
1561Multiplying a real floating-point value by @code{_Complex_I} gives a
1562complex number whose value is purely imaginary. You can use this to
1563construct complex constants:
1564
1565@smallexample
1566@math{3.0 + 4.0i} = @code{3.0 + 4.0 * _Complex_I}
1567@end smallexample
1568
1569Note that @code{_Complex_I * _Complex_I} has the value @code{-1}, but
1570the type of that value is @code{complex}.
1571@end deftypevr
1572
1573@c Put this back in when gcc supports _Imaginary_I. It's too confusing.
1574@ignore
1575@noindent
1576Without an optimizing compiler this is more expensive than the use of
1577@code{_Imaginary_I} but with is better than nothing. You can avoid all
1578the hassles if you use the @code{I} macro below if the name is not
1579problem.
1580
1581@deftypevr Macro {const float imaginary} _Imaginary_I
1582This macro is a representation of the value ``@math{1i}''. I.e., it is
1583the value for which
1584
1585@smallexample
1586_Imaginary_I * _Imaginary_I = -1
1587@end smallexample
1588
1589@noindent
1590The result is not of type @code{float imaginary} but instead @code{float}.
1591One can use it to easily construct complex number like in
1592
1593@smallexample
15943.0 - _Imaginary_I * 4.0
1595@end smallexample
1596
1597@noindent
1598which results in the complex number with a real part of 3.0 and a
1599imaginary part -4.0.
1600@end deftypevr
1601@end ignore
1602
1603@noindent
1604@code{_Complex_I} is a bit of a mouthful. @file{complex.h} also defines
1605a shorter name for the same constant.
1606
1607@deftypevr Macro {const float complex} I
1608This macro has exactly the same value as @code{_Complex_I}. Most of the
1609time it is preferable. However, it causes problems if you want to use
1610the identifier @code{I} for something else. You can safely write
1611
1612@smallexample
1613#include <complex.h>
1614#undef I
1615@end smallexample
1616
1617@noindent
1618if you need @code{I} for your own purposes. (In that case we recommend
1619you also define some other short name for @code{_Complex_I}, such as
1620@code{J}.)
1621
1622@ignore
1623If the implementation does not support the @code{imaginary} types
1624@code{I} is defined as @code{_Complex_I} which is the second best
1625solution. It still can be used in the same way but requires a most
1626clever compiler to get the same results.
1627@end ignore
1628@end deftypevr
1629
1630@node Operations on Complex
1631@section Projections, Conjugates, and Decomposing of Complex Numbers
1632@cindex project complex numbers
1633@cindex conjugate complex numbers
1634@cindex decompose complex numbers
1635@pindex complex.h
1636
1637@w{ISO C 9x} also defines functions that perform basic operations on
1638complex numbers, such as decomposition and conjugation. The prototypes
1639for all these functions are in @file{complex.h}. All functions are
1640available in three variants, one for each of the three complex types.
1641
1642@comment complex.h
1643@comment ISO
1644@deftypefun double creal (complex double @var{z})
4260bc74
UD
1645@comment complex.h
1646@comment ISO
7a68c94a 1647@deftypefunx float crealf (complex float @var{z})
4260bc74
UD
1648@comment complex.h
1649@comment ISO
7a68c94a
UD
1650@deftypefunx {long double} creall (complex long double @var{z})
1651These functions return the real part of the complex number @var{z}.
1652@end deftypefun
1653
1654@comment complex.h
1655@comment ISO
1656@deftypefun double cimag (complex double @var{z})
4260bc74
UD
1657@comment complex.h
1658@comment ISO
7a68c94a 1659@deftypefunx float cimagf (complex float @var{z})
4260bc74
UD
1660@comment complex.h
1661@comment ISO
7a68c94a
UD
1662@deftypefunx {long double} cimagl (complex long double @var{z})
1663These functions return the imaginary part of the complex number @var{z}.
1664@end deftypefun
1665
1666@comment complex.h
1667@comment ISO
1668@deftypefun {complex double} conj (complex double @var{z})
4260bc74
UD
1669@comment complex.h
1670@comment ISO
7a68c94a 1671@deftypefunx {complex float} conjf (complex float @var{z})
4260bc74
UD
1672@comment complex.h
1673@comment ISO
7a68c94a
UD
1674@deftypefunx {complex long double} conjl (complex long double @var{z})
1675These functions return the conjugate value of the complex number
1676@var{z}. The conjugate of a complex number has the same real part and a
1677negated imaginary part. In other words, @samp{conj(a + bi) = a + -bi}.
1678@end deftypefun
1679
1680@comment complex.h
1681@comment ISO
1682@deftypefun double carg (complex double @var{z})
4260bc74
UD
1683@comment complex.h
1684@comment ISO
7a68c94a 1685@deftypefunx float cargf (complex float @var{z})
4260bc74
UD
1686@comment complex.h
1687@comment ISO
7a68c94a
UD
1688@deftypefunx {long double} cargl (complex long double @var{z})
1689These functions return the argument of the complex number @var{z}.
1690The argument of a complex number is the angle in the complex plane
1691between the positive real axis and a line passing through zero and the
1692number. This angle is measured in the usual fashion and ranges from @math{0}
1693to @math{2@pi{}}.
1694
1695@code{carg} has a branch cut along the positive real axis.
1696@end deftypefun
1697
1698@comment complex.h
1699@comment ISO
1700@deftypefun {complex double} cproj (complex double @var{z})
4260bc74
UD
1701@comment complex.h
1702@comment ISO
7a68c94a 1703@deftypefunx {complex float} cprojf (complex float @var{z})
4260bc74
UD
1704@comment complex.h
1705@comment ISO
7a68c94a
UD
1706@deftypefunx {complex long double} cprojl (complex long double @var{z})
1707These functions return the projection of the complex value @var{z} onto
1708the Riemann sphere. Values with a infinite imaginary part are projected
1709to positive infinity on the real axis, even if the real part is NaN. If
1710the real part is infinite, the result is equivalent to
1711
1712@smallexample
1713INFINITY + I * copysign (0.0, cimag (z))
1714@end smallexample
1715@end deftypefun
fe0ec73e 1716
28f540f4
RM
1717@node Integer Division
1718@section Integer Division
1719@cindex integer division functions
1720
1721This section describes functions for performing integer division. These
7a68c94a
UD
1722functions are redundant when GNU CC is used, because in GNU C the
1723@samp{/} operator always rounds towards zero. But in other C
1724implementations, @samp{/} may round differently with negative arguments.
1725@code{div} and @code{ldiv} are useful because they specify how to round
1726the quotient: towards zero. The remainder has the same sign as the
1727numerator.
28f540f4
RM
1728
1729These functions are specified to return a result @var{r} such that the value
1730@code{@var{r}.quot*@var{denominator} + @var{r}.rem} equals
1731@var{numerator}.
1732
1733@pindex stdlib.h
1734To use these facilities, you should include the header file
1735@file{stdlib.h} in your program.
1736
1737@comment stdlib.h
f65fd747 1738@comment ISO
28f540f4
RM
1739@deftp {Data Type} div_t
1740This is a structure type used to hold the result returned by the @code{div}
1741function. It has the following members:
1742
1743@table @code
1744@item int quot
1745The quotient from the division.
1746
1747@item int rem
1748The remainder from the division.
1749@end table
1750@end deftp
1751
1752@comment stdlib.h
f65fd747 1753@comment ISO
28f540f4
RM
1754@deftypefun div_t div (int @var{numerator}, int @var{denominator})
1755This function @code{div} computes the quotient and remainder from
1756the division of @var{numerator} by @var{denominator}, returning the
1757result in a structure of type @code{div_t}.
1758
1759If the result cannot be represented (as in a division by zero), the
1760behavior is undefined.
1761
1762Here is an example, albeit not a very useful one.
1763
1764@smallexample
1765div_t result;
1766result = div (20, -6);
1767@end smallexample
1768
1769@noindent
1770Now @code{result.quot} is @code{-3} and @code{result.rem} is @code{2}.
1771@end deftypefun
1772
1773@comment stdlib.h
f65fd747 1774@comment ISO
28f540f4
RM
1775@deftp {Data Type} ldiv_t
1776This is a structure type used to hold the result returned by the @code{ldiv}
1777function. It has the following members:
1778
1779@table @code
1780@item long int quot
1781The quotient from the division.
1782
1783@item long int rem
1784The remainder from the division.
1785@end table
1786
1787(This is identical to @code{div_t} except that the components are of
1788type @code{long int} rather than @code{int}.)
1789@end deftp
1790
1791@comment stdlib.h
f65fd747 1792@comment ISO
28f540f4
RM
1793@deftypefun ldiv_t ldiv (long int @var{numerator}, long int @var{denominator})
1794The @code{ldiv} function is similar to @code{div}, except that the
1795arguments are of type @code{long int} and the result is returned as a
fe7bdd63
UD
1796structure of type @code{ldiv_t}.
1797@end deftypefun
1798
1799@comment stdlib.h
7a68c94a 1800@comment ISO
fe7bdd63
UD
1801@deftp {Data Type} lldiv_t
1802This is a structure type used to hold the result returned by the @code{lldiv}
1803function. It has the following members:
1804
1805@table @code
1806@item long long int quot
1807The quotient from the division.
1808
1809@item long long int rem
1810The remainder from the division.
1811@end table
1812
1813(This is identical to @code{div_t} except that the components are of
1814type @code{long long int} rather than @code{int}.)
1815@end deftp
1816
1817@comment stdlib.h
7a68c94a 1818@comment ISO
fe7bdd63
UD
1819@deftypefun lldiv_t lldiv (long long int @var{numerator}, long long int @var{denominator})
1820The @code{lldiv} function is like the @code{div} function, but the
1821arguments are of type @code{long long int} and the result is returned as
1822a structure of type @code{lldiv_t}.
1823
7a68c94a 1824The @code{lldiv} function was added in @w{ISO C 9x}.
28f540f4
RM
1825@end deftypefun
1826
e518937a
UD
1827@comment inttypes.h
1828@comment ISO
1829@deftp {Data Type} imaxdiv_t
1830This is a structure type used to hold the result returned by the @code{imaxdiv}
1831function. It has the following members:
1832
1833@table @code
1834@item intmax_t quot
1835The quotient from the division.
1836
1837@item intmax_t rem
1838The remainder from the division.
1839@end table
1840
1841(This is identical to @code{div_t} except that the components are of
1842type @code{intmax_t} rather than @code{int}.)
1843@end deftp
1844
1845@comment inttypes.h
1846@comment ISO
1847@deftypefun imaxdiv_t imaxdiv (intmax_t @var{numerator}, intmax_t @var{denominator})
1848The @code{imaxdiv} function is like the @code{div} function, but the
1849arguments are of type @code{intmax_t} and the result is returned as
1850a structure of type @code{imaxdiv_t}.
1851
1852The @code{imaxdiv} function was added in @w{ISO C 9x}.
1853@end deftypefun
1854
28f540f4
RM
1855
1856@node Parsing of Numbers
1857@section Parsing of Numbers
1858@cindex parsing numbers (in formatted input)
1859@cindex converting strings to numbers
1860@cindex number syntax, parsing
1861@cindex syntax, for reading numbers
1862
1863This section describes functions for ``reading'' integer and
1864floating-point numbers from a string. It may be more convenient in some
1865cases to use @code{sscanf} or one of the related functions; see
1866@ref{Formatted Input}. But often you can make a program more robust by
1867finding the tokens in the string by hand, then converting the numbers
1868one by one.
1869
1870@menu
1871* Parsing of Integers:: Functions for conversion of integer values.
1872* Parsing of Floats:: Functions for conversion of floating-point
1873 values.
1874@end menu
1875
1876@node Parsing of Integers
1877@subsection Parsing of Integers
1878
1879@pindex stdlib.h
1880These functions are declared in @file{stdlib.h}.
1881
1882@comment stdlib.h
f65fd747 1883@comment ISO
28f540f4
RM
1884@deftypefun {long int} strtol (const char *@var{string}, char **@var{tailptr}, int @var{base})
1885The @code{strtol} (``string-to-long'') function converts the initial
1886part of @var{string} to a signed integer, which is returned as a value
b8fe19fa 1887of type @code{long int}.
28f540f4
RM
1888
1889This function attempts to decompose @var{string} as follows:
1890
1891@itemize @bullet
b8fe19fa 1892@item
28f540f4
RM
1893A (possibly empty) sequence of whitespace characters. Which characters
1894are whitespace is determined by the @code{isspace} function
1895(@pxref{Classification of Characters}). These are discarded.
1896
b8fe19fa 1897@item
28f540f4
RM
1898An optional plus or minus sign (@samp{+} or @samp{-}).
1899
b8fe19fa 1900@item
28f540f4
RM
1901A nonempty sequence of digits in the radix specified by @var{base}.
1902
1903If @var{base} is zero, decimal radix is assumed unless the series of
1904digits begins with @samp{0} (specifying octal radix), or @samp{0x} or
1905@samp{0X} (specifying hexadecimal radix); in other words, the same
1906syntax used for integer constants in C.
1907
1908Otherwise @var{base} must have a value between @code{2} and @code{35}.
1909If @var{base} is @code{16}, the digits may optionally be preceded by
2c6fe0bd
UD
1910@samp{0x} or @samp{0X}. If base has no legal value the value returned
1911is @code{0l} and the global variable @code{errno} is set to @code{EINVAL}.
28f540f4 1912
b8fe19fa 1913@item
28f540f4
RM
1914Any remaining characters in the string. If @var{tailptr} is not a null
1915pointer, @code{strtol} stores a pointer to this tail in
1916@code{*@var{tailptr}}.
1917@end itemize
1918
1919If the string is empty, contains only whitespace, or does not contain an
1920initial substring that has the expected syntax for an integer in the
1921specified @var{base}, no conversion is performed. In this case,
1922@code{strtol} returns a value of zero and the value stored in
1923@code{*@var{tailptr}} is the value of @var{string}.
1924
1925In a locale other than the standard @code{"C"} locale, this function
1926may recognize additional implementation-dependent syntax.
1927
1928If the string has valid syntax for an integer but the value is not
1929representable because of overflow, @code{strtol} returns either
1930@code{LONG_MAX} or @code{LONG_MIN} (@pxref{Range of Type}), as
1931appropriate for the sign of the value. It also sets @code{errno}
1932to @code{ERANGE} to indicate there was overflow.
1933
7a68c94a
UD
1934You should not check for errors by examining the return value of
1935@code{strtol}, because the string might be a valid representation of
1936@code{0l}, @code{LONG_MAX}, or @code{LONG_MIN}. Instead, check whether
1937@var{tailptr} points to what you expect after the number
1938(e.g. @code{'\0'} if the string should end after the number). You also
1939need to clear @var{errno} before the call and check it afterward, in
1940case there was overflow.
2c6fe0bd 1941
28f540f4
RM
1942There is an example at the end of this section.
1943@end deftypefun
1944
1945@comment stdlib.h
f65fd747 1946@comment ISO
28f540f4
RM
1947@deftypefun {unsigned long int} strtoul (const char *@var{string}, char **@var{tailptr}, int @var{base})
1948The @code{strtoul} (``string-to-unsigned-long'') function is like
7a68c94a
UD
1949@code{strtol} except it returns an @code{unsigned long int} value. If
1950the number has a leading @samp{-} sign, the return value is negated.
1951The syntax is the same as described above for @code{strtol}. The value
1952returned on overflow is @code{ULONG_MAX} (@pxref{Range of
1953Type}).
1954
1955@code{strtoul} sets @var{errno} to @code{EINVAL} if @var{base} is out of
1956range, or @code{ERANGE} on overflow.
2c6fe0bd
UD
1957@end deftypefun
1958
1959@comment stdlib.h
7a68c94a 1960@comment ISO
fe7bdd63 1961@deftypefun {long long int} strtoll (const char *@var{string}, char **@var{tailptr}, int @var{base})
7a68c94a
UD
1962The @code{strtoll} function is like @code{strtol} except that it returns
1963a @code{long long int} value, and accepts numbers with a correspondingly
1964larger range.
2c6fe0bd
UD
1965
1966If the string has valid syntax for an integer but the value is not
fe7bdd63 1967representable because of overflow, @code{strtoll} returns either
2c6fe0bd
UD
1968@code{LONG_LONG_MAX} or @code{LONG_LONG_MIN} (@pxref{Range of Type}), as
1969appropriate for the sign of the value. It also sets @code{errno} to
1970@code{ERANGE} to indicate there was overflow.
2c6fe0bd 1971
7a68c94a 1972The @code{strtoll} function was introduced in @w{ISO C 9x}.
2c6fe0bd
UD
1973@end deftypefun
1974
1975@comment stdlib.h
1976@comment BSD
fe7bdd63 1977@deftypefun {long long int} strtoq (const char *@var{string}, char **@var{tailptr}, int @var{base})
7a68c94a 1978@code{strtoq} (``string-to-quad-word'') is the BSD name for @code{strtoll}.
2c6fe0bd
UD
1979@end deftypefun
1980
1981@comment stdlib.h
7a68c94a 1982@comment ISO
2c6fe0bd 1983@deftypefun {unsigned long long int} strtoull (const char *@var{string}, char **@var{tailptr}, int @var{base})
7a68c94a
UD
1984The @code{strtoull} function is like @code{strtoul} except that it
1985returns an @code{unsigned long long int}. The value returned on overflow
fe7bdd63
UD
1986is @code{ULONG_LONG_MAX} (@pxref{Range of Type}).
1987
7a68c94a 1988The @code{strtoull} function was introduced in @w{ISO C 9x}.
fe7bdd63
UD
1989@end deftypefun
1990
1991@comment stdlib.h
1992@comment BSD
1993@deftypefun {unsigned long long int} strtouq (const char *@var{string}, char **@var{tailptr}, int @var{base})
7a68c94a 1994@code{strtouq} is the BSD name for @code{strtoull}.
28f540f4
RM
1995@end deftypefun
1996
1997@comment stdlib.h
f65fd747 1998@comment ISO
28f540f4
RM
1999@deftypefun {long int} atol (const char *@var{string})
2000This function is similar to the @code{strtol} function with a @var{base}
2001argument of @code{10}, except that it need not detect overflow errors.
2002The @code{atol} function is provided mostly for compatibility with
2003existing code; using @code{strtol} is more robust.
2004@end deftypefun
2005
2006@comment stdlib.h
f65fd747 2007@comment ISO
28f540f4 2008@deftypefun int atoi (const char *@var{string})
7a68c94a
UD
2009This function is like @code{atol}, except that it returns an @code{int}.
2010The @code{atoi} function is also considered obsolete; use @code{strtol}
2011instead.
28f540f4
RM
2012@end deftypefun
2013
fe7bdd63 2014@comment stdlib.h
7a68c94a 2015@comment ISO
fe7bdd63
UD
2016@deftypefun {long long int} atoll (const char *@var{string})
2017This function is similar to @code{atol}, except it returns a @code{long
7a68c94a 2018long int}.
fe7bdd63 2019
7a68c94a
UD
2020The @code{atoll} function was introduced in @w{ISO C 9x}. It too is
2021obsolete (despite having just been added); use @code{strtoll} instead.
fe7bdd63
UD
2022@end deftypefun
2023
7a68c94a
UD
2024@c !!! please fact check this paragraph -zw
2025@findex strtol_l
2026@findex strtoul_l
2027@findex strtoll_l
2028@findex strtoull_l
2029@cindex parsing numbers and locales
2030@cindex locales, parsing numbers and
2031Some locales specify a printed syntax for numbers other than the one
2032that these functions understand. If you need to read numbers formatted
2033in some other locale, you can use the @code{strtoX_l} functions. Each
2034of the @code{strtoX} functions has a counterpart with @samp{_l} added to
2035its name. The @samp{_l} counterparts take an additional argument: a
2036pointer to an @code{locale_t} structure, which describes how the numbers
2037to be read are formatted. @xref{Locales}.
2038
2039@strong{Portability Note:} These functions are all GNU extensions. You
2040can also use @code{scanf} or its relatives, which have the @samp{'} flag
2041for parsing numeric input according to the current locale
2042(@pxref{Numeric Input Conversions}). This feature is standard.
2c6fe0bd 2043
28f540f4
RM
2044Here is a function which parses a string as a sequence of integers and
2045returns the sum of them:
2046
2047@smallexample
2048int
2049sum_ints_from_string (char *string)
2050@{
2051 int sum = 0;
2052
2053 while (1) @{
2054 char *tail;
2055 int next;
2056
2057 /* @r{Skip whitespace by hand, to detect the end.} */
2058 while (isspace (*string)) string++;
2059 if (*string == 0)
2060 break;
2061
2062 /* @r{There is more nonwhitespace,} */
2063 /* @r{so it ought to be another number.} */
2064 errno = 0;
2065 /* @r{Parse it.} */
2066 next = strtol (string, &tail, 0);
2067 /* @r{Add it in, if not overflow.} */
2068 if (errno)
2069 printf ("Overflow\n");
2070 else
2071 sum += next;
2072 /* @r{Advance past it.} */
2073 string = tail;
2074 @}
2075
2076 return sum;
2077@}
2078@end smallexample
2079
2080@node Parsing of Floats
2081@subsection Parsing of Floats
2082
2083@pindex stdlib.h
2084These functions are declared in @file{stdlib.h}.
2085
2086@comment stdlib.h
f65fd747 2087@comment ISO
28f540f4
RM
2088@deftypefun double strtod (const char *@var{string}, char **@var{tailptr})
2089The @code{strtod} (``string-to-double'') function converts the initial
2090part of @var{string} to a floating-point number, which is returned as a
b8fe19fa 2091value of type @code{double}.
28f540f4
RM
2092
2093This function attempts to decompose @var{string} as follows:
2094
2095@itemize @bullet
b8fe19fa 2096@item
28f540f4
RM
2097A (possibly empty) sequence of whitespace characters. Which characters
2098are whitespace is determined by the @code{isspace} function
2099(@pxref{Classification of Characters}). These are discarded.
2100
2101@item
2102An optional plus or minus sign (@samp{+} or @samp{-}).
2103
2104@item
2105A nonempty sequence of digits optionally containing a decimal-point
2106character---normally @samp{.}, but it depends on the locale
85c165be 2107(@pxref{General Numeric}).
28f540f4
RM
2108
2109@item
2110An optional exponent part, consisting of a character @samp{e} or
2111@samp{E}, an optional sign, and a sequence of digits.
2112
2113@item
2114Any remaining characters in the string. If @var{tailptr} is not a null
2115pointer, a pointer to this tail of the string is stored in
2116@code{*@var{tailptr}}.
2117@end itemize
2118
2119If the string is empty, contains only whitespace, or does not contain an
2120initial substring that has the expected syntax for a floating-point
2121number, no conversion is performed. In this case, @code{strtod} returns
2122a value of zero and the value returned in @code{*@var{tailptr}} is the
2123value of @var{string}.
2124
26761c28 2125In a locale other than the standard @code{"C"} or @code{"POSIX"} locales,
2c6fe0bd 2126this function may recognize additional locale-dependent syntax.
28f540f4
RM
2127
2128If the string has valid syntax for a floating-point number but the value
7a68c94a
UD
2129is outside the range of a @code{double}, @code{strtod} will signal
2130overflow or underflow as described in @ref{Math Error Reporting}.
2131
2132@code{strtod} recognizes four special input strings. The strings
2133@code{"inf"} and @code{"infinity"} are converted to @math{@infinity{}},
2134or to the largest representable value if the floating-point format
2135doesn't support infinities. You can prepend a @code{"+"} or @code{"-"}
2136to specify the sign. Case is ignored when scanning these strings.
2137
2138The strings @code{"nan"} and @code{"nan(@var{chars...})"} are converted
2139to NaN. Again, case is ignored. If @var{chars...} are provided, they
2140are used in some unspecified fashion to select a particular
2141representation of NaN (there can be several).
2142
2143Since zero is a valid result as well as the value returned on error, you
2144should check for errors in the same way as for @code{strtol}, by
2145examining @var{errno} and @var{tailptr}.
28f540f4
RM
2146@end deftypefun
2147
2c6fe0bd
UD
2148@comment stdlib.h
2149@comment GNU
2150@deftypefun float strtof (const char *@var{string}, char **@var{tailptr})
4260bc74
UD
2151@comment stdlib.h
2152@comment GNU
7a68c94a
UD
2153@deftypefunx {long double} strtold (const char *@var{string}, char **@var{tailptr})
2154These functions are analogous to @code{strtod}, but return @code{float}
2155and @code{long double} values respectively. They report errors in the
2156same way as @code{strtod}. @code{strtof} can be substantially faster
2157than @code{strtod}, but has less precision; conversely, @code{strtold}
2158can be much slower but has more precision (on systems where @code{long
2159double} is a separate type).
2160
2161These functions are GNU extensions.
2c6fe0bd
UD
2162@end deftypefun
2163
28f540f4 2164@comment stdlib.h
f65fd747 2165@comment ISO
28f540f4
RM
2166@deftypefun double atof (const char *@var{string})
2167This function is similar to the @code{strtod} function, except that it
2168need not detect overflow and underflow errors. The @code{atof} function
2169is provided mostly for compatibility with existing code; using
2170@code{strtod} is more robust.
2171@end deftypefun
880f421f 2172
7a68c94a
UD
2173The GNU C library also provides @samp{_l} versions of thse functions,
2174which take an additional argument, the locale to use in conversion.
2175@xref{Parsing of Integers}.
880f421f 2176
7a68c94a
UD
2177@node System V Number Conversion
2178@section Old-fashioned System V number-to-string functions
880f421f 2179
7a68c94a
UD
2180The old @w{System V} C library provided three functions to convert
2181numbers to strings, with unusual and hard-to-use semantics. The GNU C
2182library also provides these functions and some natural extensions.
880f421f 2183
7a68c94a
UD
2184These functions are only available in glibc and on systems descended
2185from AT&T Unix. Therefore, unless these functions do precisely what you
2186need, it is better to use @code{sprintf}, which is standard.
880f421f 2187
7a68c94a 2188All these functions are defined in @file{stdlib.h}.
880f421f
UD
2189
2190@comment stdlib.h
2191@comment SVID, Unix98
7a68c94a 2192@deftypefun {char *} ecvt (double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg})
880f421f 2193The function @code{ecvt} converts the floating-point number @var{value}
7a68c94a
UD
2194to a string with at most @var{ndigit} decimal digits.
2195The returned string contains no decimal point or sign. The first
880f421f 2196digit of the string is non-zero (unless @var{value} is actually zero)
7a68c94a
UD
2197and the last digit is rounded to nearest. @var{decpt} is set to the
2198index in the string of the first digit after the decimal point.
2199@var{neg} is set to a nonzero value if @var{value} is negative, zero
2200otherwise.
880f421f
UD
2201
2202The returned string is statically allocated and overwritten by each call
2203to @code{ecvt}.
2204
7a68c94a 2205If @var{value} is zero, it's implementation defined whether @var{decpt} is
880f421f
UD
2206@code{0} or @code{1}.
2207
7a68c94a
UD
2208For example: @code{ecvt (12.3, 5, &decpt, &neg)} returns @code{"12300"}
2209and sets @var{decpt} to @code{2} and @var{neg} to @code{0}.
880f421f
UD
2210@end deftypefun
2211
880f421f
UD
2212@comment stdlib.h
2213@comment SVID, Unix98
7a68c94a
UD
2214@deftypefun {char *} fcvt (double @var{value}, int @var{ndigit}, int @var{decpt}, int *@var{neg})
2215The function @code{fcvt} is like @code{ecvt}, but @var{ndigit} specifies
2216the number of digits after the decimal point. If @var{ndigit} is less
2217than zero, @var{value} is rounded to the @math{@var{ndigit}+1}'th place to the
2218left of the decimal point. For example, if @var{ndigit} is @code{-1},
2219@var{value} will be rounded to the nearest 10. If @var{ndigit} is
2220negative and larger than the number of digits to the left of the decimal
2221point in @var{value}, @var{value} will be rounded to one significant digit.
880f421f
UD
2222
2223The returned string is statically allocated and overwritten by each call
2224to @code{fcvt}.
880f421f
UD
2225@end deftypefun
2226
2227@comment stdlib.h
2228@comment SVID, Unix98
2229@deftypefun {char *} gcvt (double @var{value}, int @var{ndigit}, char *@var{buf})
7a68c94a
UD
2230@code{gcvt} is functionally equivalent to @samp{sprintf(buf, "%*g",
2231ndigit, value}. It is provided only for compatibility's sake. It
2232returns @var{buf}.
880f421f
UD
2233@end deftypefun
2234
7a68c94a
UD
2235As extensions, the GNU C library provides versions of these three
2236functions that take @code{long double} arguments.
880f421f
UD
2237
2238@comment stdlib.h
2239@comment GNU
7a68c94a
UD
2240@deftypefun {char *} qecvt (long double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg})
2241This function is equivalent to @code{ecvt} except that it
2242takes a @code{long double} for the first parameter.
880f421f
UD
2243@end deftypefun
2244
2245@comment stdlib.h
2246@comment GNU
7a68c94a
UD
2247@deftypefun {char *} qfcvt (long double @var{value}, int @var{ndigit}, int @var{decpt}, int *@var{neg})
2248This function is equivalent to @code{fcvt} except that it
2249takes a @code{long double} for the first parameter.
880f421f
UD
2250@end deftypefun
2251
2252@comment stdlib.h
2253@comment GNU
2254@deftypefun {char *} qgcvt (long double @var{value}, int @var{ndigit}, char *@var{buf})
7a68c94a
UD
2255This function is equivalent to @code{gcvt} except that it
2256takes a @code{long double} for the first parameter.
880f421f
UD
2257@end deftypefun
2258
2259
2260@cindex gcvt_r
7a68c94a
UD
2261The @code{ecvt} and @code{fcvt} functions, and their @code{long double}
2262equivalents, all return a string located in a static buffer which is
2263overwritten by the next call to the function. The GNU C library
2264provides another set of extended functions which write the converted
2265string into a user-supplied buffer. These have the conventional
2266@code{_r} suffix.
2267
2268@code{gcvt_r} is not necessary, because @code{gcvt} already uses a
2269user-supplied buffer.
880f421f
UD
2270
2271@comment stdlib.h
2272@comment GNU
7a68c94a
UD
2273@deftypefun {char *} ecvt_r (double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg}, char *@var{buf}, size_t @var{len})
2274The @code{ecvt_r} function is the same as @code{ecvt}, except
2275that it places its result into the user-specified buffer pointed to by
2276@var{buf}, with length @var{len}.
880f421f 2277
7a68c94a 2278This function is a GNU extension.
880f421f
UD
2279@end deftypefun
2280
2281@comment stdlib.h
2282@comment SVID, Unix98
7a68c94a
UD
2283@deftypefun {char *} fcvt_r (double @var{value}, int @var{ndigit}, int @var{decpt}, int *@var{neg}, char *@var{buf}, size_t @var{len})
2284The @code{fcvt_r} function is the same as @code{fcvt}, except
2285that it places its result into the user-specified buffer pointed to by
2286@var{buf}, with length @var{len}.
880f421f 2287
7a68c94a 2288This function is a GNU extension.
880f421f
UD
2289@end deftypefun
2290
2291@comment stdlib.h
2292@comment GNU
7a68c94a
UD
2293@deftypefun {char *} qecvt_r (long double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg}, char *@var{buf}, size_t @var{len})
2294The @code{qecvt_r} function is the same as @code{qecvt}, except
2295that it places its result into the user-specified buffer pointed to by
2296@var{buf}, with length @var{len}.
880f421f 2297
7a68c94a 2298This function is a GNU extension.
880f421f
UD
2299@end deftypefun
2300
2301@comment stdlib.h
2302@comment GNU
7a68c94a
UD
2303@deftypefun {char *} qfcvt_r (long double @var{value}, int @var{ndigit}, int @var{decpt}, int *@var{neg}, char *@var{buf}, size_t @var{len})
2304The @code{qfcvt_r} function is the same as @code{qfcvt}, except
2305that it places its result into the user-specified buffer pointed to by
2306@var{buf}, with length @var{len}.
880f421f 2307
7a68c94a 2308This function is a GNU extension.
880f421f 2309@end deftypefun