Velocity Reviews > compare two float values

# compare two float values

Pietro Cerutti
Guest
Posts: n/a

 02-20-2008
Kenneth Brody wrote:
> http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>> 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"?

In a different way than above....

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

--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp

CBFalconer
Guest
Posts: n/a

 02-20-2008
(E-Mail Removed) wrote:
>
> 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 */

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

neha_chhatre@yahoo.co.in
Guest
Posts: n/a

 02-20-2008
thank you for giving me the site url

Eric Sosman
Guest
Posts: n/a

 02-20-2008
CBFalconer wrote:
> (E-Mail Removed) wrote:
>> 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 */

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!

--
(E-Mail Removed)

user923005
Guest
Posts: n/a

 02-20-2008
On Feb 20, 6:42*am, (E-Mail Removed) wrote:
> 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

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

Mark McIntyre
Guest
Posts: n/a

 02-20-2008
Kenneth Brody wrote:
>
> 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

--
Mark McIntyre

CLC FAQ <http://c-faq.com/>

CBFalconer
Guest
Posts: n/a

 02-21-2008
Eric Sosman wrote:
> CBFalconer wrote:
>> (E-Mail Removed) wrote:
>>
>>> 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 */

>
> 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.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

CBFalconer
Guest
Posts: n/a

 02-21-2008
user923005 wrote:
> (E-Mail Removed) wrote:
>
>> 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

>
> 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.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

user923005
Guest
Posts: n/a

 02-21-2008
On Feb 20, 5:00*pm, CBFalconer <(E-Mail Removed)> wrote:
> user923005 wrote:
> > (E-Mail Removed) wrote:

>
> >> 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

>
> > 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.

Try it.

user923005
Guest
Posts: n/a

 02-21-2008
On Feb 20, 5:00*pm, CBFalconer <(E-Mail Removed)> wrote:
> user923005 wrote:
> > (E-Mail Removed) wrote:

>
> >> 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

>
> > 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.

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

fcomp.c
Microsoft (R) Incremental Linker Version 8.00.50727.762

/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+30 = 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