Skip to content

Instantly share code, notes, and snippets.

@sumanth232
Created July 8, 2013 11:03
Show Gist options
  • Save sumanth232/739d2dca6b17cd416145 to your computer and use it in GitHub Desktop.
Save sumanth232/739d2dca6b17cd416145 to your computer and use it in GitHub Desktop.
Comparing double variables considering the the very important precision
//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