Tuesday, December 09, 2008

Double.NaN Is Evil

I don't know what Sun had in mind when creating Double.NaN number. It is very inintuitive to use. I am sure every single developer out there fell in the trap of trying to find out if a double was NaN or not using:
Double.NaN == myDouble


This does not work (I don't know the real reason why), one has to use:
Double.isNaN(myDouble)

Not intuitive!

4 comments :

  1. This happens because Java's implementation of NaN follows IEEE 754 which I understand specifies this behaviour.

    ReplyDelete
  2. I also had many headaches with NaN values. Many algorithms do not cope well with it, or the handling was added as an afterthought. There are a few entries about NaN in my blog:
    http://www.jroller.com/ethdsy/entry/nan_of_them
    http://www.jroller.com/ethdsy/entry/looking_for_max
    http://www.jroller.com/ethdsy/entry/java_io_does_not_save

    ReplyDelete
  3. You should use Double.compare or Double.compareTo for this, works like a charm!

    ReplyDelete