Home > database >  Do we need epsilon value for lesser or greater comparison for float value?
Do we need epsilon value for lesser or greater comparison for float value?

Time:05-18

I have gone through different threads for comparing lesser or greater float value not equal comparison but not clear do we need epsilon value logic to compare lesser or greater float value as well?

e.g ->

float a, b;
 if (a < b) // is this correct way to compare two float value or we need epsilon value for lesser comparator 
{
}
if (a > b) // is this correct way to compare two float value for greater comparator
{
}

I know for comparing for equality of float, we need some epsilon value 

bool AreSame(double a, double b)
{
    return fabs(a - b) < EPSILON;
}

CodePudding user response:

When you are working with floats, it's inevitable that you will run into precision errors.

In order to mitigate this, when checking for the equality two floats we often check if their difference is small enough.

For lesser and greater, however, there is no way to tell with full certainty which float is larger. The best (presumably for your intentions) approach is to first check if the two floats are the same, using the areSame function. If so return false (as a = b implies that a < b and a > b are both false).

Otherwise, return the value of either a < b or a > b.

CodePudding user response:

The answer is application dependent.

If you are sure that a and b are sufficiently different that numerical errors will not reverse the order, then a < b is good enough.

But if a and b are dangerously close, you might require a < b EPSILON. In such a case, it should be clear to you that < and ≤ are not distinguishable.

Needless to say, EPSILON should be chosen with the greatest care (which is often pretty difficult).

CodePudding user response:

As others have stated, there would always be precision errors when dealing with floats.

Thus, you should have an epsilon value even for comparing less than / greater than.

We know that in order for a to be less than b, firstly, a must be different from b. Checking this is a simple NOT equals, which uses the epsilon. Then, once you already know a != b, the operator < is sufficient.

CodePudding user response:

It really depends on what should happen when both value are close enough to be seen as equal, meaning fabs(a - b) < EPSILON. In some use cases (for example for computing statistics), it is not very important if the comparison between 2 close values gives or not equality.

If it matters, you should first determine the uncertainty of the values. It really depends on the use case (where the input values come from and how they are processed), and then 2 value differing by less than that uncertainty should be considered as equal. But that equality is not longer a true mathematical equivalence relation: you can easily imagine how to build a chain a close values between 2 truely different values. In math words, the relation is not transitive (or is almost transitive is current language words).

I am sorry but as soon as you have to process approximations there cannot be any precise and consistent way: you have to think of the real world use case to determine how you should handle the approximation.

  • Related