1 @node Arithmetic, Date and Time, Mathematics, Top
2 @c %MENU% Low level arithmetic functions
3 @chapter Arithmetic Functions
5 This chapter contains information about functions for doing basic
6 arithmetic operations, such as splitting a float into its integer and
7 fractional parts or retrieving the imaginary part of a complex value.
8 These functions are declared in the header files @file{math.h} and
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.
25 @node Floating Point Numbers
26 @section Floating Point Numbers
27 @cindex floating point
29 @cindex IEEE floating point
31 Most computer hardware has support for two different kinds of numbers:
32 integers (@math{@dots{}-3, -2, -1, 0, 1, 2, 3@dots{}}) and
33 floating-point numbers. Floating-point numbers have three parts: the
34 @dfn{mantissa}, the @dfn{exponent}, and the @dfn{sign bit}. The real
35 number represented by a floating-point value is given by
37 $(s \mathrel? -1 \mathrel: 1) \cdot 2^e \cdot M$
40 @math{(s ? -1 : 1) @mul{} 2^e @mul{} M}
42 where @math{s} is the sign bit, @math{e} the exponent, and @math{M}
43 the mantissa. @xref{Floating Point Concepts}, for details. (It is
44 possible to have a different @dfn{base} for the exponent, but all modern
45 hardware uses @math{2}.)
47 Floating-point numbers can represent a finite subset of the real
48 numbers. While this subset is large enough for most purposes, it is
49 important to remember that the only reals that can be represented
50 exactly are rational numbers that have a terminating binary expansion
51 shorter than the width of the mantissa. Even simple fractions such as
52 @math{1/5} can only be approximated by floating point.
54 Mathematical operations and functions frequently need to produce values
55 that are not representable. Often these values can be approximated
56 closely enough for practical purposes, but sometimes they can't.
57 Historically there was no way to tell when the results of a calculation
58 were inaccurate. Modern computers implement the @w{IEEE 754} standard
59 for numerical computations, which defines a framework for indicating to
60 the program when the results of calculation are not trustworthy. This
61 framework consists of a set of @dfn{exceptions} that indicate why a
62 result could not be represented, and the special values @dfn{infinity}
63 and @dfn{not a number} (NaN).
65 @node Floating Point Classes
66 @section Floating-Point Number Classification Functions
67 @cindex floating-point classes
68 @cindex classes, floating-point
71 @w{ISO C 9x} defines macros that let you determine what sort of
72 floating-point number a variable holds.
76 @deftypefn {Macro} int fpclassify (@emph{float-type} @var{x})
77 This is a generic macro which works on all floating-point types and
78 which returns a value of type @code{int}. The possible values are:
82 The floating-point number @var{x} is ``Not a Number'' (@pxref{Infinity
85 The value of @var{x} is either plus or minus infinity (@pxref{Infinity
88 The value of @var{x} is zero. In floating-point formats like @w{IEEE
89 754}, where zero can be signed, this value is also returned if
90 @var{x} is negative zero.
92 Numbers whose absolute value is too small to be represented in the
93 normal format are represented in an alternate, @dfn{denormalized} format
94 (@pxref{Floating Point Concepts}). This format is less precise but can
95 represent values closer to zero. @code{fpclassify} returns this value
96 for values of @var{x} in this alternate format.
98 This value is returned for all other values of @var{x}. It indicates
99 that there is nothing special about the number.
104 @code{fpclassify} is most useful if more than one property of a number
105 must be tested. There are more specific macros which only test one
106 property at a time. Generally these macros execute faster than
107 @code{fpclassify}, since there is special hardware support for them.
108 You should therefore use the specific macros whenever possible.
112 @deftypefn {Macro} int isfinite (@emph{float-type} @var{x})
113 This macro returns a nonzero value if @var{x} is finite: not plus or
114 minus infinity, and not NaN. It is equivalent to
117 (fpclassify (x) != FP_NAN && fpclassify (x) != FP_INFINITE)
120 @code{isfinite} is implemented as a macro which accepts any
126 @deftypefn {Macro} int isnormal (@emph{float-type} @var{x})
127 This macro returns a nonzero value if @var{x} is finite and normalized.
131 (fpclassify (x) == FP_NORMAL)
137 @deftypefn {Macro} int isnan (@emph{float-type} @var{x})
138 This macro returns a nonzero value if @var{x} is NaN. It is equivalent
142 (fpclassify (x) == FP_NAN)
146 Another set of floating-point classification functions was provided by
147 BSD. The GNU C library also supports these functions; however, we
148 recommend that you use the C9x macros in new code. Those are standard
149 and will be available more widely. Also, since they are macros, you do
150 not have to worry about the type of their argument.
154 @deftypefun int isinf (double @var{x})
155 @deftypefunx int isinff (float @var{x})
156 @deftypefunx int isinfl (long double @var{x})
157 This function returns @code{-1} if @var{x} represents negative infinity,
158 @code{1} if @var{x} represents positive infinity, and @code{0} otherwise.
163 @deftypefun int isnan (double @var{x})
164 @deftypefunx int isnanf (float @var{x})
165 @deftypefunx int isnanl (long double @var{x})
166 This function returns a nonzero value if @var{x} is a ``not a number''
167 value, and zero otherwise.
169 @strong{Note:} The @code{isnan} macro defined by @w{ISO C 9x} overrides
170 the BSD function. This is normally not a problem, because the two
171 routines behave identically. However, if you really need to get the BSD
172 function for some reason, you can write
181 @deftypefun int finite (double @var{x})
182 @deftypefunx int finitef (float @var{x})
183 @deftypefunx int finitel (long double @var{x})
184 This function returns a nonzero value if @var{x} is finite or a ``not a
185 number'' value, and zero otherwise.
190 @deftypefun double infnan (int @var{error})
191 This function is provided for compatibility with BSD. Its argument is
192 an error code, @code{EDOM} or @code{ERANGE}; @code{infnan} returns the
193 value that a math function would return if it set @code{errno} to that
194 value. @xref{Math Error Reporting}. @code{-ERANGE} is also acceptable
195 as an argument, and corresponds to @code{-HUGE_VAL} as a value.
197 In the BSD library, on certain machines, @code{infnan} raises a fatal
198 signal in all cases. The GNU library does not do likewise, because that
199 does not fit the @w{ISO C} specification.
202 @strong{Portability Note:} The functions listed in this section are BSD
206 @node Floating Point Errors
207 @section Errors in Floating-Point Calculations
210 * FP Exceptions:: IEEE 754 math exceptions and how to detect them.
211 * Infinity and NaN:: Special values returned by calculations.
212 * Status bit operations:: Checking for exceptions after the fact.
213 * Math Error Reporting:: How the math functions report errors.
217 @subsection FP Exceptions
221 @cindex division by zero
222 @cindex inexact exception
223 @cindex invalid exception
224 @cindex overflow exception
225 @cindex underflow exception
227 The @w{IEEE 754} standard defines five @dfn{exceptions} that can occur
228 during a calculation. Each corresponds to a particular sort of error,
231 When exceptions occur (when exceptions are @dfn{raised}, in the language
232 of the standard), one of two things can happen. By default the
233 exception is simply noted in the floating-point @dfn{status word}, and
234 the program continues as if nothing had happened. The operation
235 produces a default value, which depends on the exception (see the table
236 below). Your program can check the status word to find out which
239 Alternatively, you can enable @dfn{traps} for exceptions. In that case,
240 when an exception is raised, your program will receive the @code{SIGFPE}
241 signal. The default action for this signal is to terminate the
242 program. @xref{Signal Handling} for how you can change the effect of
246 In the System V math library, the user-defined function @code{matherr}
247 is called when certain exceptions occur inside math library functions.
248 However, the Unix98 standard deprecates this interface. We support it
249 for historical compatibility, but recommend that you do not use it in
253 The exceptions defined in @w{IEEE 754} are:
256 @item Invalid Operation
257 This exception is raised if the given operands are invalid for the
258 operation to be performed. Examples are
259 (see @w{IEEE 754}, @w{section 7}):
262 Addition or subtraction: @math{@infinity{} - @infinity{}}. (But
263 @math{@infinity{} + @infinity{} = @infinity{}}).
265 Multiplication: @math{0 @mul{} @infinity{}}.
267 Division: @math{0/0} or @math{@infinity{}/@infinity{}}.
269 Remainder: @math{x} REM @math{y}, where @math{y} is zero or @math{x} is
272 Square root if the operand is less then zero. More generally, any
273 mathematical function evaluated outside its domain produces this
276 Conversion of a floating-point number to an integer or decimal
277 string, when the number cannot be represented in the target format (due
278 to overflow, infinity, or NaN).
280 Conversion of an unrecognizable input string.
282 Comparison via predicates involving @math{<} or @math{>}, when one or
283 other of the operands is NaN. You can prevent this exception by using
284 the unordered comparison functions instead; see @ref{FP Comparison Functions}.
287 If the exception does not trap, the result of the operation is NaN.
289 @item Division by Zero
290 This exception is raised when a finite nonzero number is divided
291 by zero. If no trap occurs the result is either @math{+@infinity{}} or
292 @math{-@infinity{}}, depending on the signs of the operands.
295 This exception is raised whenever the result cannot be represented
296 as a finite value in the precision format of the destination. If no trap
297 occurs the result depends on the sign of the intermediate result and the
298 current rounding mode (@w{IEEE 754}, @w{section 7.3}):
301 Round to nearest carries all overflows to @math{@infinity{}}
302 with the sign of the intermediate result.
304 Round toward @math{0} carries all overflows to the largest representable
305 finite number with the sign of the intermediate result.
307 Round toward @math{-@infinity{}} carries positive overflows to the
308 largest representable finite number and negative overflows to
312 Round toward @math{@infinity{}} carries negative overflows to the
313 most negative representable finite number and positive overflows
314 to @math{@infinity{}}.
317 Whenever the overflow exception is raised, the inexact exception is also
321 The underflow exception is raised when an intermediate result is too
322 small to be calculated accurately, or if the operation's result rounded
323 to the destination precision is too small to be normalized.
325 When no trap is installed for the underflow exception, underflow is
326 signaled (via the underflow flag) only when both tininess and loss of
327 accuracy have been detected. If no trap handler is installed the
328 operation continues with an imprecise small value, or zero if the
329 destination precision cannot hold the small exact result.
332 This exception is signalled if a rounded result is not exact (such as
333 when calculating the square root of two) or a result overflows without
337 @node Infinity and NaN
338 @subsection Infinity and NaN
343 @w{IEEE 754} floating point numbers can represent positive or negative
344 infinity, and @dfn{NaN} (not a number). These three values arise from
345 calculations whose result is undefined or cannot be represented
346 accurately. You can also deliberately set a floating-point variable to
347 any of them, which is sometimes useful. Some examples of calculations
348 that produce infinity or NaN:
352 @math{1/0 = @infinity{}}
353 @math{log (0) = -@infinity{}}
354 @math{sqrt (-1) = NaN}
358 $${1\over0} = \infty$$
360 $$\sqrt{-1} = \hbox{NaN}$$
363 When a calculation produces any of these values, an exception also
364 occurs; see @ref{FP Exceptions}.
366 The basic operations and math functions all accept infinity and NaN and
367 produce sensible output. Infinities propagate through calculations as
368 one would expect: for example, @math{2 + @infinity{} = @infinity{}},
369 @math{4/@infinity{} = 0}, atan @math{(@infinity{}) = @pi{}/2}. NaN, on
370 the other hand, infects any calculation that involves it. Unless the
371 calculation would produce the same result no matter what real value
372 replaced NaN, the result is NaN.
374 In comparison operations, positive infinity is larger than all values
375 except itself and NaN, and negative infinity is smaller than all values
376 except itself and NaN. NaN is @dfn{unordered}: it is not equal to,
377 greater than, or less than anything, @emph{including itself}. @code{x ==
378 x} is false if the value of @code{x} is NaN. You can use this to test
379 whether a value is NaN or not, but the recommended way to test for NaN
380 is with the @code{isnan} function (@pxref{Floating Point Classes}). In
381 addition, @code{<}, @code{>}, @code{<=}, and @code{>=} will raise an
382 exception when applied to NaNs.
384 @file{math.h} defines macros that allow you to explicitly set a variable
389 @deftypevr Macro float INFINITY
390 An expression representing positive infinity. It is equal to the value
391 produced by mathematical operations like @code{1.0 / 0.0}.
392 @code{-INFINITY} represents negative infinity.
394 You can test whether a floating-point value is infinite by comparing it
395 to this macro. However, this is not recommended; you should use the
396 @code{isfinite} macro instead. @xref{Floating Point Classes}.
398 This macro was introduced in the @w{ISO C 9X} standard.
403 @deftypevr Macro float NAN
404 An expression representing a value which is ``not a number''. This
405 macro is a GNU extension, available only on machines that support the
406 ``not a number'' value---that is to say, on all machines that support
409 You can use @samp{#ifdef NAN} to test whether the machine supports
410 NaN. (Of course, you must arrange for GNU extensions to be visible,
411 such as by defining @code{_GNU_SOURCE}, and then you must include
415 @w{IEEE 754} also allows for another unusual value: negative zero. This
416 value is produced when you divide a positive number by negative
417 infinity, or when a negative result is smaller than the limits of
418 representation. Negative zero behaves identically to zero in all
419 calculations, unless you explicitly test the sign bit with
420 @code{signbit} or @code{copysign}.
422 @node Status bit operations
423 @subsection Examining the FPU status word
425 @w{ISO C 9x} defines functions to query and manipulate the
426 floating-point status word. You can use these functions to check for
427 untrapped exceptions when it's convenient, rather than worrying about
428 them in the middle of a calculation.
430 These constants represent the various @w{IEEE 754} exceptions. Not all
431 FPUs report all the different exceptions. Each constant is defined if
432 and only if the FPU you are compiling for supports that exception, so
433 you can test for FPU support with @samp{#ifdef}. They are defined in
440 The inexact exception.
444 The divide by zero exception.
448 The underflow exception.
452 The overflow exception.
456 The invalid exception.
459 The macro @code{FE_ALL_EXCEPT} is the bitwise OR of all exception macros
460 which are supported by the FP implementation.
462 These functions allow you to clear exception flags, test for exceptions,
463 and save and restore the set of exceptions flagged.
467 @deftypefun void feclearexcept (int @var{excepts})
468 This function clears all of the supported exception flags indicated by
474 @deftypefun int fetestexcept (int @var{excepts})
475 Test whether the exception flags indicated by the parameter @var{except}
476 are currently set. If any of them are, a nonzero value is returned
477 which specifies which exceptions are set. Otherwise the result is zero.
480 To understand these functions, imagine that the status word is an
481 integer variable named @var{status}. @code{feclearexcept} is then
482 equivalent to @samp{status &= ~excepts} and @code{fetestexcept} is
483 equivalent to @samp{(status & excepts)}. The actual implementation may
484 be very different, of course.
486 Exception flags are only cleared when the program explicitly requests it,
487 by calling @code{feclearexcept}. If you want to check for exceptions
488 from a set of calculations, you should clear all the flags first. Here
489 is a simple example of the way to use @code{fetestexcept}:
495 feclearexcept (FE_ALL_EXCEPT);
497 raised = fetestexcept (FE_OVERFLOW | FE_INVALID);
498 if (raised & FE_OVERFLOW) @{ /* ... */ @}
499 if (raised & FE_INVALID) @{ /* ... */ @}
504 You cannot explicitly set bits in the status word. You can, however,
505 save the entire status word and restore it later. This is done with the
510 @deftypefun void fegetexceptflag (fexcept_t *@var{flagp}, int @var{excepts})
511 This function stores in the variable pointed to by @var{flagp} an
512 implementation-defined value representing the current setting of the
513 exception flags indicated by @var{excepts}.
518 @deftypefun void fesetexceptflag (const fexcept_t *@var{flagp}, int
520 This function restores the flags for the exceptions indicated by
521 @var{excepts} to the values stored in the variable pointed to by
525 Note that the value stored in @code{fexcept_t} bears no resemblance to
526 the bit mask returned by @code{fetestexcept}. The type may not even be
527 an integer. Do not attempt to modify an @code{fexcept_t} variable.
529 @node Math Error Reporting
530 @subsection Error Reporting by Mathematical Functions
531 @cindex errors, mathematical
535 Many of the math functions are defined only over a subset of the real or
536 complex numbers. Even if they are mathematically defined, their result
537 may be larger or smaller than the range representable by their return
538 type. These are known as @dfn{domain errors}, @dfn{overflows}, and
539 @dfn{underflows}, respectively. Math functions do several things when
540 one of these errors occurs. In this manual we will refer to the
541 complete response as @dfn{signalling} a domain error, overflow, or
544 When a math function suffers a domain error, it raises the invalid
545 exception and returns NaN. It also sets @var{errno} to @code{EDOM};
546 this is for compatibility with old systems that do not support @w{IEEE
547 754} exception handling. Likewise, when overflow occurs, math
548 functions raise the overflow exception and return @math{@infinity{}} or
549 @math{-@infinity{}} as appropriate. They also set @var{errno} to
550 @code{ERANGE}. When underflow occurs, the underflow exception is
551 raised, and zero (appropriately signed) is returned. @var{errno} may be
552 set to @code{ERANGE}, but this is not guaranteed.
554 Some of the math functions are defined mathematically to result in a
555 complex value over parts of their domains. The most familiar example of
556 this is taking the square root of a negative number. The complex math
557 functions, such as @code{csqrt}, will return the appropriate complex value
558 in this case. The real-valued functions, such as @code{sqrt}, will
559 signal a domain error.
561 Some older hardware does not support infinities. On that hardware,
562 overflows instead return a particular very large number (usually the
563 largest representable number). @file{math.h} defines macros you can use
564 to test for overflow on both old and new hardware.
568 @deftypevr Macro double HUGE_VAL
569 @deftypevrx Macro float HUGE_VALF
570 @deftypevrx Macro {long double} HUGE_VALL
571 An expression representing a particular very large number. On machines
572 that use @w{IEEE 754} floating point format, @code{HUGE_VAL} is infinity.
573 On other machines, it's typically the largest positive number that can
576 Mathematical functions return the appropriately typed version of
577 @code{HUGE_VAL} or @code{@minus{}HUGE_VAL} when the result is too large
582 @section Rounding Modes
584 Floating-point calculations are carried out internally with extra
585 precision, and then rounded to fit into the destination type. This
586 ensures that results are as precise as the input data. @w{IEEE 754}
587 defines four possible rounding modes:
590 @item Round to nearest.
591 This is the default mode. It should be used unless there is a specific
592 need for one of the others. In this mode results are rounded to the
593 nearest representable value. If the result is midway between two
594 representable values, the even representable is chosen. @dfn{Even} here
595 means the lowest-order bit is zero. This rounding mode prevents
596 statistical bias and guarantees numeric stability: round-off errors in a
597 lengthy calculation will remain smaller than half of @code{FLT_EPSILON}.
599 @c @item Round toward @math{+@infinity{}}
600 @item Round toward plus Infinity.
601 All results are rounded to the smallest representable value
602 which is greater than the result.
604 @c @item Round toward @math{-@infinity{}}
605 @item Round toward minus Infinity.
606 All results are rounded to the largest representable value which is less
609 @item Round toward zero.
610 All results are rounded to the largest representable value whose
611 magnitude is less than that of the result. In other words, if the
612 result is negative it is rounded up; if it is positive, it is rounded
617 @file{fenv.h} defines constants which you can use to refer to the
618 various rounding modes. Each one will be defined if and only if the FPU
619 supports the corresponding rounding mode.
632 Round toward @math{+@infinity{}}.
638 Round toward @math{-@infinity{}}.
642 @vindex FE_TOWARDZERO
647 Underflow is an unusual case. Normally, @w{IEEE 754} floating point
648 numbers are always normalized (@pxref{Floating Point Concepts}).
649 Numbers smaller than @math{2^r} (where @math{r} is the minimum exponent,
650 @code{FLT_MIN_RADIX-1} for @var{float}) cannot be represented as
651 normalized numbers. Rounding all such numbers to zero or @math{2^r}
652 would cause some algorithms to fail at 0. Therefore, they are left in
653 denormalized form. That produces loss of precision, since some bits of
654 the mantissa are stolen to indicate the decimal point.
656 If a result is too small to be represented as a denormalized number, it
657 is rounded to zero. However, the sign of the result is preserved; if
658 the calculation was negative, the result is @dfn{negative zero}.
659 Negative zero can also result from some operations on infinity, such as
660 @math{4/-@infinity{}}. Negative zero behaves identically to zero except
661 when the @code{copysign} or @code{signbit} functions are used to check
662 the sign bit directly.
664 At any time one of the above four rounding modes is selected. You can
665 find out which one with this function:
669 @deftypefun int fegetround (void)
670 Returns the currently selected rounding mode, represented by one of the
671 values of the defined rounding mode macros.
675 To change the rounding mode, use this function:
679 @deftypefun int fesetround (int @var{round})
680 Changes the currently selected rounding mode to @var{round}. If
681 @var{round} does not correspond to one of the supported rounding modes
682 nothing is changed. @code{fesetround} returns a nonzero value if it
683 changed the rounding mode, zero if the mode is not supported.
686 You should avoid changing the rounding mode if possible. It can be an
687 expensive operation; also, some hardware requires you to compile your
688 program differently for it to work. The resulting code may run slower.
689 See your compiler documentation for details.
690 @c This section used to claim that functions existed to round one number
691 @c in a specific fashion. I can't find any functions in the library
694 @node Control Functions
695 @section Floating-Point Control Functions
697 @w{IEEE 754} floating-point implementations allow the programmer to
698 decide whether traps will occur for each of the exceptions, by setting
699 bits in the @dfn{control word}. In C, traps result in the program
700 receiving the @code{SIGFPE} signal; see @ref{Signal Handling}.
702 @strong{Note:} @w{IEEE 754} says that trap handlers are given details of
703 the exceptional situation, and can set the result value. C signals do
704 not provide any mechanism to pass this information back and forth.
705 Trapping exceptions in C is therefore not very useful.
707 It is sometimes necessary to save the state of the floating-point unit
708 while you perform some calculation. The library provides functions
709 which save and restore the exception flags, the set of exceptions that
710 generate traps, and the rounding mode. This information is known as the
711 @dfn{floating-point environment}.
713 The functions to save and restore the floating-point environment all use
714 a variable of type @code{fenv_t} to store information. This type is
715 defined in @file{fenv.h}. Its size and contents are
716 implementation-defined. You should not attempt to manipulate a variable
717 of this type directly.
719 To save the state of the FPU, use one of these functions:
723 @deftypefun void fegetenv (fenv_t *@var{envp})
724 Store the floating-point environment in the variable pointed to by
730 @deftypefun int feholdexcept (fenv_t *@var{envp})
731 Store the current floating-point environment in the object pointed to by
732 @var{envp}. Then clear all exception flags, and set the FPU to trap no
733 exceptions. Not all FPUs support trapping no exceptions; if
734 @code{feholdexcept} cannot set this mode, it returns zero. If it
735 succeeds, it returns a nonzero value.
738 The functions which restore the floating-point environment can take two
743 Pointers to @code{fenv_t} objects, which were initialized previously by a
744 call to @code{fegetenv} or @code{feholdexcept}.
747 The special macro @code{FE_DFL_ENV} which represents the floating-point
748 environment as it was available at program start.
750 Implementation defined macros with names starting with @code{FE_}.
752 @vindex FE_NOMASK_ENV
753 If possible, the GNU C Library defines a macro @code{FE_NOMASK_ENV}
754 which represents an environment where every exception raised causes a
755 trap to occur. You can test for this macro using @code{#ifdef}. It is
756 only defined if @code{_GNU_SOURCE} is defined.
758 Some platforms might define other predefined environments.
762 To set the floating-point environment, you can use either of these
767 @deftypefun void fesetenv (const fenv_t *@var{envp})
768 Set the floating-point environment to that described by @var{envp}.
773 @deftypefun void feupdateenv (const fenv_t *@var{envp})
774 Like @code{fesetenv}, this function sets the floating-point environment
775 to that described by @var{envp}. However, if any exceptions were
776 flagged in the status word before @code{feupdateenv} was called, they
777 remain flagged after the call. In other words, after @code{feupdateenv}
778 is called, the status word is the bitwise OR of the previous status word
779 and the one saved in @var{envp}.
782 @node Arithmetic Functions
783 @section Arithmetic Functions
785 The C library provides functions to do basic operations on
786 floating-point numbers. These include absolute value, maximum and minimum,
787 normalization, bit twiddling, rounding, and a few others.
790 * Absolute Value:: Absolute values of integers and floats.
791 * Normalization Functions:: Extracting exponents and putting them back.
792 * Rounding Functions:: Rounding floats to integers.
793 * Remainder Functions:: Remainders on division, precisely defined.
794 * FP Bit Twiddling:: Sign bit adjustment. Adding epsilon.
795 * FP Comparison Functions:: Comparisons without risk of exceptions.
796 * Misc FP Arithmetic:: Max, min, positive difference, multiply-add.
800 @subsection Absolute Value
801 @cindex absolute value functions
803 These functions are provided for obtaining the @dfn{absolute value} (or
804 @dfn{magnitude}) of a number. The absolute value of a real number
805 @var{x} is @var{x} if @var{x} is positive, @minus{}@var{x} if @var{x} is
806 negative. For a complex number @var{z}, whose real part is @var{x} and
807 whose imaginary part is @var{y}, the absolute value is @w{@code{sqrt
808 (@var{x}*@var{x} + @var{y}*@var{y})}}.
812 Prototypes for @code{abs}, @code{labs} and @code{llabs} are in @file{stdlib.h};
813 @code{fabs}, @code{fabsf} and @code{fabsl} are declared in @file{math.h}.
814 @code{cabs}, @code{cabsf} and @code{cabsl} are declared in @file{complex.h}.
818 @deftypefun int abs (int @var{number})
819 @deftypefunx {long int} labs (long int @var{number})
820 @deftypefunx {long long int} llabs (long long int @var{number})
821 These functions return the absolute value of @var{number}.
823 Most computers use a two's complement integer representation, in which
824 the absolute value of @code{INT_MIN} (the smallest possible @code{int})
825 cannot be represented; thus, @w{@code{abs (INT_MIN)}} is not defined.
827 @code{llabs} is new to @w{ISO C 9x}
832 @deftypefun double fabs (double @var{number})
833 @deftypefunx float fabsf (float @var{number})
834 @deftypefunx {long double} fabsl (long double @var{number})
835 This function returns the absolute value of the floating-point number
841 @deftypefun double cabs (complex double @var{z})
842 @deftypefunx float cabsf (complex float @var{z})
843 @deftypefunx {long double} cabsl (complex long double @var{z})
844 These functions return the absolute value of the complex number @var{z}
845 (@pxref{Complex Numbers}). The absolute value of a complex number is:
848 sqrt (creal (@var{z}) * creal (@var{z}) + cimag (@var{z}) * cimag (@var{z}))
851 This function should always be used instead of the direct formula
852 because it takes special care to avoid losing precision. It may also
853 take advantage of hardware support for this operation. See @code{hypot}
854 in @xref{Exponents and Logarithms}.
857 @node Normalization Functions
858 @subsection Normalization Functions
859 @cindex normalization functions (floating-point)
861 The functions described in this section are primarily provided as a way
862 to efficiently perform certain low-level manipulations on floating point
863 numbers that are represented internally using a binary radix;
864 see @ref{Floating Point Concepts}. These functions are required to
865 have equivalent behavior even if the representation does not use a radix
866 of 2, but of course they are unlikely to be particularly efficient in
870 All these functions are declared in @file{math.h}.
874 @deftypefun double frexp (double @var{value}, int *@var{exponent})
875 @deftypefunx float frexpf (float @var{value}, int *@var{exponent})
876 @deftypefunx {long double} frexpl (long double @var{value}, int *@var{exponent})
877 These functions are used to split the number @var{value}
878 into a normalized fraction and an exponent.
880 If the argument @var{value} is not zero, the return value is @var{value}
881 times a power of two, and is always in the range 1/2 (inclusive) to 1
882 (exclusive). The corresponding exponent is stored in
883 @code{*@var{exponent}}; the return value multiplied by 2 raised to this
884 exponent equals the original number @var{value}.
886 For example, @code{frexp (12.8, &exponent)} returns @code{0.8} and
887 stores @code{4} in @code{exponent}.
889 If @var{value} is zero, then the return value is zero and
890 zero is stored in @code{*@var{exponent}}.
895 @deftypefun double ldexp (double @var{value}, int @var{exponent})
896 @deftypefunx float ldexpf (float @var{value}, int @var{exponent})
897 @deftypefunx {long double} ldexpl (long double @var{value}, int @var{exponent})
898 These functions return the result of multiplying the floating-point
899 number @var{value} by 2 raised to the power @var{exponent}. (It can
900 be used to reassemble floating-point numbers that were taken apart
903 For example, @code{ldexp (0.8, 4)} returns @code{12.8}.
906 The following functions, which come from BSD, provide facilities
907 equivalent to those of @code{ldexp} and @code{frexp}.
911 @deftypefun double logb (double @var{x})
912 @deftypefunx float logbf (float @var{x})
913 @deftypefunx {long double} logbl (long double @var{x})
914 These functions return the integer part of the base-2 logarithm of
915 @var{x}, an integer value represented in type @code{double}. This is
916 the highest integer power of @code{2} contained in @var{x}. The sign of
917 @var{x} is ignored. For example, @code{logb (3.5)} is @code{1.0} and
918 @code{logb (4.0)} is @code{2.0}.
920 When @code{2} raised to this power is divided into @var{x}, it gives a
921 quotient between @code{1} (inclusive) and @code{2} (exclusive).
923 If @var{x} is zero, the return value is minus infinity if the machine
924 supports infinities, and a very small number if it does not. If @var{x}
925 is infinity, the return value is infinity.
927 For finite @var{x}, the value returned by @code{logb} is one less than
928 the value that @code{frexp} would store into @code{*@var{exponent}}.
933 @deftypefun double scalb (double @var{value}, int @var{exponent})
934 @deftypefunx float scalbf (float @var{value}, int @var{exponent})
935 @deftypefunx {long double} scalbl (long double @var{value}, int @var{exponent})
936 The @code{scalb} function is the BSD name for @code{ldexp}.
941 @deftypefun {long long int} scalbn (double @var{x}, int n)
942 @deftypefunx {long long int} scalbnf (float @var{x}, int n)
943 @deftypefunx {long long int} scalbnl (long double @var{x}, int n)
944 @code{scalbn} is identical to @code{scalb}, except that the exponent
945 @var{n} is an @code{int} instead of a floating-point number.
950 @deftypefun {long long int} scalbln (double @var{x}, long int n)
951 @deftypefunx {long long int} scalblnf (float @var{x}, long int n)
952 @deftypefunx {long long int} scalblnl (long double @var{x}, long int n)
953 @code{scalbln} is identical to @code{scalb}, except that the exponent
954 @var{n} is a @code{long int} instead of a floating-point number.
959 @deftypefun {long long int} significand (double @var{x})
960 @deftypefunx {long long int} significandf (float @var{x})
961 @deftypefunx {long long int} significandl (long double @var{x})
962 @code{significand} returns the mantissa of @var{x} scaled to the range
964 It is equivalent to @w{@code{scalb (@var{x}, (double) -ilogb (@var{x}))}}.
966 This function exists mainly for use in certain standardized tests
967 of @w{IEEE 754} conformance.
970 @node Rounding Functions
971 @subsection Rounding Functions
972 @cindex converting floats to integers
975 The functions listed here perform operations such as rounding and
976 truncation of floating-point values. Some of these functions convert
977 floating point numbers to integer values. They are all declared in
980 You can also convert floating-point numbers to integers simply by
981 casting them to @code{int}. This discards the fractional part,
982 effectively rounding towards zero. However, this only works if the
983 result can actually be represented as an @code{int}---for very large
984 numbers, this is impossible. The functions listed here return the
985 result as a @code{double} instead to get around this problem.
989 @deftypefun double ceil (double @var{x})
990 @deftypefunx float ceilf (float @var{x})
991 @deftypefunx {long double} ceill (long double @var{x})
992 These functions round @var{x} upwards to the nearest integer,
993 returning that value as a @code{double}. Thus, @code{ceil (1.5)}
999 @deftypefun double floor (double @var{x})
1000 @deftypefunx float floorf (float @var{x})
1001 @deftypefunx {long double} floorl (long double @var{x})
1002 These functions round @var{x} downwards to the nearest
1003 integer, returning that value as a @code{double}. Thus, @code{floor
1004 (1.5)} is @code{1.0} and @code{floor (-1.5)} is @code{-2.0}.
1009 @deftypefun double trunc (double @var{x})
1010 @deftypefunx float truncf (float @var{x})
1011 @deftypefunx {long double} truncl (long double @var{x})
1012 @code{trunc} is another name for @code{floor}
1017 @deftypefun double rint (double @var{x})
1018 @deftypefunx float rintf (float @var{x})
1019 @deftypefunx {long double} rintl (long double @var{x})
1020 These functions round @var{x} to an integer value according to the
1021 current rounding mode. @xref{Floating Point Parameters}, for
1022 information about the various rounding modes. The default
1023 rounding mode is to round to the nearest integer; some machines
1024 support other modes, but round-to-nearest is always used unless
1025 you explicitly select another.
1027 If @var{x} was not initially an integer, these functions raise the
1033 @deftypefun double nearbyint (double @var{x})
1034 @deftypefunx float nearbyintf (float @var{x})
1035 @deftypefunx {long double} nearbyintl (long double @var{x})
1036 These functions return the same value as the @code{rint} functions, but
1037 do not raise the inexact exception if @var{x} is not an integer.
1042 @deftypefun double round (double @var{x})
1043 @deftypefunx float roundf (float @var{x})
1044 @deftypefunx {long double} roundl (long double @var{x})
1045 These functions are similar to @code{rint}, but they round halfway
1046 cases away from zero instead of to the nearest even integer.
1051 @deftypefun {long int} lrint (double @var{x})
1052 @deftypefunx {long int} lrintf (float @var{x})
1053 @deftypefunx {long int} lrintl (long double @var{x})
1054 These functions are just like @code{rint}, but they return a
1055 @code{long int} instead of a floating-point number.
1060 @deftypefun {long long int} llrint (double @var{x})
1061 @deftypefunx {long long int} llrintf (float @var{x})
1062 @deftypefunx {long long int} llrintl (long double @var{x})
1063 These functions are just like @code{rint}, but they return a
1064 @code{long long int} instead of a floating-point number.
1069 @deftypefun {long int} lround (double @var{x})
1070 @deftypefunx {long int} lroundf (float @var{x})
1071 @deftypefunx {long int} lroundl (long double @var{x})
1072 These functions are just like @code{round}, but they return a
1073 @code{long int} instead of a floating-point number.
1078 @deftypefun {long long int} llround (double @var{x})
1079 @deftypefunx {long long int} llroundf (float @var{x})
1080 @deftypefunx {long long int} llroundl (long double @var{x})
1081 These functions are just like @code{round}, but they return a
1082 @code{long long int} instead of a floating-point number.
1088 @deftypefun double modf (double @var{value}, double *@var{integer-part})
1089 @deftypefunx float modff (float @var{value}, float *@var{integer-part})
1090 @deftypefunx {long double} modfl (long double @var{value}, long double *@var{integer-part})
1091 These functions break the argument @var{value} into an integer part and a
1092 fractional part (between @code{-1} and @code{1}, exclusive). Their sum
1093 equals @var{value}. Each of the parts has the same sign as @var{value},
1094 and the integer part is always rounded toward zero.
1096 @code{modf} stores the integer part in @code{*@var{integer-part}}, and
1097 returns the fractional part. For example, @code{modf (2.5, &intpart)}
1098 returns @code{0.5} and stores @code{2.0} into @code{intpart}.
1101 @node Remainder Functions
1102 @subsection Remainder Functions
1104 The functions in this section compute the remainder on division of two
1105 floating-point numbers. Each is a little different; pick the one that
1110 @deftypefun double fmod (double @var{numerator}, double @var{denominator})
1111 @deftypefunx float fmodf (float @var{numerator}, float @var{denominator})
1112 @deftypefunx {long double} fmodl (long double @var{numerator}, long double @var{denominator})
1113 These functions compute the remainder from the division of
1114 @var{numerator} by @var{denominator}. Specifically, the return value is
1115 @code{@var{numerator} - @w{@var{n} * @var{denominator}}}, where @var{n}
1116 is the quotient of @var{numerator} divided by @var{denominator}, rounded
1117 towards zero to an integer. Thus, @w{@code{fmod (6.5, 2.3)}} returns
1118 @code{1.9}, which is @code{6.5} minus @code{4.6}.
1120 The result has the same sign as the @var{numerator} and has magnitude
1121 less than the magnitude of the @var{denominator}.
1123 If @var{denominator} is zero, @code{fmod} signals a domain error.
1128 @deftypefun double drem (double @var{numerator}, double @var{denominator})
1129 @deftypefunx float dremf (float @var{numerator}, float @var{denominator})
1130 @deftypefunx {long double} dreml (long double @var{numerator}, long double @var{denominator})
1131 These functions are like @code{fmod} except that they rounds the
1132 internal quotient @var{n} to the nearest integer instead of towards zero
1133 to an integer. For example, @code{drem (6.5, 2.3)} returns @code{-0.4},
1134 which is @code{6.5} minus @code{6.9}.
1136 The absolute value of the result is less than or equal to half the
1137 absolute value of the @var{denominator}. The difference between
1138 @code{fmod (@var{numerator}, @var{denominator})} and @code{drem
1139 (@var{numerator}, @var{denominator})} is always either
1140 @var{denominator}, minus @var{denominator}, or zero.
1142 If @var{denominator} is zero, @code{drem} signals a domain error.
1147 @deftypefun double remainder (double @var{numerator}, double @var{denominator})
1148 @deftypefunx float remainderf (float @var{numerator}, float @var{denominator})
1149 @deftypefunx {long double} remainderl (long double @var{numerator}, long double @var{denominator})
1150 This function is another name for @code{drem}.
1153 @node FP Bit Twiddling
1154 @subsection Setting and modifying single bits of FP values
1155 @cindex FP arithmetic
1157 There are some operations that are too complicated or expensive to
1158 perform by hand on floating-point numbers. @w{ISO C 9x} defines
1159 functions to do these operations, which mostly involve changing single
1164 @deftypefun double copysign (double @var{x}, double @var{y})
1165 @deftypefunx float copysignf (float @var{x}, float @var{y})
1166 @deftypefunx {long double} copysignl (long double @var{x}, long double @var{y})
1167 These functions return @var{x} but with the sign of @var{y}. They work
1168 even if @var{x} or @var{y} are NaN or zero. Both of these can carry a
1169 sign (although not all implementations support it) and this is one of
1170 the few operations that can tell the difference.
1172 @code{copysign} never raises an exception.
1173 @c except signalling NaNs
1175 This function is defined in @w{IEC 559} (and the appendix with
1176 recommended functions in @w{IEEE 754}/@w{IEEE 854}).
1181 @deftypefun int signbit (@emph{float-type} @var{x})
1182 @code{signbit} is a generic macro which can work on all floating-point
1183 types. It returns a nonzero value if the value of @var{x} has its sign
1186 This is not the same as @code{x < 0.0}, because @w{IEEE 754} floating
1187 point allows zero to be signed. The comparison @code{-0.0 < 0.0} is
1188 false, but @code{signbit (-0.0)} will return a nonzero value.
1193 @deftypefun double nextafter (double @var{x}, double @var{y})
1194 @deftypefunx float nextafterf (float @var{x}, float @var{y})
1195 @deftypefunx {long double} nextafterl (long double @var{x}, long double @var{y})
1196 The @code{nextafter} function returns the next representable neighbor of
1197 @var{x} in the direction towards @var{y}. The size of the step between
1198 @var{x} and the result depends on the type of the result. If
1199 @math{@var{x} = @var{y}} the function simply returns @var{x}. If either
1200 value is @code{NaN}, @code{NaN} is returned. Otherwise
1201 a value corresponding to the value of the least significant bit in the
1202 mantissa is added or subtracted, depending on the direction.
1203 @code{nextafter} will signal overflow or underflow if the result goes
1204 outside of the range of normalized numbers.
1206 This function is defined in @w{IEC 559} (and the appendix with
1207 recommended functions in @w{IEEE 754}/@w{IEEE 854}).
1212 @deftypefun {long long int} nextafterx (double @var{x}, long double @var{y})
1213 @deftypefunx {long long int} nextafterxf (float @var{x}, long double @var{y})
1214 @deftypefunx {long long int} nextafterxl (long double @var{x}, long double @var{y})
1215 These functions are identical to the corresponding versions of
1216 @code{nextafter} except that their second argument is a @code{long
1223 @deftypefun double nan (const char *@var{tagp})
1224 @deftypefunx float nanf (const char *@var{tagp})
1225 @deftypefunx {long double} nanl (const char *@var{tagp})
1226 The @code{nan} function returns a representation of NaN, provided that
1227 NaN is supported by the target platform.
1228 @code{nan ("@var{n-char-sequence}")} is equivalent to
1229 @code{strtod ("NAN(@var{n-char-sequence})")}.
1231 The argument @var{tagp} is used in an unspecified manner. On @w{IEEE
1232 754} systems, there are many representations of NaN, and @var{tagp}
1233 selects one. On other systems it may do nothing.
1236 @node FP Comparison Functions
1237 @subsection Floating-Point Comparison Functions
1238 @cindex unordered comparison
1240 The standard C comparison operators provoke exceptions when one or other
1241 of the operands is NaN. For example,
1248 will raise an exception if @var{a} is NaN. (This does @emph{not}
1249 happen with @code{==} and @code{!=}; those merely return false and true,
1250 respectively, when NaN is examined.) Frequently this exception is
1251 undesirable. @w{ISO C 9x} therefore defines comparison functions that
1252 do not raise exceptions when NaN is examined. All of the functions are
1253 implemented as macros which allow their arguments to be of any
1254 floating-point type. The macros are guaranteed to evaluate their
1255 arguments only once.
1259 @deftypefn Macro int isgreater (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
1260 This macro determines whether the argument @var{x} is greater than
1261 @var{y}. It is equivalent to @code{(@var{x}) > (@var{y})}, but no
1262 exception is raised if @var{x} or @var{y} are NaN.
1267 @deftypefn Macro int isgreaterequal (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
1268 This macro determines whether the argument @var{x} is greater than or
1269 equal to @var{y}. It is equivalent to @code{(@var{x}) >= (@var{y})}, but no
1270 exception is raised if @var{x} or @var{y} are NaN.
1275 @deftypefn Macro int isless (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
1276 This macro determines whether the argument @var{x} is less than @var{y}.
1277 It is equivalent to @code{(@var{x}) < (@var{y})}, but no exception is
1278 raised if @var{x} or @var{y} are NaN.
1283 @deftypefn Macro int islessequal (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
1284 This macro determines whether the argument @var{x} is less than or equal
1285 to @var{y}. It is equivalent to @code{(@var{x}) <= (@var{y})}, but no
1286 exception is raised if @var{x} or @var{y} are NaN.
1291 @deftypefn Macro int islessgreater (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
1292 This macro determines whether the argument @var{x} is less or greater
1293 than @var{y}. It is equivalent to @code{(@var{x}) < (@var{y}) ||
1294 (@var{x}) > (@var{y})} (although it only evaluates @var{x} and @var{y}
1295 once), but no exception is raised if @var{x} or @var{y} are NaN.
1297 This macro is not equivalent to @code{@var{x} != @var{y}}, because that
1298 expression is true if @var{x} or @var{y} are NaN.
1303 @deftypefn Macro int isunordered (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
1304 This macro determines whether its arguments are unordered. In other
1305 words, it is true if @var{x} or @var{y} are NaN, and false otherwise.
1308 Not all machines provide hardware support for these operations. On
1309 machines that don't, the macros can be very slow. Therefore, you should
1310 not use these functions when NaN is not a concern.
1312 @strong{Note:} There are no macros @code{isequal} or @code{isunequal}.
1313 They are unnecessary, because the @code{==} and @code{!=} operators do
1314 @emph{not} throw an exception if one or both of the operands are NaN.
1316 @node Misc FP Arithmetic
1317 @subsection Miscellaneous FP arithmetic functions
1320 @cindex positive difference
1321 @cindex multiply-add
1323 The functions in this section perform miscellaneous but common
1324 operations that are awkward to express with C operators. On some
1325 processors these functions can use special machine instructions to
1326 perform these operations faster than the equivalent C code.
1330 @deftypefun double fmin (double @var{x}, double @var{y})
1331 @deftypefunx float fminf (float @var{x}, float @var{y})
1332 @deftypefunx {long double} fminl (long double @var{x}, long double @var{y})
1333 The @code{fmin} function returns the lesser of the two values @var{x}
1334 and @var{y}. It is similar to the expression
1336 ((x) < (y) ? (x) : (y))
1338 except that @var{x} and @var{y} are only evaluated once.
1340 If an argument is NaN, the other argument is returned. If both arguments
1341 are NaN, NaN is returned.
1346 @deftypefun double fmax (double @var{x}, double @var{y})
1347 @deftypefunx float fmaxf (float @var{x}, float @var{y})
1348 @deftypefunx {long double} fmaxl (long double @var{x}, long double @var{y})
1349 The @code{fmax} function returns the greater of the two values @var{x}
1352 If an argument is NaN, the other argument is returned. If both arguments
1353 are NaN, NaN is returned.
1358 @deftypefun double fdim (double @var{x}, double @var{y})
1359 @deftypefunx float fdimf (float @var{x}, float @var{y})
1360 @deftypefunx {long double} fdiml (long double @var{x}, long double @var{y})
1361 The @code{fdim} function returns the positive difference between
1362 @var{x} and @var{y}. The positive difference is @math{@var{x} -
1363 @var{y}} if @var{x} is greater than @var{y}, and @math{0} otherwise.
1365 If @var{x}, @var{y}, or both are NaN, NaN is returned.
1370 @deftypefun double fma (double @var{x}, double @var{y}, double @var{z})
1371 @deftypefunx float fmaf (float @var{x}, float @var{y}, float @var{z})
1372 @deftypefunx {long double} fmal (long double @var{x}, long double @var{y}, long double @var{z})
1374 The @code{fma} function performs floating-point multiply-add. This is
1375 the operation @math{(@var{x} @mul{} @var{y}) + @var{z}}, but the
1376 intermediate result is not rounded to the destination type. This can
1377 sometimes improve the precision of a calculation.
1379 This function was introduced because some processors have a special
1380 instruction to perform multiply-add. The C compiler cannot use it
1381 directly, because the expression @samp{x*y + z} is defined to round the
1382 intermediate result. @code{fma} lets you choose when you want to round
1386 On processors which do not implement multiply-add in hardware,
1387 @code{fma} can be very slow since it must avoid intermediate rounding.
1388 @file{math.h} defines the symbols @code{FP_FAST_FMA},
1389 @code{FP_FAST_FMAF}, and @code{FP_FAST_FMAL} when the corresponding
1390 version of @code{fma} is no slower than the expression @samp{x*y + z}.
1391 In the GNU C library, this always means the operation is implemented in
1395 @node Complex Numbers
1396 @section Complex Numbers
1398 @cindex complex numbers
1400 @w{ISO C 9x} introduces support for complex numbers in C. This is done
1401 with a new type qualifier, @code{complex}. It is a keyword if and only
1402 if @file{complex.h} has been included. There are three complex types,
1403 corresponding to the three real types: @code{float complex},
1404 @code{double complex}, and @code{long double complex}.
1406 To construct complex numbers you need a way to indicate the imaginary
1407 part of a number. There is no standard notation for an imaginary
1408 floating point constant. Instead, @file{complex.h} defines two macros
1409 that can be used to create complex numbers.
1411 @deftypevr Macro {const float complex} _Complex_I
1412 This macro is a representation of the complex number ``@math{0+1i}''.
1413 Multiplying a real floating-point value by @code{_Complex_I} gives a
1414 complex number whose value is purely imaginary. You can use this to
1415 construct complex constants:
1418 @math{3.0 + 4.0i} = @code{3.0 + 4.0 * _Complex_I}
1421 Note that @code{_Complex_I * _Complex_I} has the value @code{-1}, but
1422 the type of that value is @code{complex}.
1425 @c Put this back in when gcc supports _Imaginary_I. It's too confusing.
1428 Without an optimizing compiler this is more expensive than the use of
1429 @code{_Imaginary_I} but with is better than nothing. You can avoid all
1430 the hassles if you use the @code{I} macro below if the name is not
1433 @deftypevr Macro {const float imaginary} _Imaginary_I
1434 This macro is a representation of the value ``@math{1i}''. I.e., it is
1438 _Imaginary_I * _Imaginary_I = -1
1442 The result is not of type @code{float imaginary} but instead @code{float}.
1443 One can use it to easily construct complex number like in
1446 3.0 - _Imaginary_I * 4.0
1450 which results in the complex number with a real part of 3.0 and a
1451 imaginary part -4.0.
1456 @code{_Complex_I} is a bit of a mouthful. @file{complex.h} also defines
1457 a shorter name for the same constant.
1459 @deftypevr Macro {const float complex} I
1460 This macro has exactly the same value as @code{_Complex_I}. Most of the
1461 time it is preferable. However, it causes problems if you want to use
1462 the identifier @code{I} for something else. You can safely write
1465 #include <complex.h>
1470 if you need @code{I} for your own purposes. (In that case we recommend
1471 you also define some other short name for @code{_Complex_I}, such as
1475 If the implementation does not support the @code{imaginary} types
1476 @code{I} is defined as @code{_Complex_I} which is the second best
1477 solution. It still can be used in the same way but requires a most
1478 clever compiler to get the same results.
1482 @node Operations on Complex
1483 @section Projections, Conjugates, and Decomposing of Complex Numbers
1484 @cindex project complex numbers
1485 @cindex conjugate complex numbers
1486 @cindex decompose complex numbers
1489 @w{ISO C 9x} also defines functions that perform basic operations on
1490 complex numbers, such as decomposition and conjugation. The prototypes
1491 for all these functions are in @file{complex.h}. All functions are
1492 available in three variants, one for each of the three complex types.
1496 @deftypefun double creal (complex double @var{z})
1497 @deftypefunx float crealf (complex float @var{z})
1498 @deftypefunx {long double} creall (complex long double @var{z})
1499 These functions return the real part of the complex number @var{z}.
1504 @deftypefun double cimag (complex double @var{z})
1505 @deftypefunx float cimagf (complex float @var{z})
1506 @deftypefunx {long double} cimagl (complex long double @var{z})
1507 These functions return the imaginary part of the complex number @var{z}.
1512 @deftypefun {complex double} conj (complex double @var{z})
1513 @deftypefunx {complex float} conjf (complex float @var{z})
1514 @deftypefunx {complex long double} conjl (complex long double @var{z})
1515 These functions return the conjugate value of the complex number
1516 @var{z}. The conjugate of a complex number has the same real part and a
1517 negated imaginary part. In other words, @samp{conj(a + bi) = a + -bi}.
1522 @deftypefun double carg (complex double @var{z})
1523 @deftypefunx float cargf (complex float @var{z})
1524 @deftypefunx {long double} cargl (complex long double @var{z})
1525 These functions return the argument of the complex number @var{z}.
1526 The argument of a complex number is the angle in the complex plane
1527 between the positive real axis and a line passing through zero and the
1528 number. This angle is measured in the usual fashion and ranges from @math{0}
1531 @code{carg} has a branch cut along the positive real axis.
1536 @deftypefun {complex double} cproj (complex double @var{z})
1537 @deftypefunx {complex float} cprojf (complex float @var{z})
1538 @deftypefunx {complex long double} cprojl (complex long double @var{z})
1539 These functions return the projection of the complex value @var{z} onto
1540 the Riemann sphere. Values with a infinite imaginary part are projected
1541 to positive infinity on the real axis, even if the real part is NaN. If
1542 the real part is infinite, the result is equivalent to
1545 INFINITY + I * copysign (0.0, cimag (z))
1549 @node Integer Division
1550 @section Integer Division
1551 @cindex integer division functions
1553 This section describes functions for performing integer division. These
1554 functions are redundant when GNU CC is used, because in GNU C the
1555 @samp{/} operator always rounds towards zero. But in other C
1556 implementations, @samp{/} may round differently with negative arguments.
1557 @code{div} and @code{ldiv} are useful because they specify how to round
1558 the quotient: towards zero. The remainder has the same sign as the
1561 These functions are specified to return a result @var{r} such that the value
1562 @code{@var{r}.quot*@var{denominator} + @var{r}.rem} equals
1566 To use these facilities, you should include the header file
1567 @file{stdlib.h} in your program.
1571 @deftp {Data Type} div_t
1572 This is a structure type used to hold the result returned by the @code{div}
1573 function. It has the following members:
1577 The quotient from the division.
1580 The remainder from the division.
1586 @deftypefun div_t div (int @var{numerator}, int @var{denominator})
1587 This function @code{div} computes the quotient and remainder from
1588 the division of @var{numerator} by @var{denominator}, returning the
1589 result in a structure of type @code{div_t}.
1591 If the result cannot be represented (as in a division by zero), the
1592 behavior is undefined.
1594 Here is an example, albeit not a very useful one.
1598 result = div (20, -6);
1602 Now @code{result.quot} is @code{-3} and @code{result.rem} is @code{2}.
1607 @deftp {Data Type} ldiv_t
1608 This is a structure type used to hold the result returned by the @code{ldiv}
1609 function. It has the following members:
1613 The quotient from the division.
1616 The remainder from the division.
1619 (This is identical to @code{div_t} except that the components are of
1620 type @code{long int} rather than @code{int}.)
1625 @deftypefun ldiv_t ldiv (long int @var{numerator}, long int @var{denominator})
1626 The @code{ldiv} function is similar to @code{div}, except that the
1627 arguments are of type @code{long int} and the result is returned as a
1628 structure of type @code{ldiv_t}.
1633 @deftp {Data Type} lldiv_t
1634 This is a structure type used to hold the result returned by the @code{lldiv}
1635 function. It has the following members:
1638 @item long long int quot
1639 The quotient from the division.
1641 @item long long int rem
1642 The remainder from the division.
1645 (This is identical to @code{div_t} except that the components are of
1646 type @code{long long int} rather than @code{int}.)
1651 @deftypefun lldiv_t lldiv (long long int @var{numerator}, long long int @var{denominator})
1652 The @code{lldiv} function is like the @code{div} function, but the
1653 arguments are of type @code{long long int} and the result is returned as
1654 a structure of type @code{lldiv_t}.
1656 The @code{lldiv} function was added in @w{ISO C 9x}.
1660 @node Parsing of Numbers
1661 @section Parsing of Numbers
1662 @cindex parsing numbers (in formatted input)
1663 @cindex converting strings to numbers
1664 @cindex number syntax, parsing
1665 @cindex syntax, for reading numbers
1667 This section describes functions for ``reading'' integer and
1668 floating-point numbers from a string. It may be more convenient in some
1669 cases to use @code{sscanf} or one of the related functions; see
1670 @ref{Formatted Input}. But often you can make a program more robust by
1671 finding the tokens in the string by hand, then converting the numbers
1675 * Parsing of Integers:: Functions for conversion of integer values.
1676 * Parsing of Floats:: Functions for conversion of floating-point
1680 @node Parsing of Integers
1681 @subsection Parsing of Integers
1684 These functions are declared in @file{stdlib.h}.
1688 @deftypefun {long int} strtol (const char *@var{string}, char **@var{tailptr}, int @var{base})
1689 The @code{strtol} (``string-to-long'') function converts the initial
1690 part of @var{string} to a signed integer, which is returned as a value
1691 of type @code{long int}.
1693 This function attempts to decompose @var{string} as follows:
1697 A (possibly empty) sequence of whitespace characters. Which characters
1698 are whitespace is determined by the @code{isspace} function
1699 (@pxref{Classification of Characters}). These are discarded.
1702 An optional plus or minus sign (@samp{+} or @samp{-}).
1705 A nonempty sequence of digits in the radix specified by @var{base}.
1707 If @var{base} is zero, decimal radix is assumed unless the series of
1708 digits begins with @samp{0} (specifying octal radix), or @samp{0x} or
1709 @samp{0X} (specifying hexadecimal radix); in other words, the same
1710 syntax used for integer constants in C.
1712 Otherwise @var{base} must have a value between @code{2} and @code{35}.
1713 If @var{base} is @code{16}, the digits may optionally be preceded by
1714 @samp{0x} or @samp{0X}. If base has no legal value the value returned
1715 is @code{0l} and the global variable @code{errno} is set to @code{EINVAL}.
1718 Any remaining characters in the string. If @var{tailptr} is not a null
1719 pointer, @code{strtol} stores a pointer to this tail in
1720 @code{*@var{tailptr}}.
1723 If the string is empty, contains only whitespace, or does not contain an
1724 initial substring that has the expected syntax for an integer in the
1725 specified @var{base}, no conversion is performed. In this case,
1726 @code{strtol} returns a value of zero and the value stored in
1727 @code{*@var{tailptr}} is the value of @var{string}.
1729 In a locale other than the standard @code{"C"} locale, this function
1730 may recognize additional implementation-dependent syntax.
1732 If the string has valid syntax for an integer but the value is not
1733 representable because of overflow, @code{strtol} returns either
1734 @code{LONG_MAX} or @code{LONG_MIN} (@pxref{Range of Type}), as
1735 appropriate for the sign of the value. It also sets @code{errno}
1736 to @code{ERANGE} to indicate there was overflow.
1738 You should not check for errors by examining the return value of
1739 @code{strtol}, because the string might be a valid representation of
1740 @code{0l}, @code{LONG_MAX}, or @code{LONG_MIN}. Instead, check whether
1741 @var{tailptr} points to what you expect after the number
1742 (e.g. @code{'\0'} if the string should end after the number). You also
1743 need to clear @var{errno} before the call and check it afterward, in
1744 case there was overflow.
1746 There is an example at the end of this section.
1751 @deftypefun {unsigned long int} strtoul (const char *@var{string}, char **@var{tailptr}, int @var{base})
1752 The @code{strtoul} (``string-to-unsigned-long'') function is like
1753 @code{strtol} except it returns an @code{unsigned long int} value. If
1754 the number has a leading @samp{-} sign, the return value is negated.
1755 The syntax is the same as described above for @code{strtol}. The value
1756 returned on overflow is @code{ULONG_MAX} (@pxref{Range of
1759 @code{strtoul} sets @var{errno} to @code{EINVAL} if @var{base} is out of
1760 range, or @code{ERANGE} on overflow.
1765 @deftypefun {long long int} strtoll (const char *@var{string}, char **@var{tailptr}, int @var{base})
1766 The @code{strtoll} function is like @code{strtol} except that it returns
1767 a @code{long long int} value, and accepts numbers with a correspondingly
1770 If the string has valid syntax for an integer but the value is not
1771 representable because of overflow, @code{strtoll} returns either
1772 @code{LONG_LONG_MAX} or @code{LONG_LONG_MIN} (@pxref{Range of Type}), as
1773 appropriate for the sign of the value. It also sets @code{errno} to
1774 @code{ERANGE} to indicate there was overflow.
1776 The @code{strtoll} function was introduced in @w{ISO C 9x}.
1781 @deftypefun {long long int} strtoq (const char *@var{string}, char **@var{tailptr}, int @var{base})
1782 @code{strtoq} (``string-to-quad-word'') is the BSD name for @code{strtoll}.
1787 @deftypefun {unsigned long long int} strtoull (const char *@var{string}, char **@var{tailptr}, int @var{base})
1788 The @code{strtoull} function is like @code{strtoul} except that it
1789 returns an @code{unsigned long long int}. The value returned on overflow
1790 is @code{ULONG_LONG_MAX} (@pxref{Range of Type}).
1792 The @code{strtoull} function was introduced in @w{ISO C 9x}.
1797 @deftypefun {unsigned long long int} strtouq (const char *@var{string}, char **@var{tailptr}, int @var{base})
1798 @code{strtouq} is the BSD name for @code{strtoull}.
1803 @deftypefun {long int} atol (const char *@var{string})
1804 This function is similar to the @code{strtol} function with a @var{base}
1805 argument of @code{10}, except that it need not detect overflow errors.
1806 The @code{atol} function is provided mostly for compatibility with
1807 existing code; using @code{strtol} is more robust.
1812 @deftypefun int atoi (const char *@var{string})
1813 This function is like @code{atol}, except that it returns an @code{int}.
1814 The @code{atoi} function is also considered obsolete; use @code{strtol}
1820 @deftypefun {long long int} atoll (const char *@var{string})
1821 This function is similar to @code{atol}, except it returns a @code{long
1824 The @code{atoll} function was introduced in @w{ISO C 9x}. It too is
1825 obsolete (despite having just been added); use @code{strtoll} instead.
1828 @c !!! please fact check this paragraph -zw
1833 @cindex parsing numbers and locales
1834 @cindex locales, parsing numbers and
1835 Some locales specify a printed syntax for numbers other than the one
1836 that these functions understand. If you need to read numbers formatted
1837 in some other locale, you can use the @code{strtoX_l} functions. Each
1838 of the @code{strtoX} functions has a counterpart with @samp{_l} added to
1839 its name. The @samp{_l} counterparts take an additional argument: a
1840 pointer to an @code{locale_t} structure, which describes how the numbers
1841 to be read are formatted. @xref{Locales}.
1843 @strong{Portability Note:} These functions are all GNU extensions. You
1844 can also use @code{scanf} or its relatives, which have the @samp{'} flag
1845 for parsing numeric input according to the current locale
1846 (@pxref{Numeric Input Conversions}). This feature is standard.
1848 Here is a function which parses a string as a sequence of integers and
1849 returns the sum of them:
1853 sum_ints_from_string (char *string)
1861 /* @r{Skip whitespace by hand, to detect the end.} */
1862 while (isspace (*string)) string++;
1866 /* @r{There is more nonwhitespace,} */
1867 /* @r{so it ought to be another number.} */
1870 next = strtol (string, &tail, 0);
1871 /* @r{Add it in, if not overflow.} */
1873 printf ("Overflow\n");
1876 /* @r{Advance past it.} */
1884 @node Parsing of Floats
1885 @subsection Parsing of Floats
1888 These functions are declared in @file{stdlib.h}.
1892 @deftypefun double strtod (const char *@var{string}, char **@var{tailptr})
1893 The @code{strtod} (``string-to-double'') function converts the initial
1894 part of @var{string} to a floating-point number, which is returned as a
1895 value of type @code{double}.
1897 This function attempts to decompose @var{string} as follows:
1901 A (possibly empty) sequence of whitespace characters. Which characters
1902 are whitespace is determined by the @code{isspace} function
1903 (@pxref{Classification of Characters}). These are discarded.
1906 An optional plus or minus sign (@samp{+} or @samp{-}).
1909 A nonempty sequence of digits optionally containing a decimal-point
1910 character---normally @samp{.}, but it depends on the locale
1911 (@pxref{General Numeric}).
1914 An optional exponent part, consisting of a character @samp{e} or
1915 @samp{E}, an optional sign, and a sequence of digits.
1918 Any remaining characters in the string. If @var{tailptr} is not a null
1919 pointer, a pointer to this tail of the string is stored in
1920 @code{*@var{tailptr}}.
1923 If the string is empty, contains only whitespace, or does not contain an
1924 initial substring that has the expected syntax for a floating-point
1925 number, no conversion is performed. In this case, @code{strtod} returns
1926 a value of zero and the value returned in @code{*@var{tailptr}} is the
1927 value of @var{string}.
1929 In a locale other than the standard @code{"C"} or @code{"POSIX"} locales,
1930 this function may recognize additional locale-dependent syntax.
1932 If the string has valid syntax for a floating-point number but the value
1933 is outside the range of a @code{double}, @code{strtod} will signal
1934 overflow or underflow as described in @ref{Math Error Reporting}.
1936 @code{strtod} recognizes four special input strings. The strings
1937 @code{"inf"} and @code{"infinity"} are converted to @math{@infinity{}},
1938 or to the largest representable value if the floating-point format
1939 doesn't support infinities. You can prepend a @code{"+"} or @code{"-"}
1940 to specify the sign. Case is ignored when scanning these strings.
1942 The strings @code{"nan"} and @code{"nan(@var{chars...})"} are converted
1943 to NaN. Again, case is ignored. If @var{chars...} are provided, they
1944 are used in some unspecified fashion to select a particular
1945 representation of NaN (there can be several).
1947 Since zero is a valid result as well as the value returned on error, you
1948 should check for errors in the same way as for @code{strtol}, by
1949 examining @var{errno} and @var{tailptr}.
1954 @deftypefun float strtof (const char *@var{string}, char **@var{tailptr})
1955 @deftypefunx {long double} strtold (const char *@var{string}, char **@var{tailptr})
1956 These functions are analogous to @code{strtod}, but return @code{float}
1957 and @code{long double} values respectively. They report errors in the
1958 same way as @code{strtod}. @code{strtof} can be substantially faster
1959 than @code{strtod}, but has less precision; conversely, @code{strtold}
1960 can be much slower but has more precision (on systems where @code{long
1961 double} is a separate type).
1963 These functions are GNU extensions.
1968 @deftypefun double atof (const char *@var{string})
1969 This function is similar to the @code{strtod} function, except that it
1970 need not detect overflow and underflow errors. The @code{atof} function
1971 is provided mostly for compatibility with existing code; using
1972 @code{strtod} is more robust.
1975 The GNU C library also provides @samp{_l} versions of thse functions,
1976 which take an additional argument, the locale to use in conversion.
1977 @xref{Parsing of Integers}.
1979 @node System V Number Conversion
1980 @section Old-fashioned System V number-to-string functions
1982 The old @w{System V} C library provided three functions to convert
1983 numbers to strings, with unusual and hard-to-use semantics. The GNU C
1984 library also provides these functions and some natural extensions.
1986 These functions are only available in glibc and on systems descended
1987 from AT&T Unix. Therefore, unless these functions do precisely what you
1988 need, it is better to use @code{sprintf}, which is standard.
1990 All these functions are defined in @file{stdlib.h}.
1993 @comment SVID, Unix98
1994 @deftypefun {char *} ecvt (double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg})
1995 The function @code{ecvt} converts the floating-point number @var{value}
1996 to a string with at most @var{ndigit} decimal digits.
1997 The returned string contains no decimal point or sign. The first
1998 digit of the string is non-zero (unless @var{value} is actually zero)
1999 and the last digit is rounded to nearest. @var{decpt} is set to the
2000 index in the string of the first digit after the decimal point.
2001 @var{neg} is set to a nonzero value if @var{value} is negative, zero
2004 The returned string is statically allocated and overwritten by each call
2007 If @var{value} is zero, it's implementation defined whether @var{decpt} is
2008 @code{0} or @code{1}.
2010 For example: @code{ecvt (12.3, 5, &decpt, &neg)} returns @code{"12300"}
2011 and sets @var{decpt} to @code{2} and @var{neg} to @code{0}.
2015 @comment SVID, Unix98
2016 @deftypefun {char *} fcvt (double @var{value}, int @var{ndigit}, int @var{decpt}, int *@var{neg})
2017 The function @code{fcvt} is like @code{ecvt}, but @var{ndigit} specifies
2018 the number of digits after the decimal point. If @var{ndigit} is less
2019 than zero, @var{value} is rounded to the @math{@var{ndigit}+1}'th place to the
2020 left of the decimal point. For example, if @var{ndigit} is @code{-1},
2021 @var{value} will be rounded to the nearest 10. If @var{ndigit} is
2022 negative and larger than the number of digits to the left of the decimal
2023 point in @var{value}, @var{value} will be rounded to one significant digit.
2025 The returned string is statically allocated and overwritten by each call
2030 @comment SVID, Unix98
2031 @deftypefun {char *} gcvt (double @var{value}, int @var{ndigit}, char *@var{buf})
2032 @code{gcvt} is functionally equivalent to @samp{sprintf(buf, "%*g",
2033 ndigit, value}. It is provided only for compatibility's sake. It
2037 As extensions, the GNU C library provides versions of these three
2038 functions that take @code{long double} arguments.
2042 @deftypefun {char *} qecvt (long double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg})
2043 This function is equivalent to @code{ecvt} except that it
2044 takes a @code{long double} for the first parameter.
2049 @deftypefun {char *} qfcvt (long double @var{value}, int @var{ndigit}, int @var{decpt}, int *@var{neg})
2050 This function is equivalent to @code{fcvt} except that it
2051 takes a @code{long double} for the first parameter.
2056 @deftypefun {char *} qgcvt (long double @var{value}, int @var{ndigit}, char *@var{buf})
2057 This function is equivalent to @code{gcvt} except that it
2058 takes a @code{long double} for the first parameter.
2063 The @code{ecvt} and @code{fcvt} functions, and their @code{long double}
2064 equivalents, all return a string located in a static buffer which is
2065 overwritten by the next call to the function. The GNU C library
2066 provides another set of extended functions which write the converted
2067 string into a user-supplied buffer. These have the conventional
2070 @code{gcvt_r} is not necessary, because @code{gcvt} already uses a
2071 user-supplied buffer.
2075 @deftypefun {char *} ecvt_r (double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg}, char *@var{buf}, size_t @var{len})
2076 The @code{ecvt_r} function is the same as @code{ecvt}, except
2077 that it places its result into the user-specified buffer pointed to by
2078 @var{buf}, with length @var{len}.
2080 This function is a GNU extension.
2084 @comment SVID, Unix98
2085 @deftypefun {char *} fcvt_r (double @var{value}, int @var{ndigit}, int @var{decpt}, int *@var{neg}, char *@var{buf}, size_t @var{len})
2086 The @code{fcvt_r} function is the same as @code{fcvt}, except
2087 that it places its result into the user-specified buffer pointed to by
2088 @var{buf}, with length @var{len}.
2090 This function is a GNU extension.
2095 @deftypefun {char *} qecvt_r (long double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg}, char *@var{buf}, size_t @var{len})
2096 The @code{qecvt_r} function is the same as @code{qecvt}, except
2097 that it places its result into the user-specified buffer pointed to by
2098 @var{buf}, with length @var{len}.
2100 This function is a GNU extension.
2105 @deftypefun {char *} qfcvt_r (long double @var{value}, int @var{ndigit}, int @var{decpt}, int *@var{neg}, char *@var{buf}, size_t @var{len})
2106 The @code{qfcvt_r} function is the same as @code{qfcvt}, except
2107 that it places its result into the user-specified buffer pointed to by
2108 @var{buf}, with length @var{len}.
2110 This function is a GNU extension.