4 ms·
Completely worked out at least 20 years ago: https://www.lomont.org/papers/2005/CompareFloat.pdf https://www.lomont.org/papers/2005/CompareFloat.pdf
by SideQuark 6mo ago
Completely worked out at least 20 years ago: https://www.lomont.org/papers/2005/CompareFloat.pdf https://www.lomont.org/papers/2005/CompareFloat.pdf
- fn-mote 6mo agoNote for the skeptic: this cites Knuth, Volume II, writes out the IEEE edge cases, and optimizes.
- mizmar 6mo agoGreat 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; }