INFINITY的一个坑

float a=INFINITY;

if(a==INFINITY){

  cout<<"a is inf"<<endl;

}else{

  cout<<"a is not inf"<<endl;

}

float b=-INFINITY;

if(b==-INFINITY){

  cout<<"b is -inf"<<endl;

}else{

  cout<<"b is not -inf"<<endl;

}

两个输出都是非预期(或不确定)的。

为了程序安全,永远不要使用INFINITY(或(numeric_limits<float>::max)())做求相反数或比较大小或加减乘等运算。

换句话说,永远不要在自己的代码中使用INFINITY(或(numeric_limits<float>::max)()),而要使用自定义的myInf,例如

#define myInf (0.5*(numeric_limits<float>::max)())

原文地址:https://www.cnblogs.com/wantnon/p/4464562.html