6 ms·
Silly me. As far as I can remember I've always used epsilon when comparing floats.
by FpUser 4y ago
Silly me. As far as I can remember I've always used epsilon when comparing floats.
- eru 4y agoThat's not a general solution. (And if you know what you are doing, you can also compare floats exactly in some circumstances.) But the article already mentions that.
- kccqzy 4y agoAbsolutely. I was once refactoring some badly written floating point code. To make sure I did not screw up, I want the result of the old and new code to be identical, since I know the refactoring should not change the float operations. (I also knew the code won't produce NaNs so == suffices.)
- k0k0r0 4y agoOh, yes... This. Had a project like this not long ago. NaNs almost killed me. The code used complex numbers, and you know who their userbase is. Anyway, exact floating point comparisons made all of this possible. In the process my coworker and found something we believed to be a bug with complex nans in some recent GCC versions, but where not smart enough to understand whether it is actually a bug or undefined behaviour in the standart. Anyway, we where so exhausted from the refactor, we didn't follow it up again.
- FpUser 4y ago>"And if you know what you are doing" I knew what I was doing and that is why using Epsilon was the most reliable thing in my data processing algos.
- eru 4y agoHence the 'in some circumstances'.
- FpUser 4y agoI can not recall when did I ever need to do "if X=Y" without actually needing to account for tolerances.
- eru 4y agoYour experience seems a bit limited then? Obviously, you need to account for tolerances, but that accounting does not need to be via an epsilon in your count, it can also take the form of careful numerical analysis. For example, in some numerical algorithms, it is right and proper to run them until an equality test (without epsilon) succeeds. And using any positive epsilon would give a worse result.