Java isNan how it works?

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

    From Java Language Specification:

    Floating-point equality testing is performed in accordance with the rules of the IEEE 754 standard:

    • If either operand is NaN, then the result of == is false but the result of != is true. Indeed, the test x!=x is true if and only if the value of x is NaN. (The methods Float.isNaN and Double.isNaN may also be used to test whether a value is NaN.)

    • Positive zero and negative zero are considered equal. Therefore, -0.0==0.0 is true, for example.

    • Otherwise, two distinct floating-point values are considered unequal by the equality operators. In particular, there is one value representing positive infinity and one value representing negative infinity; each compares equal only to itself, and each compares unequal to all other values.

提交回复
热议问题