如何制作可移植的 isnan/isinf 函数

how do I make a portable isnan/isinf function(如何制作可移植的 isnan/isinf 函数)
本文介绍了如何制作可移植的 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 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why does C++ compilation take so long?(为什么 C++ 编译需要这么长时间?)
Why is my program slow when looping over exactly 8192 elements?(为什么我的程序在循环 8192 个元素时很慢?)
C++ performance challenge: integer to std::string conversion(C++ 性能挑战:整数到 std::string 的转换)
Fast textfile reading in c++(在 C++ 中快速读取文本文件)
Is it better to use std::memcpy() or std::copy() in terms to performance?(就性能而言,使用 std::memcpy() 或 std::copy() 更好吗?)
Does the C++ standard mandate poor performance for iostreams, or am I just dealing with a poor implementation?(C++ 标准是否要求 iostreams 性能不佳,或者我只是在处理一个糟糕的实现?)