5.2 infinity and Nan
IEEE 754 floating point numbers can represent positive or negative infinity, andNan(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:
1/0 = & infin; log (0) =-& infin; SQRT (-1) = Nan
When a calculation produces any of these values, an exception also occurs; see FP exceptions.
the basic operations and math functions all accept infinity and Nan and produce sensible output. infinities propagate through calculations as one wowould country CT: for example, 2 + & infin; = & infin;, 4/& infin; = 0, atan (& infin;) = & PI; /2. nan, on the other hand, infects any calculation that involves it. unless the calculation wocould 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 between T itself and Nan, and negative infinity is smaller than all values between T itself and Nan. Nan isUnordered: It is not equal to, greater than, or less than anything,Including itself.X = x
Is false if the valueX
Is Nan. You can use this to test whether a value is Nan or not, but the recommended way to test for Nan is withIsnan
Function (see floating point classes). In addition,<
,>
,<=
, And> =
Will raise an exception when applied to nans.
Math. hDefines macros that allow you to explicitly set a variable to infinity or Nan.
-Macro: Float
Infinity
An expression representing positive infinity. It is equal to the value produced by mathematical operations like1.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 shocould useIsfinite
Macro instead. See floating point classes.
This macro was introduced in the ISO c99 standard.
-Macro: Float
Nan
an expression representing a value which is "not a number ". this macro is a GNU extension, available only on machines that support the "Not a number" value-that is to say, on all machines that support IEEE floating point.
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 bitSignbit
OrCopysign
.
Http://www.linuxtopia.org/online_books/programming_books/gnu_libc_guide/Infinity-and-NaN.html