I was watching "Using C++20 three way comparison - Jonathan Müller - Meeting C++ 2019" talk and it mentioned problems with classes that contain floating point members.
Problem comes from the fact that IEEE 754 comparisons involving NaN(s) are weird and do not provide total ordering. Talk gives a way to work around this problems, for example using strong_order or manually ignoring NaN values when implementing <=> (assuming that values are never NaN).
My questions is if there are some library wrappers that would enable me to say that "I promise" that my floats are never NaN or that would do slow but valid comparisons on floats(slower but safer since NaNs are now ordered). My goal is to avoid manual implementation of spaceship by making member float spaceship friendly(so I can default spaceship).
Using example from the talk:
// 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
};
bit_cast
ing to a uint of the same size if both arguments are NaNs, or something. – Khotanoperator<=>
for types that containfloat
s. They simply can't be strongly ordered; they'll have to be weakly ordered. What's the problem? Do you specifically need to have a strong order? – Scopoline