Java isNan how it works?

后端 未结 5 1970
别那么骄傲
别那么骄傲 2021-02-04 03:04

I was looking at the openjdk-1.7.0_25 source code and I have seen this method:

/**
 * Returns {@code true} if the specified number is a
 * Not-a-Num         


        
5条回答
  •  醉梦人生
    2021-02-04 03:29

    Because only NaN compares false with itself. So it will return true when you pass NaN to the method.

    A comparison with a NaN always returns an unordered result even when comparing with itself. ... The equality and inequality predicates are non-signaling so x = x returning false can be used to test if x is a quiet NaN.

    Source

    It not just about Java, It is also true for all languages following IEEE754 standard.

    Related question : Why does Double.NaN==Double.NaN return false?

提交回复
热议问题