union float_ieee{
int i;
float f;
};
int is_nanf(float x){
#if 0
int t;
union float_ieee e;
e.f=x;
t=e.i&0x7fffffff;
t=0x7f800000-t;
return ((unsigned)t>>31);
#endif
return x!=x;
}
其中第二种方法 return x!=x; 有什么原理吗?查了半天没找到,这是怎么回事呢?
1
thinkIn OP 看来还是要 google 啊。
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 Floating Point Classes). In addition, <, >, <=, and >= will raise an exception when applied to NaNs. -----https://www.gnu.org/software/libc/manual/html_node/Infinity-and-NaN.html#Infinity-and-NaN |
2
nashge 2015-11-12 18:45:36 +08:00
知道浮点数在计算机中的表示,就能理解上面一段代码了
|