我正在观看“使用 C++20 三路比较 - Jonathan Müller - Meeting C++ 2019”演讲,其中提到了包含浮点成员的类的问题。
问题源于这样一个事实:涉及 NaN 的 IEEE 754 比较很奇怪,并且不提供总排序。
Talk 提供了解决此问题的方法,例如使用强顺序 https://en.cppreference.com/w/cpp/utility/compare/strong_order或者在实现 时手动忽略 NaN 值(假设值永远不是 NaN)。
我的问题是是否有一些库包装器可以让我说“我保证”我的浮点数永远不会是 NaNor这将对浮点数进行缓慢但有效的比较(较慢但更安全,因为 NaN 现在是有序的)。我的目标是通过使成员漂浮宇宙飞船友好来避免手动实现宇宙飞船(这样我可以默认宇宙飞船)。
使用演讲中的例子:
// original class
struct Temperature{
double value;
};
struct TemperatureNoNan{
std::a_number<double> value; // I promise value will never be NaN
// Now spaceship defaulting works
};
struct TemperatureStrongO{
std::s_ordered<double> value; // I want strong ordering(2 diff NaNs are not the same)
// Now spaceship defaulting works
};
“我保证”我的浮点数永远不会是 NaN
template <std::floating_point T>
struct NeverNaN {
T val;
constexpr NeverNaN(T val) : val(val) { }
constexpr operator T() const { return val; }
constexpr bool operator==(NeverNaN const&) const = default;
constexpr std::strong_ordering operator<=>(NeverNaN const& rhs) const {
auto c = val <=> rhs.val;
assert(c != std::partial_ordering::unordered);
return c > 0 ? std::strong_ordering::greater :
c < 0 ? std::strong_ordering::less :
std::strong_ordering::equal;
}
};
不幸的是,没有什么好方法可以“提升”这样的比较类别。而且目前优化还不是很好。
这将对浮点数进行缓慢但有效的比较(较慢但更安全,因为 NaN 现在是有序的)
这个有特定的库支持,通过以下任一方式std::strong_order()
or std::weak_order()
[cmp.alg] http://eel.is/c++draft/cmp.alg取决于您想要哪种比较:
template <std::floating_point T>
struct TotallyOrdered {
T val;
constexpr TotallyOrdered(T val) : val(val) { }
constexpr operator T() const { return val; }
// depends on whether or not you want == NaN to still be false?
// might need to be: return (*this <=> rhs) == 0;
constexpr bool operator==(TotallyOrdered const&) const = default;
constexpr auto operator<=>(TotallyOrdered const& rhs) const {
return std::strong_order(val, rhs.val);
// ... or std::weak_order(val, rhs.val)
}
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)