IEEE 754 floating point numbers can represent positive or negative infinity, and NaN (not a number). These three values arise from calculations whose result is undefined or cannot be represented accurately. You can also deliberately set a floating-point variable to any of them, which is sometimes useful. Some examples of calculations that produce infinity or NaN:
@ifnottex
@math{1/0 = @infinity{}} @math{log (0) = -@infinity{}} @math{sqrt (-1) = NaN}
When a calculation produces any of these values, an exception also occurs; see section FP Exceptions.
The basic operations and math functions all accept infinity and NaN and produce sensible output. Infinities propagate through calculations as one would expect: for example, @math{2 + @infinity{} = @infinity{}}, @math{4/@infinity{} = 0}, atan @math{(@infinity{}) = @pi{}/2}. NaN, on the other hand, infects any calculation that involves it. Unless the calculation would produce the same result no matter what real value replaced NaN, the result is NaN.
In comparison operations, positive infinity is larger than all values
except itself and NaN, and negative infinity is smaller than all values
except itself and NaN. NaN is unordered: it is not equal to,
greater than, or less than anything, including itself. x ==
x
is false if the value of x
is NaN. You can use this to test
whether a value is NaN or not, but the recommended way to test for NaN
is with the isnan
function (see section Floating-Point Number Classification Functions). In
addition, <
, >
, <=
, and >=
will raise an
exception when applied to NaNs.
`math.h' defines macros that allow you to explicitly set a variable to infinity or NaN.
1.0 / 0.0
.
-INFINITY
represents negative infinity.
You can test whether a floating-point value is infinite by comparing it
to this macro. However, this is not recommended; you should use the
isfinite
macro instead. See section Floating-Point Number Classification Functions.
This macro was introduced in the ISO C99 standard.
You can use `#ifdef NAN' to test whether the machine supports
NaN. (Of course, you must arrange for GNU extensions to be visible,
such as by defining _GNU_SOURCE
, and then you must include
`math.h'.)
IEEE 754 also allows for another unusual value: negative zero. This
value is produced when you divide a positive number by negative
infinity, or when a negative result is smaller than the limits of
representation. Negative zero behaves identically to zero in all
calculations, unless you explicitly test the sign bit with
signbit
or copysign
.
Go to the first, previous, next, last section, table of contents.