# #QNAN、1.#IND和1.#INF等“无效”浮点数说明及其判断

此处的1.#QNAN是一个打印呈现，QNAN是指Quiet Not a Number，类似的浮点错误还有SNaN（Signaling Not a Number），通常如0.0/0.0、给负数开平方根等溢出或无效运算就会产生一个NAN结果的表示值，NaN及两种浮点错误的说明如下：
The value NaN (Not a Number) is used to represent a value that does not represent a real number. NaN’s are represented by a bit pattern with an exponent of all 1s and a non-zero fraction. There are two categories of NaN: QNaN (Quiet NaN) and SNaN (Signalling NaN).
A QNaN is a NaN with the most significant fraction bit set. QNaN’s propagate freely through most arithmetic operations. These values pop out of an operation when the result is not mathematically defined.
An SNaN is a NaN with the most significant fraction bit clear. It is used to signal an exception when used in operations. SNaN’s can be handy to assign to uninitialized variables to trap premature usage.
Semantically, QNaN’s denote indeterminate operations, while SNaN’s denote invalid operations. If a return value is a QNaN, it means that it is impossible to determine the result of the operation, a SNaN means that the operation is invalid.
这样的特殊浮点数还有INF和IND：INF就是Infinity，表示一个无穷大的数，包括正无穷和负无穷；IND则表示无限小，但不确定。如1.0/0.0会产生一个INF无穷大，-1.0/0.0会产生一个负无穷大。

回到最初的错误上，这种无效数据之所以通过了经纬度有效值判断，是因为常规的浮点范围对这类浮点数无效。我们知道常规的浮点数不能直接判0等于，而如果这个浮点数为NaN，那if (f==f) 会得到false结果。但是利用这个结果来判断一个浮点数是否为NaN并不总是安全的，例如用这个宏定义#define isnan(x) ((x) != (x)) ，在某些编译环境下可能就会脱离了你的预期。那如何判断特殊的浮点数呢？各种语言应该都能找到对应的辅助函数或者类方法，在C语言中，可以用float.h中的int _isnan(double x)、int _finite(double x)、int _fpclass(double x)函数来判断，返回结果代表意义分别为：_FPCLASS_SNAN (Signaling NaN)、_FPCLASS_QNAN (Quiet NaN)、_FPCLASS_NINF (Negative Infinity, –INF)、_FPCLASS_PINF (Positive Infinity, +INF)；在C++中，可以用STL中的limits类：numeric_limits::quiet_NaN()、numeric_limits::signaling_NaN()、numeric_limits::infinity()等方法。

http://www.ibm.com/developerworks/cn/java/j-jtp0114/
http://steve.hollasch.net/cgindex/coding/ieeefloat.html
http://www.topcoder.com/tc?module=Static&d1=tutorials&d2=integersReals2