Skip to content

Instantly share code, notes, and snippets.

@sumanth232
Created July 8, 2013 18:39
Show Gist options
  • Save sumanth232/ab4784067c5f88d6012e to your computer and use it in GitHub Desktop.
Save sumanth232/ab4784067c5f88d6012e to your computer and use it in GitHub Desktop.
Taking care of Precision in calculations involving floating point numbers
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