• Post Reply Bookmark Topic Watch Topic
  • New Topic

Double, isNaN and v != v  RSS feed

 
Alistair Parler
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I've been looking through the JDK code and came across this in the Double class:



Is there some trickery going on here?!?! I can't see how this would work!! In 'normal' Java doesn't this mean that for any double, it would never be 'not a number' (which I agree with) but where is the true case coded for (i.e. for 0.0d / 0.0)
Any help from some Java gurus greatly appreciated in explaining this!


Thanks,
Alistair
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nan is special. NaN != Nan. So if we want to know if a double is Nan then we compare it with itself.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NaN is a special value that isn't equal to anything, not even itself. So if a double is not equal to itself, it is NaN, I believe.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 minute, 5 seconds apart. You two will have to try harder. You want to beat each other to the post by 1 second

You may find useful things about NaN in the Java™ Language Specification; also look for the index and look up NaN. I believe this behaviour is common to many computer languages; C and C++ certainly support the same behaviour.

Note: the JLS is by no means easy reading.
 
Alistair Parler
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NaN is a special value that isn't equal to anything, not even itself.


I 'understand' that (in that I've read it and just accept it as being true).

But I don't see how we can deduce the following from that:

So if we want to know if a double is Nan then we compare it with itself.


Looking at the Javadoc it talks of NaN being defined internally as a sequence of bit patterns:



and



(taken from Double.fromLongBitsToDouble)

So maybe the JVM shuffles some things around internally to make these bit comparisons unequal?

I don't need to know this (as many of us don't) but I'm just curious that's all!

If anyone has a more technical answer (put in a big print way ;-)) I'd appreciate it.

Thanks though for your input.
 
Alistair Parler
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks - I'll have a read! ;)
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Google for the IEEE specification; I think it's IEEE785 or IEEE754 (or IEEE 7something ). In 32 bits 0 11111111 000... is ∞ and x 11111111 xxxxx... is NaN where x means 1 or 0, and xxxxx... means any combination of bits except all 0. The interpreters or executors of the code are programmed to interpret every NaN as different from everything else. The format is similar for double (64 bits) but there are eleven 1s together before xxxxx...
You may find something useful (but difficult to read) in the Java™ Language Specification. (The JLS does tell you it's IEEE 754, at least.)
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!