Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

compare two float values

 
 
user923005
Guest
Posts: n/a
 
      02-21-2008
On Feb 20, 5:59*pm, user923005 <(E-Mail Removed)> wrote:
> On Feb 20, 5:00*pm, CBFalconer <(E-Mail Removed)> wrote:

[snip]
> > 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.


This version has better formatting, which will allow us to see the
dirt.
That makes the output much easier to grok.

#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+5, a, DBL_DIG+5, b,
double_compare (a, b));
a *= -1;
b *= -1;
printf ("compare(%.*g, %.*g) = %d.\n", DBL_DIG+5, a, DBL_DIG+5, 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+5, u, DBL_DIG+5, v,
double_compare (u, v));


u *= big;
v *= big;

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

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

return 0;
}
/*
compare(1, 1) = 0.
compare(-1, -1) = 0.
compare(1, 1.00000000000001) = -1.
compare(18446744073709552000, 18446744073709736000) = -1.
compare(2.2204460492503131e-016, -1.7976931348623157e+30 = 1.
*/
#endif

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      02-21-2008
Mark McIntyre <(E-Mail Removed)> writes:
> 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.


Depends on what you mean by "mathematically identical".

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


Here you have two floating-point values that result from calculations
that, if they were carried out with complete mathematical exactness,
would be equal. But the actual values are not equal, because
floating-point calculations aren't completely exact. In this case,
you can see that they're inexact by using a different format. For
example:

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

4.44399999999999906208358879666775
4.44399999999999995026200849679299
false

If you want to know whether two floating-point values are "equal", the
first thing you have to ask yourself is just what that means. The
usual recommendation is to test whether the values are close enough --
but how close is close enough depends on the nature of the
calculations that generated the values. If the tolerance is too
tight, numbers that are logically equal might appear to be unequal; if
it's too loose, numbers that are logically distinct might appear to be
equal.

And sometimes exact equality (the built-in "==" operator) is just what
you want. For example, with a comparison function that checks whether
two numbers are within some epsilon of each other, you can have X == Y
and Y == Z, but X != Z. Whether that's a problem depends on the
algorithm.

Floating-point arithmetic is very probably harder than you think it is
unless your name is David Goldberg, and perhaps even if it is.
Speaking of which, see David Goldberg's paper "What Every Computer
Scientist Should Know About Floating-Point Arithmetic",
<http://docs.sun.com/source/806-3568/ncg_goldberg.html>.

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      02-21-2008
user923005 <(E-Mail Removed)> wrote:

> On Feb 20, 6:42=A0am, (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.


And surprise, surprise, it is: 14.5, <http://c-faq.com/fp/fpequal.html>.

Richard
 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      02-21-2008
user923005 wrote:
> On Feb 20, 5:59 pm, user923005 <(E-Mail Removed)> wrote:
>> On Feb 20, 5:00 pm, CBFalconer <(E-Mail Removed)> wrote:

> [snip]
>>> 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.

>
> This version has better formatting, which will allow us to see the
> dirt.
> That makes the output much easier to grok.
>
> #include <float.h>
> #include <math.h>

<snip>
> int float_compare (float d1, float d2)
> {
> if (d1 > d2)
> if ((d1 - d2) < fabsf (d1 * FLT_EPSILON))

<snip>

Where is fabsf()? Doesn't seem to be avaible on the platform I'm using,
which claims to be C89.
What do use instead?

Bye, Jojo


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      02-21-2008
Joachim Schmitz wrote:
> user923005 wrote:
>> user923005 <(E-Mail Removed)> wrote:
>>> CBFalconer <(E-Mail Removed)> wrote:
>>>
>>> [snip]
>>>
>>>> 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.

>>
>> This version has better formatting, which will allow us to see
>> the dirt. That makes the output much easier to grok.
>>
>> #include <float.h>
>> #include <math.h>

> <snip>
>> int float_compare (float d1, float d2) {
>> if (d1 > d2)
>> if ((d1 - d2) < fabsf (d1 * FLT_EPSILON))

> <snip>
>
> Where is fabsf()? Doesn't seem to be avaible on the platform I'm
> using, which claims to be C89. What do use instead?


>From N869, for C99. For C90 use doubles and fabs.


7.12.7.2 The fabs functions
Synopsis
[#1]
#include <math.h>
double fabs(double x);
float fabsf(float x);
long double fabsl(long double x);

--
[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
 
karthikbalaguru
Guest
Posts: n/a
 
      02-21-2008
On Feb 20, 9:29*pm, santosh <(E-Mail Removed)> 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

>
> > 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%20flo...>
> <http://docs.sun.com/source/806-3568/ncg_goldberg.html>


I like the links. Nice Collection

Karthik Balaguru
 
Reply With Quote
 
user923005
Guest
Posts: n/a
 
      02-21-2008
On Feb 20, 11:25*pm, (E-Mail Removed) (Richard Bos) wrote:
> user923005 <(E-Mail Removed)> wrote:
> > On Feb 20, 6:42=A0am, (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.

>
> And surprise, surprise, it is: 14.5, <http://c-faq.com/fp/fpequal.html>.


Unfortunately, it's hopelessly broken:
C:\tmp>reldif
RelDif of 1 and -1 is 2

C:\tmp>type reldif.c
#define Abs(x) ((x) < 0 ? -(x) : (x))
#define Max(a, b) ((a) > (b) ? (a) : (b))

double RelDif(double a, double b)
{
double c = Abs(a);
double d = Abs(b);

d = Max(c, d);

return d == 0.0 ? 0.0 : Abs(a - b) / d;
}

int main(void)
{
printf("RelDif of %.20g and %.20g is %.20g\n", 1.0, -1.0,
RelDif(1.0, -1.0));
return 0;
}
 
Reply With Quote
 
user923005
Guest
Posts: n/a
 
      02-21-2008
On Feb 21, 11:53*am, user923005 <(E-Mail Removed)> wrote:
> On Feb 20, 11:25*pm, (E-Mail Removed) (Richard Bos) wrote:
>
> > user923005 <(E-Mail Removed)> wrote:
> > > On Feb 20, 6:42=A0am, (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.

>
> > And surprise, surprise, it is: 14.5, <http://c-faq.com/fp/fpequal.html>.

>
> Unfortunately, it's hopelessly broken:
> C:\tmp>reldif
> RelDif of 1 and -1 is 2
>
> C:\tmp>type reldif.c
> #define Abs(x) * *((x) < 0 ? -(x) : (x))
> #define Max(a, b) ((a) > (b) ? (a) : (b))
>
> double * * * * *RelDif(double a, double b)
> {
> * * double * * * * *c = Abs(a);
> * * double * * * * *d = Abs(b);
>
> * * d = Max(c, d);
>
> * * return d == 0.0 ? 0.0 : Abs(a - b) / d;
>
> }
>
> int * * * * * * main(void)
> {
> * * printf("RelDif of %.20g and %.20g is %.20g\n", 1.0, -1.0,
> RelDif(1.0, -1.0));
> * * return 0;
>
>
>
> }


Nevermind. It works the way it was designed to work.
 
Reply With Quote
 
Harald van Dijk
Guest
Posts: n/a
 
      02-21-2008
On Thu, 21 Feb 2008 11:53:18 -0800, user923005 wrote:
> On Feb 20, 11:25*pm, (E-Mail Removed) (Richard Bos) wrote:
>> And surprise, surprise, it is: 14.5,
>> <http://c-faq.com/fp/fpequal.html>.

>
> Unfortunately, it's hopelessly broken: C:\tmp>reldif
> RelDif of 1 and -1 is 2


What's wrong with that? What answer did you expect, and why?
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      02-21-2008
Keith Thompson wrote:
> Mark McIntyre <(E-Mail Removed)> writes:
>> 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.

>
> Depends on what you mean by "mathematically identical".


Er, I mean mathematically identical, as in the same.

>
> Here you have two floating-point values that result from calculations
> that, if they were carried out with complete mathematical exactness,
> would be equal. But the actual values are not equal, because
> floating-point calculations aren't completely exact. In this case,
> you can see that they're inexact by using a different format.



<fx>egg being sucked</fx>
I know this. Why do you think I posted the example?

> printf("%.32f\n%.32f\n%s\n",


Note that other than as a demo of the issue, I consider this meaningless
since one is printing greater than the precision of the type.

> Floating-point arithmetic is very probably harder than you think


Did you confuse me with the OP?

> unless your name is David Goldberg,


Its not, but I did spend several years using numerical analysis as part
of my post-doc research so I hope I'm fairly familiar with FP....

--
Mark McIntyre

CLC FAQ <http://c-faq.com/>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
 
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