Remix.run Logo
SideQuark 10 hours ago

Completely worked out at least 20 years ago: https://www.lomont.org/papers/2005/CompareFloat.pdf

fn-mote 9 hours ago | parent | next [-]

Note for the skeptic: this cites Knuth, Volume II, writes out the IEEE edge cases, and optimizes.

mizmar 6 hours ago | parent | prev [-]

Great reading, thanks. Describes how to handle ±0, works with difference to avoid truncation errors. First half of the paper is arriving at this correct snippet, second part of the paper is about optimizing it.

    bool DawsonCompare(float af, float bf, int maxDiff)
    {
        int ai = *reinterpret_cast<int*>(&af);
        int bi = *reinterpret_cast<int*>(&bf);
        if (ai < 0)
            ai = 0x80000000 - ai;
        if (bi < 0)
            bi = 0x80000000 - bi;
        int diff = ai - bi;
        if (abs(diff) < maxDiff)
            return true;
        return false;
    }