Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > compare two float values

Reply
Thread Tools

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
 
Reply With Quote
 
 
 
 
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>
Try the download section.



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

 
Reply With Quote
 
 
 
 
neha_chhatre@yahoo.co.in
Guest
Posts: n/a
 
      02-20-2008
thank you for giving me the site url
 
Reply With Quote
 
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)
 
Reply With Quote
 
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
> 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
 
Reply With Quote
 
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/>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
 
Reply With Quote
 
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>
Try the download section.



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

 
Reply With Quote
 
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>
Try the download section.



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

 
Reply With Quote
 
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.
 
Reply With Quote
 
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
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+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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
How to compare two SOAP Envelope or two Document or two XML files GenxLogic Java 3 12-06-2006 08:41 PM
Float to int conversion by using two int variables for representation of the float variable k3n3dy C++ 15 04-20-2006 06:53 PM
How to compare values of two objects in C# Sukh ASP .Net 3 02-06-2006 05:50 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments