Created
July 8, 2013 11:03
-
-
Save sumanth232/739d2dca6b17cd416145 to your computer and use it in GitHub Desktop.
Comparing double variables considering the the very important precision
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). | |
// Another better insight | |
/* 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