[Issue 13663] Comparison of Tuples with floating point fields

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Dec 4 07:46:06 UTC 2019


https://issues.dlang.org/show_bug.cgi?id=13663

Simen Kjaeraas <simen.kjaras at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
                 CC|                            |simen.kjaras at gmail.com
         Resolution|WONTFIX                     |---

--- Comment #3 from Simen Kjaeraas <simen.kjaras at gmail.com> ---
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.

--


More information about the Digitalmars-d-bugs mailing list