5 ms·
I would like to see a spaceship operator for floating point comparison, where a<=>b means a is less than, equal to, or greater than b. It would be useful for ch
by idea1434 9y ago
I would like to see a spaceship operator for floating point comparison, where a<=>b means a is less than, equal to, or greater than b. It would be useful for checking that a and b aren't NaN. If either were NaN, then the comparison would return false.
- Joky 9y agoBasically adding an operator to replace the existing `std::is_nan()`? Not sure if this deserves to be first class in the language...
- stochastic_monk 9y agoPlus then you couldn't use it for </>/== at the same time like you'd want to and relative meanings for different types would be inconsistent. I think this idea is a nonstarter.
- idea1434 9y agoNo, it would be the same as evaluating (a<b)&&(a==b)&&(a>b), not std::is_nan.
- slavik81 9y agoA special syntax seems unnecessary. Just drop something like this in your project: template<typename A, typename B> bool is_unordered(const A& a, const B& b) { return !(a<b||a==b||a>b); } edit: changed implementation to match name.
- idea1434 9y agoIt just makes more sense. Similar to how a<=b is equivalent to (a<b)&&(a==b). Coming up with an entirely fresh meaning for <=> involving spaceships and a conglomerated comparative threesome just seems illogical to me, given the original meanings of the component symbols of <=>.
- slavik81 9y agoThis operator already exists in Ruby, Perl and a couple other languages. It's also incredibly useful, allowing six functions to be replaced by one. Or, rather, with zero, because in 99.9% of cases you will just be able to declare the signature with '= default;' and let the compiler generate the implementation. Then you have all relational operators available for that type. It makes defining new types much, much easier. Plus, it provides a place to statically define what sort of ordering the type has, so further compile-time optimizations can be done. In contrast, <=> as is_ordered is nearly useless. It would basically only be used for floats, where !(isnan(a)||isnan(b)) is both equivalent and IMO more clear.
- verall 9y ago(a<=b) is equivalent to ((a<b)||(a==b)) ((a<b)&&(a==b)) is equivalent to (false)
- LeifCarrotson 9y agoYou mean: (a<b)||(a==b)||(a>b) Double ampersand is logical AND, you want double bars for logical OR. And still, that's equivalent to !(std::is_nan()), unless you're suggesting using the operator on other non-floating-point types which have a similar problem...
- pleasecalllater 9y agoSpecial operator for one type? Please no.
- idea1434 9y agoNo, it could be used for any type. It would be equivalent to (a<b)&&(a==b)&&(a>b), but would be most useful for floating point, where this operator would evaluate to false if a or b is NaN.
- pleasecalllater 9y agoNope, this operator will always evaluate to false. Show me any pair of numbers, which is not NaN, which will be at the same time, equal, smaller, and greater. Of course I can imagine such a set, and operations... (I have a little bit of a mathematical background) but for programming it will be quite useless. Why not `is_nan()` insted?
- daemin 9y agoNo, the operator is like the same operator in Ruby. You define an operator<=> for one or two types and then whenever you use one of the normal comparison operators in code (say a < b), the compiler replaces it with a<=>b < 0.
- pavlov 9y agoWell, this is C++, so the spaceship operator can be overloaded for all sorts of clever purposes. For example, when comparing strings, you could use it to coerce the arguments into spaceship objects, then run a little Space War simulation to lighten the user's day. Just another day of C++ operator overloading.
- WalterBright 9y agoThe NCEG (Numerical C Extensions Group) proposed a complete set of floating point operators for all combinations of NAN and <, ==, and >. I implemented them in Digital Mars C++, and nobody used them. I also implemented them in the D programming language, and there is simply no interest in it. https://dlang.org/spec/expression.html#floating-point-comparisons https://dlang.org/spec/expression.html#floating-point-compar...
- jstimpfle 9y agoI researched a little bit into to this question - this might be interesting: https://stackoverflow.com/a/43773235/1073695 https://stackoverflow.com/a/43773235/1073695
- jzwinck 9y agoIf you want to check whether either of two values is NaN, rejoice, it is now optimized to a single instruction on x86 by GCC 6 and Clang 3.7: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63387 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63387 You write isnan(x)||isnan(y) but the compilers now optimize the whole thing to one instruction. If you want specific syntax you can use the builtin unordered(x, y).