相关疑难解决方法(0)

检查C++中的double(或float)是否为NaN

是否有isnan()函数?

PS.:我在MinGW(如果这有所不同).

我不得不用isnan()从此解决了<math.h>,这是不存在的<cmath>,这让我#include在第一荷兰国际集团.

c++ double nan

358
推荐指数
12
解决办法
35万
查看次数

如何制作便携式isnan/isinf功能

我一直在使用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)

c c++ math function

37
推荐指数
6
解决办法
4万
查看次数

负NaN不是NaN?

在编写一些测试用例时,有些测试会检查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的相等性?

c++ nan fast-math

29
推荐指数
2
解决办法
9713
查看次数

isnan与`-Ofast`标志无法正常工作

考虑以下测试程序:

# 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() …

c++ gsl

2
推荐指数
1
解决办法
546
查看次数

标签 统计

c++ ×4

nan ×2

c ×1

double ×1

fast-math ×1

function ×1

gsl ×1

math ×1