typeid(double) does not respect NaNs

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Jul 8 20:02:01 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=7836

Upon closer inspection, however, it seems that this may have been
deliberate??? Here's the code from rt/typeinfo/ti_double.d:

    class TypeInfo_d : TypeInfo
    {
        // ...

        static bool _equals(double f1, double f2)
        {
            return f1 == f2 ||
                    (f1 !<>= f1 && f2 !<>= f2);
        }

        static int _compare(double d1, double d2)
        {
            if (d1 !<>= d2)         // if either are NaN
            {
                if (d1 !<>= d1)
                {
                    if (d2 !<>= d2)
                        return 0;
                    return -1;
                }
                return 1;
            }
            return (d1 == d2) ? 0 : ((d1 < d2) ? -1 : 1);
        }

        // ...
    }

It seems as though NaNs are explicitly being checked for, and _equals is
made to return true when comparing two NaNs, and some black magic is
being used to determine the sign of the comparison in _compare.

Why is this? Isn't this a violation of the IEEE floating-point spec??

More to the point, *if* typeinfo isn't meant to match == for whatever
reason, then why is it being used as the standard of comparison for AA
keys?


T

-- 
Don't modify spaghetti code unless you can eat the consequences.


More information about the Digitalmars-d mailing list