-3

Why Float.NaN == Float.NaN gives false ? I have tested it on more than one JVMs.

Any help is appreciated .

  System.out.println(Float.NaN == Float.NaN); // gives false
amicngh
  • 7,831
  • 3
  • 35
  • 54

1 Answers1

3

That's not specific to Java.

IEEE754's NaN are by contract equal to no number, even themselves.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • isn't that the easiest way to detect NaN - if the number is not itself? – Tobias Langner Apr 08 '13 at 12:00
  • @TobiasLangner As put by Wikipedia : *"The equality and inequality predicates are non-signaling so x = x returning false can be used to test if x is a quiet NaN"*. But in your code the clearest would be to use `Float.isNaN`. – Denys Séguret Apr 08 '13 at 12:01