Operating System - HP-UX
1823015 Members
3790 Online
109645 Solutions
New Discussion юеВ

Testing for equal values in c

 
SOLVED
Go to solution
Mary Rice
Frequent Advisor

Testing for equal values in c

Hello everybody,

I read in a thread earlier today that it was bad to test two floating point values for equality. Can someone please explain this more fully? I have been working on a lot a Informix ESQL/C code that has many comparisons between double values.

TIA, Mary
4 REPLIES 4
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Testing for equal values in c

Hi Mary:

I suppose that you are referring to this thread: http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0xb87550011d20d6118ff40090279cd0f9,00.html

The answer is yes is it very bad for the same reason that 2 * 3 = 5.9999999999 on some calculators. The problem is that internally floating-point values are represented in base-2 logarithms of finite precision but you want to see the results in base 10. There is not an exact correspondence between these values.

To make matters worse, there is even a negative zero and a positive zero and these compare differently as well. In this case, the only difference between the two is the sign bit but they are different values.

I suppose an example is in order:
double a = 1.25, b = 1.25;

BAD:
if (a == b)
{
printf("a equals b\n");
}
else
{
printf("a not equal to b\n");
}

This code will work sometimes and sometimes not depending upon the values compared especially when you really intended to test
1.24999999999 and 1.250000000001. You probably meant for these to evaluate equal but they will not. The fix is to do something like this:

#define SMALL_NUMBER 1.0e-6

if (fabs(a - b) <= SMALL_NUMBER)
{
printf("a equals b (more or less)\n");
}
else
{
printf("a not equal to b\n");
};

This stuff is pretty standard technique for dealing with floating-point values and I assure you that if you don't do this now you will do it later.

Food for thought, Clay

P. S. If you like, I can dig up one of my standard functions, nearlyequal(), which does just what its name implies.




If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Testing for equal values in c

Hi again Mary:

I've attached a small int function nearlyequal() to help you.

Use if like this:

double a = 1.249999999, b = 1.25000000001;

if (nearlyequal(a,b))
{
printf("a equals b\n");
}
else
{
printf("a not equal to b\n");
}

The function returns 1 if the values are equal (or nearly so) and 0 otherwise.

Regards, Clay
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Testing for equal values in c

Hi again Mary:

Ooops ... I'm stupid, I missed the attachment.

I've attached a small int function nearlyequal() to help you.


Use if like this:

double a = 1.249999999, b = 1.25000000001;

if (nearlyequal(a,b))
{
printf("a equals b\n");
}
else
{
printf("a not equal to b\n");
}

The function returns 1 if the values are equal (or nearly so) and 0 otherwise.

Regards, Clay
If it ain't broke, I can fix that.
Thomas Kollig
Trusted Contributor

Re: Testing for equal values in c

Hi Mary,

all floats or doubles have a fintie precision. If you are doing numerical calculations all calculations can introduce small errors since these calculation are done only with finite precision, too. Thus if you are testing two floats for equality these two floats can have different values although theoretically they are equal if all calculations are done without error. Therefore a so-called epsilon-test (see Clay's attachment) is used for testing in numerical calculations.

By the way you can find in many books about numerics techniques to deal with errors introduced by the finite precision calculations. I'm doing a lot numerical simulations and handling floats in a wrong way can cost a lot of time.

Bye,
-Thomas