我一直在使用isinf
,isnan
在Linux平台上运行完美的功能.但是这在OS-X上不起作用,所以我决定使用std::isinf
std::isnan
哪种适用于Linux和OS-X.
但英特尔编译器无法识别它,我猜它是英特尔编译器中的一个错误,根据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 …
Run Code Online (Sandbox Code Playgroud) 在编写一些测试用例时,有些测试会检查NaN的结果.
我尝试使用std::isnan
但断言错误:
Assertion `std::isnan(x)' failed.
Run Code Online (Sandbox Code Playgroud)
打印出值后x
,结果显示它是负NaN(-nan
),在我的情况下是完全可以接受的.
在尝试使用NaN != NaN
和使用的事实之后assert(x == x)
,编译器给我一个'恩惠'并优化断言.
制作我自己的isNaN
功能也正在优化.
如何检查NaN 和 -NaN的相等性?
考虑以下测试程序:
# include <gsl/gsl_statistics_double.h>
# include <iostream>
using namespace std;
int main()
{
double y = 50.2944, yc = 63.2128;
double pearson_corr = gsl_stats_correlation(&y, 1, &yc, 1, 1);
cout << "pearson_corr = " << pearson_corr << endl;
if (isnan(pearson_corr))
cout << "It is nan" << endl;
else
cout << "Not nan" << endl;
}
Run Code Online (Sandbox Code Playgroud)
在某种程度上,这段代码有些荒谬,但它的目的是显示我遇到的一个微妙的错误.
调用gsl_stats_correlation()
应该给出错误,因为样本数是1并且皮尔逊系数对于至少两个样本是有意义的.
当我编译时:
c++ test-r2.cc -lgsl -lgslcblas
Run Code Online (Sandbox Code Playgroud)
程序打印出-nan
结果和消息"它是南",我认为是正确的,因为正如我所说,不可能计算系数.isnan()
正确检测结果的调用nan
.但是,当我编译时:
c++ -Ofast test-r2.cc -lgsl -lgslcblas
Run Code Online (Sandbox Code Playgroud)
该程序打印出-nan
结果但消息"Not nan",这表明调用isnan() …