我一直在 Linux 平台上使用 isinf
、isnan
函数,效果很好。
但这在 OS-X 上不起作用,所以我决定使用在 Linux 和 OS-X 上都可以使用的 std::isinf
std::isnan
。
但英特尔编译器无法识别它,根据 http://software.intel.com/en-us/forums/showthread.php?t=64188,我猜它是英特尔编译器中的一个错误。
所以现在我只想避免麻烦,定义自己的isinf
、isnan
实现。
有谁知道如何做到这一点?
编辑:
我最终在我的源代码中这样做以使 isinf
/isnan
工作
#include <iostream>
#include <cmath>
#ifdef __INTEL_COMPILER
#include <mathimf.h>
#endif
int isnan_local(double x) {
#ifdef __INTEL_COMPILER
return isnan(x);
#else
return std::isnan(x);
#endif
}
int isinf_local(double x) {
#ifdef __INTEL_COMPILER
return isinf(x);
#else
return std::isinf(x);
#endif
}
int myChk(double a){
std::cerr<<"val is: "<<a <<"\t";
if(isnan_local(a))
std::cerr<<"program says isnan";
if(isinf_local(a))
std::cerr<<"program says isinf";
std::cerr<<"\n";
return 0;
}
int main(){
double a = 0;
myChk(a);
myChk(log(a));
myChk(-log(a));
myChk(0/log(a));
myChk(log(a)/log(a));
return 0;
}
最佳答案
你也可以使用 boost 来完成这个任务:
#include <boost/math/special_functions/fpclassify.hpp> // isnan
if( boost::math::isnan( ... ) .... )
关于c++ - 如何制作可移植 isnan/isinf 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2249110/