Here's what I had in my own notes regarding NaN...

NaN is non-ordinal, so relational comparisons involving NaN always result in false. The only exception is NaN != NaN, which returns true. Wrapper methods Float.isNaN(float) and Double.isNaN(double) can be used to test for NaN. Alternatively, a float or double is NaN if it is not equal to itself (e.g., x != x).

Furthermore...

Math.round(Float.NaN) results in an int zero, and Math.round(Double.NaN) results in a long zero.

String literals "NaN" and "Infinity" are acceptable for Float and Double constructors.

float or double division by zero results in +/- Infinity; except dividing zero by zero, which results in NaN.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org

Tilo Hemp
Ranch Hand

Joined: Nov 21, 2005
Posts: 91

posted

0

Hi Marc,

thanks a lot for the interesting information!

I've got one remark and one question:

NaN is non-ordinal, so relational comparisons involving NaN always result in false. The only exception is NaN != NaN, which returns true.

--> the exception is not limited to NaN != NaN, but can also applied to say Double.NaN != 12.34, which also gives true. I guess != simply inverts the output of ==.

And my question is: what does "Cuius rei demonstrationem mirabilem sane detexi hanc marginis exiguitas non caperet." mean? I tried to find an automated latin-translator, but this did not work out

Originally posted by Tilo Hemp: ...--> the exception is not limited to NaN != NaN, but can also applied to say Double.NaN != 12.34, which also gives true. I guess != simply inverts the output of ==...

You're correct! I guess it would be more accurate to say something like, "the exception is in applying the != comparison to NaN, which returns true -- even when comparing two NaNs."

As for the quote, see Fermat's Last Theorem (and note the enigmatic quality that made Fermat's words so intriguing in the first place ).