logoalt Hacker News

SideQuarktoday at 11:09 AM2 repliesview on HN

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


Replies

mizmartoday at 3:22 PM

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;
    }
fn-motetoday at 11:57 AM

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