本文介绍了如何在C ++ 11 constexpr中检查double的位模式为0x0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查给定的double / float变量是否具有实际的位模式0x0。不要问为什么,它用在Qt( qIsNull())的函数中,我想成为 constexpr

I want to check that a given double/float variable has the actual bit pattern 0x0. Don't ask why, it's used in a function in Qt (qIsNull()) that I'd like to be constexpr.

原始代码使用了联合:

union { double d; int64_t i; } u;
u.d = d;
return u.i == 0;

这不能用作 constexpr 当然。

下一次尝试是使用 reinterpret_cast

return *reinterpret_cast<int64_t*>(&d) == 0;

但是虽然在 constexpr 中GCC 4.7,它在Clang 3.1中失败了(正确的说,是指针操作的b / c)。

But while that works as a constexpr in GCC 4.7, it fails (rightfully, b/c of pointer manipulation) in Clang 3.1.

最后的想法是去Alexandrescuesque并这样做:

The final idea was to go Alexandrescuesque and do this:

template <typename T1, typename T2>
union Converter {
    T1 t1;
    T2 t2;
    explicit constexpr Converter( T1 t1 ) : t1(t1) {}
    constexpr operator T2() const { return t2; }
};

// in qIsNull():
return Converter<double,int64_t>(d);

但这对Clang来说还不够聪明,或者:

But that's not clever enough for Clang, either:

note: read of member 't2' of union with active member 't1' is not allowed in a constant expression
constexpr operator T2() const { return t2; }
                                       ^

其他人有个好主意吗?

推荐答案

但是,如果它是 constexpr ,则它没有检查任何变量,而是检查了 value ,该变量是静态确定要保留的。这就是为什么您不应该使用指针和联合技巧的原因,官方地没有任何指向的记忆。

But if it's constexpr then it's not checking any variable, it's checking the value that this variable is statically determined to hold. That's why you aren't supposed to pull pointer and union tricks, "officially" there isn't any memory to point at.

如果可以说服您的实现去做非陷印IEEE除以零,那么您可以执行以下操作:

If you can persuade your implementation to do non-trapping IEEE division-by-zero, then you could do something like:

return (d == 0) && (1 / d > 0)

+/- 0 等于0。 1 / -0 -Inf ,不大于0。 1 / + 0 + Inf 。但是我不知道如何使这种非诱捕算法发生。

Only +/-0 are equal to 0. 1/-0 is -Inf, which isn't greater than 0. 1/+0 is +Inf, which is. But I don't know how to make that non-trapping arithmetic happen.

这篇关于如何在C ++ 11 constexpr中检查double的位模式为0x0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 08:00