常用的浮点数INF&NaN判断方法:
Windows中,使用_isnan判断NaN情况;使用!_isnan && !_finite判断INF情况;使用!_finite判断NaN和INF两种情况
//0/0的情况下或得到NAN,非零除以零会得到INF
double spread=0.0;
spread/=0.0;
if (_isnan(spread))
{
cout<<"not a number"<
依据以下特性也可自己实现判断:
1. NaN是唯一与自身不等的浮点数类型,所以IsNan可以用 x != x来判断。Anything compared with NAN is false, so NAN == NAN is false.
2. INF阶码全1,尾数全0
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;
}