c++ - 如何制作可移植 isnan/isinf 函数

标签 c++ c math function

我一直在 Linux 平台上使用 isinfisnan 函数,效果很好。 但这在 OS-X 上不起作用,所以我决定使用在 Linux 和 OS-X 上都可以使用的 std::isinf std::isnan

但英特尔编译器无法识别它,根据 http://software.intel.com/en-us/forums/showthread.php?t=64188,我猜它是英特尔编译器中的一个错误。

所以现在我只想避免麻烦,定义自己的isinfisnan实现。

有谁知道如何做到这一点?

编辑:

我最终在我的源代码中这样做以使 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/

相关文章:

c - 执行openwrt交叉编译程序出错

ctime 给出不正确的输出

.net - 在对象列表中搜索所有属性

c++ - 将无理分数转换为一组四个真/假分数

c++ - 无法让复制文件工作

c++ - 如何从 C++ 中的 pgm 文件中读取数据

javascript - 计算第二行的 Y1,它是第一行大小的一半

c - mod C 的问题

c++ - 转换参数包类型

c++ - 执行 AES 解密后字符串末尾的 Crypto++ 和垃圾