A Tuple with only component nan is greater than itself. unittest { import std.typecons; real x; auto t = tuple(x); assert (t > t); assert (!(t < t)); }
There is nothing that can be done here. For user defined types (e.g. Tuple), comparison operators are converted to calls to opCmp, i.e. a < b a.opCmp(b) < 0 a <= b a.opCmp(b) <= 0 a > b a.opCmp(b) > 0 a >= b a.opCmp(b) >= 0 For NaN vs NaN, the comparison is neither equal, less than, nor greater than. There is nothing that opCmp can return to give the desired semantics.
I cannot even see a place, where this can be clearified in the docs. Maybe in some very general place, like some page on floating point numbers in general.
Peter's claims in comment 1 are plain false - opCmp can return float, and float.nan for incomparable cases. Here's an implementation of opCmp that does that: float opCmp(R)(R rhs) if (areCompatibleTuples!(typeof(this), R, "<")) { static foreach (i; 0 .. Types.length) { if (field[i] != field[i] || rhs.field[i] != rhs.field[i]) { return float.nan; } if (field[i] != rhs.field[i]) { return field[i] < rhs.field[i] ? -1 : 1; } } return 0; } /// ditto float opCmp(R)(R rhs) const if (areCompatibleTuples!(typeof(this), R, "<")) { static foreach (i; 0 .. Types.length) { if (field[i] != field[i] || rhs.field[i] != rhs.field[i]) { return float.nan; } if (field[i] != rhs.field[i]) { return field[i] < rhs.field[i] ? -1 : 1; } } return 0; } These are taken directly from std.typecons, and the only change made is they return float, and check if any of the fields are incomparable.
(In reply to Simen Kjaeraas from comment #3) > opCmp can return float Oh, I didn't know that trick with returning nan. I'll check your solution and add a PR.
@berni44 created dlang/phobos pull request #7301 "Fix Issue 13663 - Comparison of Tuples with floating point fields" fixing this issue: - Fix Issue 13663 - Comparison of Tuples with floating point fields https://github.com/dlang/phobos/pull/7301
*** Issue 18832 has been marked as a duplicate of this issue. ***
@berni44 created dlang/phobos pull request #7748 "Fix Issue 13663 - Comparison of Tuples with floating point fields" fixing this issue: - Fix Issue 13663 - Comparison of Tuples with floating point fields https://github.com/dlang/phobos/pull/7748
dlang/phobos pull request #7748 "Fix Issue 13663 - Comparison of Tuples with floating point fields" was merged into master: - ab2f7cdef4d80964a85440699b358fb95b50b26f by Bernhard Seckinger: Fix Issue 13663 - Comparison of Tuples with floating point fields https://github.com/dlang/phobos/pull/7748