如何制作可移植的 isnan/isinf 函数 [英] how do I make a portable isnan/isinf function

查看:56
本文介绍了如何制作可移植的 isnan/isinf 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在 Linux 平台上使用 isinfisnan 函数,效果很好.但这在 OS-X 上不起作用,所以我决定使用 std::isinf std::isnan,它适用于 Linux 和 OS-X.>

但英特尔编译器无法识别它,根据 http://software.intel.com/en-us/forums/showthread.php?t=64188

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

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

为了使 isinf/isnan 工作

,我最终在我的源代码中这样做了

#include #include <cmath>#ifdef __INTEL_COMPILER#include #万一int isnan_local(双x){#ifdef __INTEL_COMPILER返回 isnan(x);#别的返回 std::isnan(x);#万一}int isinf_local(双x){#ifdef __INTEL_COMPILER返回 isinf(x);#别的返回 std::isinf(x);#万一}int myChk(double a){std::cerr<<"val 是:"<<a <<"	";如果(isnan_local(a))std::cerr<<"程序说 isnan";如果(isinf_local(a))std::cerr<<"程序说 isinf";std::cerr<<"
";返回0;}int main(){双 a = 0;myChk(a);myChk(log(a));myChk(-log(a));myChk(0/log(a));myChk(log(a)/log(a));返回0;}

解决方案

你也可以使用 boost 来完成这个任务:

#include //伊斯南if( boost::math::isnan( ... ) .... )

I've been using isinf, isnan functions on Linux platforms which worked perfectly. But this didn't work on OS-X, so I decided to use std::isinf std::isnan which works on both Linux and OS-X.

But the Intel compiler doesn't recognize it, and I guess its a bug in the intel compiler according to http://software.intel.com/en-us/forums/showthread.php?t=64188

So now I just want to avoid the hassle and define my own isinf, isnan implementation.

Does anyone know how this could be done?

edit:

I ended up doing this in my source code for making isinf/isnan working

#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 <<"	";
  if(isnan_local(a))
    std::cerr<<"program says isnan";
  if(isinf_local(a))
    std::cerr<<"program says isinf";
  std::cerr<<"
";
  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;
}

解决方案

You could also use boost for this task:

#include <boost/math/special_functions/fpclassify.hpp> // isnan

if( boost::math::isnan( ... ) .... )

这篇关于如何制作可移植的 isnan/isinf 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆