C++中的nan和inf

nan和inf的产生

nan 代表不是一个数字(not a number), inf代表无穷大(infinity)

IEEE 754 的一些规定:

‘Invalid Operation’

This exception is raised if the given operands are invalid for the operation to be performed. Examples are (see IEEE 754, section 7):

Addition or subtraction: ∞ - ∞. (But ∞ + ∞ = ∞).
Multiplication: 0 · ∞.
Division: 0/0 or ∞/∞.
Remainder: x REM y, where y is zero or x is infinite.
Square root if the operand is less than zero. More generally, any mathematical function evaluated outside its domain produces this exception.
Conversion of a floating-point number to an integer or decimal string, when the number cannot be represented in the target format (due to overflow, infinity, or NaN).
Conversion of an unrecognizable input string.
Comparison via predicates involving < or >, when one or other of the operands is NaN. You can prevent this exception by using the unordered comparison functions instead; see FP Comparison Functions.
If the exception does not trap, the result of the operation is NaN.

‘Division by Zero’

This exception is raised when a finite nonzero number is divided by zero. If no trap occurs the result is either +∞ or -∞, depending on the signs of the operands.

‘Overflow’

This exception is raised whenever the result cannot be represented as a finite value in the precision format of the destination. If no trap occurs the result depends on the sign of the intermediate result and the current rounding mode (IEEE 754, section 7.3):

Round to nearest carries all overflows to ∞ with the sign of the intermediate result.
Round toward 0 carries all overflows to the largest representable finite number with the sign of the intermediate result.
Round toward -∞ carries positive overflows to the largest representable finite number and negative overflows to -∞.
Round toward ∞ carries negative overflows to the most negative representable finite number and positive overflows to ∞.
Whenever the overflow exception is raised, the inexact exception is also raised.

‘Underflow’

The underflow exception is raised when an intermediate result is too small to be calculated accurately, or if the operation’s result rounded to the destination precision is too small to be normalized.

When no trap is installed for the underflow exception, underflow is signaled (via the underflow flag) only when both tininess and loss of accuracy have been detected. If no trap handler is installed the operation continues with an imprecise small value, or zero if the destination precision cannot hold the small exact result.

‘Inexact’

This exception is signalled if a rounded result is not exact (such as when calculating the square root of two) or a result overflows without an overflow trap.

nan和inf的运算

n + inf = inf; // 任意数 + 无穷大 = 无穷大
n / inf = 0; // 任意数 / 无穷大 = 0
atan(inf) = π // 对无穷大取反正切,结果为π
任意数和nan运算结果都为nan
nan不大于,不小于,也不等于任意数(包括自身)
inf除了自身及nan,正无穷大大于一切,负无穷大小于一切

nan和inf的判断

c++可以使用中的std::isinf(x)和std::isnan(x)来分别判断inf和nan。

参考

http://www.gnu.org/software/libc/manual/html_node/FP-Exceptions.html
http://www.gnu.org/software/libc/manual/html_node/Infinity-and-NaN.html
https://stackoverflow.com/questions/570669/checking-if-a-double-or-float-is-nan-in-c
https://stackoverflow.com/questions/4095337/how-to-check-for-inf-and-or-nan-in-a-double-variable
https://stackoverflow.com/questions/16691207/c-c-nan-constant-literal