You get a NaN from taking the Math.sqrt(...) of a negative floating point number (except negative zero).  i.e.


double d = Math.sqrt( -1 );                  here d is NaN

double d = Math.sqrt( -0.0 );               here d is -0.0


  You cannot assign NaN unqualified without its class name.  Meaning you cannot just say NaN without its Wrapper name.  i.e.

float f = Float.NaN;                       works.

double d = Double.NaN;                 works.

float f = Nan;                               will not compile.

double d = NaN;                           will not compile.


  There is no such thing as Integer.NaN.  NaN is only relevant to the two floating point wrappers Double and Float.


  After casting, the integer equivalent of NaN is always zero. i.e.


double d = -1;

double nan = Math.sqrt( d );

int x = ( int ) nan;                  Here x becomes 0:


  Any math operation against NaN yields NaN.  i.e.


double d = -1;

float f = -1;

double dnan = Math.sqrt( d );

float fnan = ( float ) Math.sqrt( f );

double df = dnan +  fnan;                    Here df becomes NaN.


  You cannot detect NaN with an if == statement. The == operator will always return false for NaN. Instead you must convert to a wrapper and use the isNaN( ) method.  i.e.


float f = Float.NaN; 

if ( f == Float.NaN )                        will not work correctly as it returns false.

if( new Float( f).isNaN(  ) )                   works, correctly returning true.


  Any ( NaN == NaN ) comparison will always return false.  i.e.
if ( Float.Nan == Float.NaN ) is false.

However if ( Float.POSITIVE_INFINITY == Float.POSITIVE_INFINITY ) is true.  See next item.


  NaN is treated differently than Infinity, whereby math operations against POSITIVE_INFINITY and NEGATIVE_INFINITY yield positive or negative Infinity, depending upon the sign combinations.  Any identical comparisons on them yield true.  i.e.


double d = Double.POSITIVE_INFINITY;

float f = Float.POSITIVE_INFINITY;

double df = d +  f;   Here df becomes POSITIVE_INFINITY:


Also if ( Float.POSITIVE_INFINITY == Float.POSITIVE_INFINITY ) is true.