Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Is this the best way to check if two floating point numbers are equal, or close to being equal?

template <class T>
bool IsEqual(T rhs, T lhs)
{
    T diff = std::abs(lhs - rhs);

    T epsilon = std::numeric_limits<T>::epsilon( ) * std::max(std::abs(rhs), std::abs(lhs));

    return diff <= epsilon ;
}
share|improve this question
    
So I should consider: 100000000000000000 and 100000000000000020 to be the same number? –  Loki Astari Oct 26 '12 at 14:23
    
PS. Typo inside std::max() you have two rhs. –  Loki Astari Oct 26 '12 at 14:24
1  
@LokiAstari With floating point numbers, they are not accurate and the farther away you get from 1 the less accurate you get. Therefore, I'm trying to check if they are equal with some reason. To answer your question, yes those two values should be equal. But at the same time, I don't want 1 == 20 –  Caesar Oct 26 '12 at 14:42
1  
Correct. And if that is what you need fine. But it is not what most people are going to need. I would use @William Morris solution as this would be more normal usage as i do want to distinguish larger numbers. –  Loki Astari Oct 26 '12 at 15:40
    
@LokiAstari The thing is that you can't guarantee that your large numbers are stored accurately. –  Caesar Oct 26 '12 at 15:49

2 Answers 2

up vote 2 down vote accepted
+50

No, not really. You want a third parameter that gives an acceptable difference -- this can be number of decimals, percent of value, or a fixed value, but it needs to be coming from outside to really be useful.

A function that does it with a constant diff, might be useful in some limited circumstances, but not generally.

share|improve this answer

Seems unlikely. epsilon from std::numeric_limits is the smallest increment representable by the type (around the value 1).

You want to check for something "close to" equal, but you don't say what close to means for you. Assuming it to be a few multiples of epsilon, the following check would seem reasonable:

const int FEW = 10;
T diff = std::abs(lhs - rhs);
T epsilon = std::numeric_limits<T>::epsilon();

return diff < (epsilon * FEW);
share|improve this answer
1  
From my understanding, floating point numbers get less and less accurate the further away you get from 1. While you code is fine, it is only gone work for a small lower range of floating point numbers. –  Caesar Oct 26 '12 at 14:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.