MSVC 上的 std::isfinite [英] std::isfinite on MSVC

查看:30
本文介绍了MSVC 上的 std::isfinite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C++11 和 C11 标准定义了std::isfinite功能.Visual Studio 2012 似乎没有提供它作为cmathmath.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.

isfinite 是否可以与 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.hisfinite> 将在 C++ AMP 中使用,这是一个 MS 扩展,用于在类似于 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)来自 ,这是一个 MS - 至少从 VS 2003 开始支持特定功能.但请记住,_finite 只接受 double 并因此转换任何非 double 参数(尽管 VC 不会'似乎没有一个合适的 long double 无论如何),以及它的所有含义(而 INFs 和 quiet NaNs 应该毫无问题地转换,我不确定转换中对信号 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.)

除此之外,检查 INFs 和 NaNs 的直接方法也可能有效:

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 的可能陷阱具有相同的含义(尽管我不得不承认我并不精通信号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).

这篇关于MSVC 上的 std::isfinite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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