参考:
http://www.cplusplus.com/reference/cmath/isnan/
http://technet.microsoft.com/zh-cn/tzthab44(v=vs.95)
http://blog.163.com/chen_dawn/blog/static/112506320125494852135/
http://blog.csdn.net/zhang11wu4/article/details/7781099
http://baike.baidu.com/view/1634738.htm?fr=aladdin
http://bbs.csdn.net/topics/110121822
http://blog.csdn.net/sunmenggmail/article/details/7853081
http://stackoverflow.com/questions/570669/checking-if-a-double-or-float-is-nan-in-c
Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu 转载请标明来源
在一些情况会出现无效的浮点数,例如除0,例如负数求平方根等,像这类情况,获取到的浮点数的值是无效的。
NaN 即 Not a Number 非数字
INF 即 Infinite 无穷大
通常无效浮点数的内存表示方法是:
根据IEEE 754标准:
阶码全1,尾数全0表示无穷大INF。例如1.0/0.0
阶码全1,尾数非全0的表示无效数NaN。例如:求负数的平方根,例如0.0/0.0。
(注意: int型时除0是非法的,产生运行异常。Remember,integer division by 0 causes a runtime exception.)
float f = 0; (4字节 1位符号位,8位指数,23位小数,指数偏移127)
*(UINT*)&f = 0x7F800000L;//正无穷(二进制 [0111 1111] [1000 0000] [0000 0000] [0000 0000])
*(UINT*)&f = 0xFF800000L;//负无穷(二进制 [1111 1111] [1000 0000] [0000 0000] [0000 0000])
double var = 0; (8字节 1位符号位,11位指数,52位小数,指数偏移1023)
// 因为通常是little endian,所以通常修改其前后4个字节
UINT* pVar = ((UINT*)&var) + 1;
*(pVar) = 0x7FF00000L;//正无穷
*(pVar) = 0xFFF00000L;//负无穷
1. Windows中,使用_isnan判断NaN情况;使用!_isnan && !_finite判断INF情况;使用!_finite判断NaN和INF两种情况
_isnan //it returns a nonzero value if the argument x is a NaN;
_finite //It returns 0 if the argument is infinite or a NaN.
2.Linux中,使用insnan和isinf两个函数分别判别两种情况:
isNan
isInf
3.也可自己来写:
int isNaN(double x) {return x != x; }
#define _INF_DEFINE 1.0/0.0
int isInf(double n)
{
static double pinf = 0.0;
static double ninf = 0.0;
if (pinf == 0.0) {
pinf = _INF_DEFINE;
ninf = -pinf;
}
return memcmp(&n, &pinf, sizeof(n)) == 0
|| memcmp(&n, &ninf, sizeof(n)) == 0;
}
依据:
1. NaN是唯一与自身不等的浮点数类型,所以IsNan可以用 x != x来判断。Anything compared with NAN is false, so NAN == NAN is false.
2. INF阶码全1,尾数全0
还有一种借用isNan定义INF的方法
int isInf(double x) {return !isNaN(x) && isnan(x-x);}
依据:
1. Inf – Inf得到的为NaN
#include "stdio.h" void ViewHex(double x) { unsigned int* pUInt = ((unsigned int*)(&x)); printf("0x%08X ", *pUInt); pUInt++; printf("0x%08X\n", *pUInt); } int main() { double a = 1.0/0.0; double b = 0.0/0.0; double c = a-a; printf("%f\n%f\n%f\n", a, b, c); printf("%f\n%f\n%f\n", -a, -b, -c); ViewHex(a); ViewHex(b); ViewHex(c); ViewHex(-a); ViewHex(-b); ViewHex(-c); getchar(); return 0; }
// 测试输出(运行环境: window 编译程序:mingw/gcc)
inf
nan
nan
-inf
nan
nan
0x00000000 0x7FF00000
0x00000000 0xFFF80000
0x00000000 0xFFF80000
0x00000000 0xFFF00000
0x00000000 0x7FF80000
0x00000000 0x7FF80000