3 ms·
The frst rule of floating point comparison is you do not compare them for equality, but instead calculate the difference and check if the difference is less tha
by NullPrefix 7y ago
The frst rule of floating point comparison is you do not compare them for equality, but instead calculate the difference and check if the difference is less than epsilon.
- ska 7y agoThis is super common advice but it is generally wrong, at least the second part. Comparing floats is more subtle than most programmers realize, and there really isn't a one-size-fits-all solution. Things to consider due to the nature of fp representation - comparing results close to zero is different (i.e. "is a small" needs a differen test than "are a & b close" - the distance between fp numbers depends on their magnitude, so comparing two large numbers to each other shouldn't have the same bounds as comparing two numbers near 1, say[1]. - if you aren't quite careful you can easily create tests where a == b but b != a , which can cause sorting issues, etc. Hand-wavily speaking if you want to do this "right", you should probably look at doing the analysis in ULP (units in last place) rather than directly on the floats. Don't do it for values near zero though. And have a fast path for differently signed values. The above doesn't even get into denormalized values. [1] note that what people usually mean for epsilon is the version of machine epsilon that is the difference between 1 and the next representable float above 1 [2]. So by definition this is smaller than the representable difference between any two numbers in larger decades [2] MS .NET somewhat confusingly defines Epsilon as the smallest representable normalized number.