Created
July 8, 2013 18:39
-
-
Save sumanth232/ab4784067c5f88d6012e to your computer and use it in GitHub Desktop.
Taking care of Precision in calculations involving floating point numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
When I need to compare double values, I just make a function: | |
int CompareDouble(double a, double b) | |
{ | |
if(a < b - EPS) | |
return -1; | |
if(a > b + EPS) | |
return 1; | |
return 0; | |
} | |
So, instead of (a <= b), I use (CompareDouble(a, b) <= 0). | |
The rule of thumb is: if the difference is less than EPS, the values are equal. | |
So, if your formula contains "a < b" (less), then you write "a < b - EPS". And if it contains "a <= b" (less or equal), then you write "a < b + EPS". |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://apps.topcoder.com/forums/?module=Thread&threadID=507829&mc=8#516283