Comparison between floats... can it be dangerous?

A

Andrea B

Hi everybody,

a guy told me during a job interview that we can't compare directly 2
floats, because in very rare situations we can get wrong results.
I googled it, with no result.
Do you know something about that?

Bye,
Andrea
 
J

Jonathan Mcdougall

Andrea said:
Hi everybody,

a guy told me during a job interview that we can't compare directly 2
floats, because in very rare situations we can get wrong results.
I googled it, with no result.

Funny, I just googled "float comparisons" and I got some pretty good
hits.


Jonathan
 
A

Andrea B

Jonathan said:
Andrea B wrote: [...]
Funny, I just googled "float comparisons" and I got some pretty good
hits.

Alright, I've found the generic problem of float comparison (related to
machine precision), but I haven't found the 'standard' solution to this
problem in C++.
To avoid instability I'd write a function like:

bool AlmostEqualRelative(float A, float B, float maxRelativeError)
{
if (A == B)
return true;
float relativeError = fabs((A - B) / B);
if (relativeError <= maxRelativeError)
return true;
return false;
}

But that's not the best solution.
Is there a std library providing some useful functions?

Thank you,
bye
 
R

Rolf Magnus

Andrea said:
Jonathan said:
Andrea B wrote: [...]
Funny, I just googled "float comparisons" and I got some pretty good
hits.

Alright, I've found the generic problem of float comparison (related to
machine precision), but I haven't found the 'standard' solution to this
problem in C++.

That's because there is no standard solution.
To avoid instability I'd write a function like:

bool AlmostEqualRelative(float A, float B, float maxRelativeError)
{
if (A == B)
return true;
float relativeError = fabs((A - B) / B);
if (relativeError <= maxRelativeError)
return true;
return false;
}

But that's not the best solution.

Then write a better one.
Is there a std library providing some useful functions?

No.
 
N

noone

Hi everybody,

a guy told me during a job interview that we can't compare directly 2
floats, because in very rare situations we can get wrong results. I
googled it, with no result.
Do you know something about that?

yup...Because floating point numbers in computers are generally
approximations you have to think about how close two numbers are to each
other to decide if they are equivalent. what amount of error is
allowable?

Is (35.400003456 == 35.400003457) ?

Well, it is if you are only concerned about the first 8 digits of the
fractional part. The first number is definitely less than the second
number but can they be (considered) equal for your intended purpose?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top