Re: Equality of floating-point numbers (special case)
- From: Eric Sosman <Eric.Sosman@xxxxxxx>
- Date: Wed, 19 Apr 2006 10:29:15 -0400
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");
}
--------
Will the condition in the if statement always, unconditionally, and
portably evaluate to a true value?
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).
--
Eric.Sosman@xxxxxxx
.
- Follow-Ups:
- Re: Equality of floating-point numbers (special case)
- From: Robert Latest
- Re: Equality of floating-point numbers (special case)
- References:
- Equality of floating-point numbers (special case)
- From: Robert Latest
- Equality of floating-point numbers (special case)
- Prev by Date: Re: File exist
- Next by Date: Re: Need projects
- Previous by thread: Equality of floating-point numbers (special case)
- Next by thread: Re: Equality of floating-point numbers (special case)
- Index(es):