Velocity Reviews > Equality of floating-point numbers (special case)

# Equality of floating-point numbers (special case)

Naresh
Guest
Posts: n/a

 04-20-2006

Eric Sosman wrote:

> Well, now I think you're going overboard. Equality
> comparisons of F-P values are cause for raised eyebrows,
> but not necessarily to be shunned in every circumstance.
> For example, consider this qsort() comparator:
>
> int compare_doubles(const void *p, const void *q) {
> double u = *(const double*)p;
> double v = *(const double*)q;
> if (u < v) return -1;
> if (u > v) return +1;
> return 0;
> }
>
> There's an implicit equality comparison tucked away in
> this code, and I don't think it can be eliminated: no
> matter how the comparison function is written, it must
> be capable of returning zero. When it returns zero, it
> has, in effect, performed an equality comparison.
>

#include<stdio.h>

int compare_doubles(const void *p, const void *q) {
double u = *(const double*)p;
double v = *(const double*)q;
if (u < v) return -1;
if (u > v) return +1;
return 0;
}

int main(void)
{
float u, v;
/* I'm using float here instead of double */

int value;

u = 4.50;
v = 4.50;

value = compare_doubles(&u, &v);

return value;
}

It doesnt return 0 unless i use double u,v; in main.
Whats is the wrong i'm doing by using float? Isnt that function should
itself extend float to double precision and do the things?

Naresh
Guest
Posts: n/a

 04-20-2006

Naresh wrote:
> Eric Sosman wrote:
>
> > Well, now I think you're going overboard. Equality
> > comparisons of F-P values are cause for raised eyebrows,
> > but not necessarily to be shunned in every circumstance.
> > For example, consider this qsort() comparator:
> >
> > int compare_doubles(const void *p, const void *q) {
> > double u = *(const double*)p;
> > double v = *(const double*)q;
> > if (u < v) return -1;
> > if (u > v) return +1;
> > return 0;
> > }
> >
> > There's an implicit equality comparison tucked away in
> > this code, and I don't think it can be eliminated: no
> > matter how the comparison function is written, it must
> > be capable of returning zero. When it returns zero, it
> > has, in effect, performed an equality comparison.

>

Oh sorry, I misunderstood the statement. "..and I don't think it can be
eliminated: no
matter how the comparison function is written, it must be capable of
returning zero...."

Sjouke Burry
Guest
Posts: n/a

 04-20-2006
Naresh wrote:
> Eric Sosman wrote:
>
>
>> Well, now I think you're going overboard. Equality
>>comparisons of F-P values are cause for raised eyebrows,
>>but not necessarily to be shunned in every circumstance.
>>For example, consider this qsort() comparator:
>>
>> int compare_doubles(const void *p, const void *q) {
>> double u = *(const double*)p;
>> double v = *(const double*)q;
>> if (u < v) return -1;
>> if (u > v) return +1;
>> return 0;
>> }
>>
>>There's an implicit equality comparison tucked away in
>>this code, and I don't think it can be eliminated: no
>>matter how the comparison function is written, it must
>>be capable of returning zero. When it returns zero, it
>>has, in effect, performed an equality comparison.
>>

>
>
> #include<stdio.h>
>
> int compare_doubles(const void *p, const void *q) {
> double u = *(const double*)p;
> double v = *(const double*)q;
> if (u < v) return -1;
> if (u > v) return +1;
> return 0;
> }
>
> int main(void)
> {
> float u, v;
> /* I'm using float here instead of double */
>
> int value;
>
> u = 4.50;
> v = 4.50;
>
> value = compare_doubles(&u, &v);
>
> return value;
> }
>
> It doesnt return 0 unless i use double u,v; in main.
> Whats is the wrong i'm doing by using float? Isnt that function should
> itself extend float to double precision and do the things?
>

You are pointing to floats and compare them
as doubles,of course they are not equal.
Both pointers as used add 4 random
bytes to themselfs.(assuming 4byte float and
8 byte doubles).
If there had been cleared bytes behind them,
you might even find them equal(and get bitten
later on).

Patrick
Guest
Posts: n/a

 04-20-2006
Branching off of what Eric Sosman described, you could try the same
thing but with an explicit comparison of the difference with an error
constant. It would look something like this:

------------------------------
#define EPSILON 0.000001
/* ... */
if ( abs(y-x) < EPSILON )
puts("This is what I want");

----------------------------

This way, you can define how many decimal digits of equality you want
in the comparison, and in my experiance, the less-than operator is much
more stable than the equality operator, at least with floating point
numbers.

P.J. Plauger
Guest
Posts: n/a

 04-20-2006
"Patrick" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...

> Branching off of what Eric Sosman described, you could try the same
> thing but with an explicit comparison of the difference with an error
> constant. It would look something like this:
>
> ------------------------------
> #define EPSILON 0.000001
> /* ... */
> if ( abs(y-x) < EPSILON )
> puts("This is what I want");
>
> ----------------------------
>
> This way, you can define how many decimal digits of equality you want
> in the comparison, and in my experiance, the less-than operator is much
> more stable than the equality operator, at least with floating point
> numbers.

Such an approach is *not* suitable for ordering floating-point values,
as in bsearch and qsort. You can end up with the situation where a == b
and b == c, but a != c. You need a strict weak ordering which is just
not all that hard to create for floating-point values. NaNs are the only
real trouble spot.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com

 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 OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Lance Hoffmeyer Python 2 07-26-2007 01:45 AM Andrew Tatum C++ 6 05-27-2007 12:47 AM Subra C Programming 25 03-08-2007 01:31 AM Breedo A+ Certification 0 12-02-2005 12:45 AM mort Digital Photography 3 02-16-2005 01:43 AM

Advertisments