aspose file tools*
The moose likes Beginning Java and the fly likes Comparison with NaN Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Comparison with NaN" Watch "Comparison with NaN" New topic
Author

Comparison with NaN

leo donahue
Ranch Hand

Joined: Apr 17, 2003
Posts: 327
Why is this comparison false?



Just because?


Thanks, leo
Tim LeMaster
Ranch Hand

Joined: Aug 31, 2006
Posts: 226
I would say just because that are both NaN doesn't make the equal. Lots and Lots of things are NaN (a, b, c, d) but are not equal.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Yes, just because -- or as we computer sciency type fellas like to say, "by definition." The Java Lanaguage Specificiation explicitly states that NaN is not equal to anything, including itself.


[Jess in Action][AskingGoodQuestions]
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
I think for the same reason the two strings in the following code are not equal:

In other words, you have two different objects that are identical, but == compares whether the references are equal, and that's only true when the references refer to the same object.
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
Specificiation explicitly states that NaN is not equal to anything, including itself.

That's the strange part.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Not really strange. It's specified in IEEE 754.
leo donahue
Ranch Hand

Joined: Apr 17, 2003
Posts: 327
Originally posted by Ernest Friedman-Hill:
Yes, just because -- or as we computer sciency type fellas like to say, "by definition." The Java Lanaguage Specificiation explicitly states that NaN is not equal to anything, including itself.


If we compare with the inequality operator: != The comparison is true.

"By definition" is the only option.

If floating point division by zero is really not the same as whatever the value of java.lang.Double.NaN is, then what is the value of floating point division by zero? Did someone just choose to use the same "word"(NaN) to describe that value?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by leo donahue:


If we compare with the inequality operator: != The comparison is true.

"By definition" is the only option.

If floating point division by zero is really not the same as whatever the value of java.lang.Double.NaN is, then what is the value of floating point division by zero? Did someone just choose to use the same "word"(NaN) to describe that value?


It depends on what the numerator is.

If the numerator and denominator are both 0, the result is NaN.

If the numerator is, for instance, 1.0, then 1.0/0 is Infinity.

As Tim pointed out, in the case of 0.0/0 != Double.NaN, just because two things are not numbers, they are not necessarily equal to each other.
[ October 16, 2006: Message edited by: Keith Lynn ]
leo donahue
Ranch Hand

Joined: Apr 17, 2003
Posts: 327
Yes, I meant both numerator and denominator.

I keep forgetting the slightest inference really takes the converstation in different directions. I'll try harder.
Robert Hill
Ranch Hand

Joined: Feb 24, 2006
Posts: 94
Mathematically speaking, x/0 is undefined, not infinity. Where x is a real number, not zero. Infinity is not a number, but can basically be described as if there exists a one-to-one correspondence between that set and a proper subset of itself then the set is infinite.

If you have 1/x, then its value approaches infinity as x goes to zero, but 1/0 is not an infinite number, since infinity is not a number.

0/0 is indeterminant.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparison with NaN
 
Similar Threads
x-- -x/0??
Mixed bag
Enum example from Java Language Specification does not compile.
[easy]Find the number
exponential to float