compare two float values

N

neha_chhatre

can anybody tell me how to compare two float values
say for example
t and check are two variables declared float how to compare t and
check
please help me as soon as possible
 
J

Joachim Schmitz

can anybody tell me how to compare two float values
say for example
t and check are two variables declared float how to compare t and
check
please help me as soon as possible
float t, check;
....
if ( t > check)
printf("%f is greater than %f\n" t, check);
else
printf("%f is less than %f\n" t, check);

Don't compare for equality, won't work for float (or double), rather compare
the difference against some delta that suits your needs for accuracy

Bye, Jojo
 
J

Joachim Schmitz

Joachim said:
float t, check;
...
if ( t > check)
printf("%f is greater than %f\n" t, check);
else
printf("%f is less than %f\n" t, check);

Don't compare for equality, won't work for float (or double), rather
compare the difference against some delta that suits your needs for
accuracy
oops, epsilon, not delta, off by one in greek alphabeth...
 
N

neha_chhatre

actually i want to check wether two float values are equal or not
equal
i.e suppose t and t1 are two float values and i want to check wether t
and t1 are equal or not

please send in syntax form as in code form

please tell me how to do,
 
F

fred.l.kleinschmidt

float t, check;
...
if ( t > check)
    printf("%f is greater than %f\n" t, check);
else
    printf("%f is less than %f\n" t, check);

Don't compare for equality, won't work for float (or double), rather compare
the difference against some delta that suits your needs for accuracy

Bye, Jojo

Of course it will work:
if ( t == check ) {...}
WILL execute the block if the float value 't' is equal to the
float value 'check'.

It depends on how the values for 't' and 'check' were assigned
whether this is accurate enough to provide the desired results.
For example:

t = 10.0;
check = 5.0 + 5.0;

The above test will work, since the two values will
be identical.

However, for:
t = 11.0;
check = 10 * 1.1;

the above comparison (t==check), will most likely NOT work
to determine whether these are mathematically identical.


Bottom line: using the == operator will always check for
exact equality of the two entities. However, either of the
two entities being checked (or both) may not represent the
exact mathematical value desired, and will probably
differ if their calcualtion and assignment were not
identical.
 
M

Mark Bluemel

actually i want to check wether two float values are equal or not
equal
i.e suppose t and t1 are two float values and i want to check wether t
and t1 are equal or not

please send in syntax form as in code form

Smells like homework to me...

have you looked at the FAQ (http://www.c-faq.com)? There's a whole
section on floating point numbers, and question 14.5 is probably
close to what you want.
 
J

Joachim Schmitz

Of course it will work:
if ( t == check ) {...}
WILL execute the block if the float value 't' is equal to the
float value 'check'.

It depends on how the values for 't' and 'check' were assigned
whether this is accurate enough to provide the desired results.
For example:

t = 10.0;
check = 5.0 + 5.0;

The above test will work, since the two values will
be identical.

However, for:
t = 11.0;
check = 10 * 1.1;

the above comparison (t==check), will most likely NOT work
to determine whether these are mathematically identical.


Bottom line: using the == operator will always check for
exact equality of the two entities. However, either of the
two entities being checked (or both) may not represent the
exact mathematical value desired, and will probably
differ if their calcualtion and assignment were not
identical.
That's what I meant.

Bye, Jojo
 
K

Kenneth Brody

actually i want to check wether two float values are equal or not
equal
i.e suppose t and t1 are two float values and i want to check wether t
and t1 are equal or not

please send in syntax form as in code form

please tell me how to do,

How would you check for equality if t and t1 were "int"?

How would you check for equality if t and t1 were "short"?

How would you check for equality if t and t1 were "long"?

Now, how would you check for equality if t and t1 were "float"?

Or did you have something else in mind for what "equal" means?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
S

santosh

actually i want to check wether two float values are equal or not
equal
i.e suppose t and t1 are two float values and i want to check wether t
and t1 are equal or not

please send in syntax form as in code form

please tell me how to do,

Please quote relevant parts of the post to which you are replying. This
is good practise on Usenet.

Comparing floats for equality is fraught with danger as many values are
not perfectly representible in binary floating point. You might find
that you need to allow for a small variation on either side of the
comparison value, just like the margin of error in scientific
experiments.

You might want to read:

<http://www.cygnus-software.com/papers/comparingfloats/Comparing floating point numbers.htm>
<http://docs.sun.com/source/806-3568/ncg_goldberg.html>
 
P

Pietro Cerutti

Kenneth said:
How would you check for equality if t and t1 were "int"?

How would you check for equality if t and t1 were "short"?

How would you check for equality if t and t1 were "long"?

Now, how would you check for equality if t and t1 were "float"?

In a different way than above....
 
P

Pietro Cerutti

Kenneth said:
How would you check for equality if t and t1 were "int"?

How would you check for equality if t and t1 were "short"?

How would you check for equality if t and t1 were "long"?

Now, how would you check for equality if t and t1 were "float"?

In a different way than above....
 
C

CBFalconer

actually i want to check wether two float values are equal or not
equal. i.e suppose t and t1 are two float values and i want to
check wether t and t1 are equal or not

#include <float.h> /* FLT_EPSILON */
#include <math.h> /* fabsf() */

int floatequal(float t, float t1) {

float criterion;

criterion = (fabsf(t) + fabsf(t1)) * FLT_EPSILON;
if (fabsf(t - t1) > criterion) return 0; /* not equal */
else return 1; /* equal */
} /* untested */
 
E

Eric Sosman

CBFalconer said:
#include <float.h> /* FLT_EPSILON */
#include <math.h> /* fabsf() */

int floatequal(float t, float t1) {

float criterion;

criterion = (fabsf(t) + fabsf(t1)) * FLT_EPSILON;
if (fabsf(t - t1) > criterion) return 0; /* not equal */
else return 1; /* equal */
} /* untested */

Needs a little more work for NaNs and infinities ...

More to the point, the choice of a proper epsilon is
not dictated solely by the machine's precision, but also
by the computations that produced the numbers. For example,
if you calculate a binomial coefficient as

exp(lgamma(n)-lgamma(m)-lgamma(n-m))*n/(m*(n-m))

.... the accuracy of the result will quite likely depend a
good deal on the accuracies of the four transcendental
function evaluations, as well as on the fundamental precision
of the floating-point representation. An "approximately
equal" comparison based solely on the machine's epsilon might
lead one to conclude that the number five-card poker hands
(52 choose 5 == 52!/(5!*47!) == 2598960) is not an integer!
 
U

user923005

can anybody tell me how to compare two float values
say for example
t and check are two variables declared float how to compare t and
check
please help me as soon as possible

This should be a FAQ.

#include <float.h>
#include <math.h>

int double_compare (double d1, double d2)
{
if (d1 > d2)
if ((d1 - d2) < fabs (d1 * DBL_EPSILON))
return 0;
else
return 1;
if (d1 < d2)
if ((d2 - d1) < fabs (d2 * DBL_EPSILON))
return 0;
else
return -1;
return 0;
}

int float_compare (float d1, float d2)
{
if (d1 > d2)
if ((d1 - d2) < fabsf (d1 * FLT_EPSILON))
return 0;
else
return 1;
if (d1 < d2)
if ((d2 - d1) < fabsf (d2 * FLT_EPSILON))
return 0;
else
return -1;
return 0;
}

#ifdef UNIT_TEST

#include <stdio.h>

int main ()
{
double u = 1.0;
double v = u + 1e-14;
double big = 1.0;
double a = 1.0, b = 0.99999999999999999999999999;
printf ("compare(%*g, %*g) = %d.\n", DBL_DIG+2, a, DBL_DIG+2, b,
double_compare (a, b));
a *= -1;
b *= -1;
printf ("compare(%*g, %*g) = %d.\n", DBL_DIG+2, a, DBL_DIG+2, b,
double_compare (a, b));

big *= 1 << 16;
big *= 1 << 16;
big *= 1 << 16;
big *= 1 << 16;


printf ("compare(%*g, %*g) = %d.\n", DBL_DIG+2, u, DBL_DIG+2, v,
double_compare (u, v));


u *= big;
v *= big;

printf ("compare(%*g, %*g) = %d.\n", DBL_DIG+2, u, DBL_DIG+2, v,
double_compare (u, v));

return 0;
}

#endif
 
M

Mark McIntyre

Kenneth said:
>
How would you check for equality if t and t1 were "int"?
Now, how would you check for equality if t and t1 were "float"?

By some different means. Two mathematically identical floats may compare
inequal by obvious methods.
Or did you have something else in mind for what "equal" means?

Doubtful, but he also probably expected the below to print "true".

#include <stdio.h>
#include <math.h>
int main(void)
{
printf("%f %f %s\n",
exp(log(4.444)), 4.444,
exp(log(4.444))==4.444?"true":"false");
return 0;
}

$ ./a.out
4.444000 4.444000 false
 
C

CBFalconer

Eric said:
Needs a little more work for NaNs and infinities ...

More to the point, the choice of a proper epsilon is
not dictated solely by the machine's precision, but also
by the computations that produced the numbers. For example,
if you calculate a binomial coefficient as

Ignoring NaNs and infinities, the criterion I used is sufficient to
guarantee that an equal/nonequal decision can be made. At most
that criterion is too large by a factor of two, giving an excessive
range of equality. BTW, I believe there is no guarantee that NaNs
and infinities exist in the floating point system.
 
C

CBFalconer

user923005 said:
This should be a FAQ.

#include <float.h>
#include <math.h>

int double_compare (double d1, double d2)
{
if (d1 > d2)
if ((d1 - d2) < fabs (d1 * DBL_EPSILON))
return 0;
else
return 1;
if (d1 < d2)
if ((d2 - d1) < fabs (d2 * DBL_EPSILON))
return 0;
else
return -1;
return 0;
}

What if d1 == DBL_EPSILON and d2 == -LARGE_NUMBER? I think you are
trying to do too much in one routine. I also think I have the
foul-up condition fouled. :)
 
U

user923005

What if d1 == DBL_EPSILON and d2 == -LARGE_NUMBER?  I think you are
trying to do too much in one routine.  I also think I have the
foul-up condition fouled. :)

Try it.
 
U

user923005

What if d1 == DBL_EPSILON and d2 == -LARGE_NUMBER?  I think you are
trying to do too much in one routine.  I also think I have the
foul-up condition fouled. :)

I would be very interested to see any instance where this compare
function fails to perform as expected.

C:\tmp>cl /DUNIT_TEST /W4 /Ox fcomp.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762
for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

fcomp.c
Microsoft (R) Incremental Linker Version 8.00.50727.762
Copyright (C) Microsoft Corporation. All rights reserved.

/out:fcomp.exe
fcomp.obj

C:\tmp>fcomp
compare( 1, 1) = 0.
compare( -1, -1) = 0.
compare( 1, 1) = -1.
compare( 1.84467e+019, 1.84467e+019) = -1.
compare( 2.22045e-016, -1.79769e+308) = 1.

C:\tmp>type fcomp.c
#include <float.h>
#include <math.h>

int double_compare (double d1, double d2)
{
if (d1 > d2)
if ((d1 - d2) < fabs (d1 * DBL_EPSILON))
return 0;
else
return 1;
if (d1 < d2)
if ((d2 - d1) < fabs (d2 * DBL_EPSILON))
return 0;
else
return -1;
return 0;
}

int float_compare (float d1, float d2)
{
if (d1 > d2)
if ((d1 - d2) < fabsf (d1 * FLT_EPSILON))
return 0;
else
return 1;
if (d1 < d2)
if ((d2 - d1) < fabsf (d2 * FLT_EPSILON))
return 0;
else
return -1;
return 0;
}

#ifdef UNIT_TEST

#include <stdio.h>

int main ()
{
double u = 1.0;
double v = u + 1e-14;
double big = 1.0;
double a = 1.0, b = 0.99999999999999999999999999;
printf ("compare(%*g, %*g) = %d.\n", DBL_DIG+2, a, DBL_DIG+2, b,
double_compare (a, b));
a *= -1;
b *= -1;
printf ("compare(%*g, %*g) = %d.\n", DBL_DIG+2, a, DBL_DIG+2, b,
double_compare (a, b));

big *= 1 << 16;
big *= 1 << 16;
big *= 1 << 16;
big *= 1 << 16;


printf ("compare(%*g, %*g) = %d.\n", DBL_DIG+2, u, DBL_DIG+2, v,
double_compare (u, v));


u *= big;
v *= big;

printf ("compare(%*g, %*g) = %d.\n", DBL_DIG+2, u, DBL_DIG+2, v,
double_compare (u, v));

u = DBL_EPSILON;
v = -DBL_MAX;
printf ("compare(%*g, %*g) = %d.\n", DBL_DIG+2, u, DBL_DIG+2, v,
double_compare (u, v));

return 0;
}

#endif
 

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
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top