Equality of floating-point numbers (special case)

R

Robert Latest

Hi guys,

I'm sure this has been beaten to death on this newsgroup, but I can't
find it in the CLC FAQ.

Consider the following code:

--------

double x = some_value_from_somewhere;
double y = x;

if ((x == y) && ((x - y) == 0)) {
puts("This is what I want");
}

--------

Will the condition in the if statement always, unconditionally, and
portably evaluate to a true value?

I know that I normally cannot rely on testing floats for equality; I
just wonder if this holds also when the variables in question are
explicitly set to the same value in the source.

I feel a bit stupid even typing this question since I can't imagine how
x and y could possibly come out as unequal by any implementation's
standard, but since I've been bitten quite a few times by things that
seemed obvious but turned out to be different (for, in retrospect, very
sensible reasons) I feel that I'd rather ask.

Thanks,
robert
 
E

Eric Sosman

Robert Latest wrote On 04/19/06 10:06,:
Hi guys,

I'm sure this has been beaten to death on this newsgroup, but I can't
find it in the CLC FAQ.

Consider the following code:

--------

double x = some_value_from_somewhere;
double y = x;

if ((x == y) && ((x - y) == 0)) {
puts("This is what I want");
}

No. The Standard permits floating-point implementations
that support the notion of "not a number," or NaN. In IEEE
implementations, NaN is unequal to all floating-point values,
including itself (so `x == y' is false) and most ordinary
arithmetic operations with NaN operands yield a NaN result
(so `x - y' is NaN and `x - y == 0' is false).
 
R

Robert Latest

On Wed, 19 Apr 2006 10:29:15 -0400,
in Msg. said:
No. The Standard permits floating-point implementations
that support the notion of "not a number," or NaN. In IEEE
implementations, NaN is unequal to all floating-point values,
including itself (so `x == y' is false)

Makes sense. What about the non-NaN case?

robert
 
E

Eric Sosman

Robert Latest wrote On 04/19/06 11:13,:
On Wed, 19 Apr 2006 10:29:15 -0400,



Makes sense. What about the non-NaN case?

The up-thread code was

puts() will not be called if `some_value_from_somewhere'
is a NaN, and I think it will also remain uncalled if the
value is an infinity. Even for finite values I think you are
at the mercy of the implementation. Some systems compute
intermediate values to more precision than `double' will
hold, rounding or truncating when the value is actually
stored. On such a system, the result could depend on just
what the optimizer decides to do: if `some_value_from_somewhere'
carries extra precision it is at least possible that the
generated code might do something like

compute_high_precision some_value_from_somewhere
store_rounded x
store_rounded y
compare_to y

.... thus re-using the extra-precise `some_value_from_somewhere'
instead of the normally-precise `x', and the comparison could
yield non-equal.

Floating-point implementations have a lot of quirks, even
today, and it is no wonder that the C committee decided not to
try to iron them out by fiat. Thus, the specifications of F-P
behavior in C are awfully loose, and the corner cases tend to
yield different answers on different systems. I've heard it
said (though I'm not enough of an F-P specialist to evaluate
the argument) that no high-level programming language provides
a correct implementation even of IEEE binary floating-point.
 
R

Robert Latest

Even for finite values I think you are
at the mercy of the implementation.
[...]

Floating-point implementations have a lot of quirks, even
today, and it is no wonder that the C committee decided not to
try to iron them out by fiat.

[...]

In other words: Don't compare float values. Ever. Under no
circumstances. Be prepared that the expression

((double) 0) == (double) 0)

may evaluate to 0.

Thanks,
robert
 
C

Coos Haak

Op 19 Apr 2006 18:39:54 GMT schreef Robert Latest:
Even for finite values I think you are
at the mercy of the implementation.
[...]

Floating-point implementations have a lot of quirks, even
today, and it is no wonder that the C committee decided not to
try to iron them out by fiat.

[...]

In other words: Don't compare float values. Ever. Under no
circumstances.
Rather a bold statement, it's only for equality.
Everyone can freely and dependently compare for less than
or greater than. ;-)
 
E

Eric Sosman

Robert Latest wrote On 04/19/06 14:39,:
Even for finite values I think you are
at the mercy of the implementation.

[...]


Floating-point implementations have a lot of quirks, even
today, and it is no wonder that the C committee decided not to
try to iron them out by fiat.


[...]

In other words: Don't compare float values. Ever. Under no
circumstances. Be prepared that the expression

((double) 0) == (double) 0)

may evaluate to 0.

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

bert

Coos said:
Op 19 Apr 2006 18:39:54 GMT schreef Robert Latest:
Rather a bold statement, it's only for equality.
Everyone can freely and dependently compare for less than
or greater than. ;-)

Alas, probably not so. If I recall correctly, then according to
the strict IEEE spec, when 'x' is a NaN, the conditions (x > y),
(x == y) and (x < y) should all be FALSE. But an optimising
compiler may unsoundly choose to evaluate them differently.
--
 
M

Mark McIntyre

Op 19 Apr 2006 18:39:54 GMT schreef Robert Latest:
Even for finite values I think you are
at the mercy of the implementation.
[...]

Floating-point implementations have a lot of quirks, even
today, and it is no wonder that the C committee decided not to
try to iron them out by fiat.

[...]

In other words: Don't compare float values. Ever. Under no
circumstances.
Rather a bold statement, it's only for equality.
Everyone can freely and dependently compare for less than
or greater than. ;-)

I've seen this fail too:

float s = some_value_retrieved_from_database;
if (s >4.5) puts("high interest rate!");



Mark McIntyre
 
D

Dik T. Winter

> double x = some_value_from_somewhere;
> double y = x;
>
> if ((x == y) && ((x - y) == 0)) {
> puts("This is what I want");
> } ....
> Will the condition in the if statement always, unconditionally, and
> portably evaluate to a true value?
No.

> I feel a bit stupid even typing this question since I can't imagine how
> x and y could possibly come out as unequal by any implementation's
> standard,

Consider the situation where "some_value_from_somewhere" is calculated
in extended precision. The implementation is allowed to use that
extended precision values in every place where "x" or "y" are used,
or it may not. So it can happen that the "x" in "x == y" is retrieved
in the stored precision but that the extended precision value is used
in "y", and they possibly do not compare equal.
 
N

Naresh

Eric said:
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?
 
N

Naresh

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

Sjouke Burry

Naresh said:
Eric Sosman wrote:




#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).
 
P

Patrick

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

P.J. Plauger

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
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top