Posts

Showing posts with the label precision

Floating point equality

Floating point equality It is common knowledge that one has to be careful when comparing floating point values. Usually, instead of using == , we use some epsilon or ULP based equality testing. == However, I wonder, are there any cases, when using == is perfectly fine? == Look at this simple snippet, which cases are guaranteed to succeed? void fn(float a, float b) { float l1 = a/b; float l2 = a/b; if (l1==l1) { } // case a) if (l1==l2) { } // case b) if (l1==a/b) { } // case c) if (l1==5.0f/3.0f) { } // case d) } int main() { fn(5.0f, 3.0f); } Note: I've checked this and this, but they don't cover (all of) my cases. Note2: It seems that I have to add some plus information, so answers can be useful in practice: I'd like to know: This is the only relevant statement I found in the current draft standard: The value representation of floating-point types is implementation-defined. [ Note: This document imposes no requirements on t...