Java isNan how it works?

后端 未结 5 1973
别那么骄傲
别那么骄傲 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:21

    Simple.. Nan is always != NaN, these values are not equal to anything. See this:

    As has already been described, NaN is unordered, so a numeric comparison operation involving one or two NaNs returns false and any != comparison involving NaN returns true, including x!=x when x is NaN.

    So testing if v != v is sufficient to tell whether the value is NaN or not.

提交回复
热议问题