floating point - Java isNan how it works? -


i looking @ openjdk-1.7.0_25 source code , have seen method:

/**  * returns {@code true} if specified number  * not-a-number (nan) value, {@code false} otherwise.  *  * @param   v   value tested.  * @return  {@code true} if argument nan;  *          {@code false} otherwise.  */ static public boolean isnan(float v) {     return (v != v); } 

i can't understand how works, when method can return true?

that method can return true operations, example:

system.out.println(float.isnan(0.0f / 0.0f)); system.out.println(double.isnan(math.sqrt(-1))); 

basically, nan represents undefined value. value of 0.0 / 0.0 nan, , nan != nan. may seem logical because math.sqrt(-1) gives nan.

see javadoc of double.nan:

it equivalent value returned double.longbitstodouble(0x7ff8000000000000l)

and double.longbitstodouble():

if argument value in range 0x7ff0000000000001l through 0x7fffffffffffffffl or in range 0xfff0000000000001l through 0xffffffffffffffffl, result nan. no ieee 754 floating-point operation provided java can distinguish between 2 nan values of same type different bit patterns.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -