在MSVC上std :: isfinite

在MSVC上std :: isfinite

问题描述:

C ++ 11和C11标准定义了
std: :isfinite
函数。 Visual Studio 2012似乎并未将其作为
cmath math.h 的一部分提供,但具有 amp_math.h ,其中
似乎提供了此功能

The C++11 and C11 standard define the std::isfinite function. Visual Studio 2012 doesn't seem to provide it as part of the cmath or math.h, but has amp_math.h which seems to provide this function.

是有限的是否可以与 std :: isfinite 互换?
文档没有讨论用 NAN
调用时的行为,而且我没有VS编译器对此进行测试。

Is the isfinite interchangeable with std::isfinite? The documentation doesn't talk about the behavior when called with NAN and I don't have a VS compiler to test this.

正如 Marius 所指出的,是有限的 amp_math.h >将用于C ++ AMP,这是 MS 的扩展,用于在类似于CUDA或CUDA的多核体系结构上进行并行计算OpenCL的。而且由于此功能只能在实际的AMP限制功能(通常是GPU内核)中使用,因此对您而言并没有太多的用途。

As Marius has already pointed out, the isfinite from amp_math.h is to be used in C++ AMP, which is an MS extension for parallel computing on many-core architectures similar to CUDA or OpenCL. And since this function can only be used in actual AMP restricted functions (usually GPU kernels) it won't be of much general use for you.

不幸的是 VS 2012 不支持C ++ 11数学和浮点控制功能。但是,一旦您意识到自己使用的是 VC 并为其实现了特殊代码,就可以使用 _finite (或更确切地说是!_ finite ),来自< float .h> ,这是自 VS 2003 起受支持的 MS 安全功能。但是请记住, _finite 只需要 double s,因此可以转换任何非 double 参数(尽管 VC 似乎没有正确的 long double ),其所有含义(而 INF s和安静的 NaN s应该没有问题地转换,我不确定是否在信号 NaN 也可能是直接调用 std :: finite )造成的。

Unfortunately VS 2012 doesn't support the C++11 math and floating point control functions. But once you recognize that you are on VC and implement special code for it, you can just use _finite (or rather !_finite) from <float.h>, which is an MS-secific function supported since at least VS 2003. But keep in mind that _finite only takes doubles and thus converts any non-double arguments (though VC doesn't seem to have a proper long double anyway), with all its implications (while INFs and quiet NaNs should be converted without problem, I'm not sure if the trapping on a signalling NaN in the conversion would also have resulted from a direct call to std::finite).

VC 的标准库具有其他此类功能以适应缺乏C ++ 11 / C99支持(例如 _isnan 之类)。 (为什么他们拒绝只删除这些功能前面的下划线,并在 _controlfp 周围放置一个简单的< cfenv> 包装器>,因此更加接近完整的C ++ 11支持是一个完全不同的问题。)

VC's standard library has other such functions to accomodate for their lack of C++11/C99 support (like _isnan and the like). (Why they refuse to just remove that underscore in front of those functions and put a simple <cfenv> wrapper around _controlfp and thus get a bit nearer to complete C++11 support is a totally different question though.)

编辑:除此之外,检查 INF s和 NaN s的简单方法也可能有效:

Other than that, the straight-forward approach for checking INFs and NaNs might also work:

template<typename T> bool isfinite(T arg)
{
    return arg == arg && 
           arg != std::numeric_limits<T>::infinity() &&
           arg != -std::numeric_limits<T>::infinity();
}

但当然也有可能暗示 NaN s(尽管我不得不承认,我对信号 NaN s和浮点异常在一般)。

But of course with the same implications of probably trapping for signalling NaNs (though I have to admit I'm not that well-versed in the intricacies of signalling NaNs and floating point exceptions in general).