NaN Constant Magic in Java

Possible duplicate:
Why does Double.NaN == Double.NaN return false?

NaN = "NaN" means "not a number". Nan is created if the floating point operation has some input parameters that cause the operation to get some undefined result. For example, 0.0 divided by 0.0 is arithmetically undefined. Taking the square root of a negative number is also undefined.

I tried to use NaN Constant in Java

public class NaNDemo {
    public static void main(String s[]) {
        double x = Double.NaN;
        double y = Double.NaN;

        System.out.println((x == y));
        System.out.println("x=" + x);
        System.out.println("y=" + y);
    }
}

Output

false
x=NaN
y=NaN

So why is x == y false?

+5
source share
4 answers

NaN - , . - (, 0/0 ..), , NaN NaN.

, Double::NEGATIVE_INFINITY , .

+3

== false, NaN.

JSL NaN:

  • <, < =, > >= return false, NaN (ยง15.20.1).
  • == false, NaN.
  • , (x = y) , x y - NaN.
  • != true, NaN (ยง15.21.1).

, x!= x , x NaN.

+1

NaN . , undefined . , .

JLS -

NaN (not-a-number) :

  • <, < =, > >= false, NaN.
  • == false, NaN.
  • != true, NaN.

: -

    Double a = new Double(Double.NaN);
    Double b = new Double(Double.NaN);

    if (a == b) {
        System.out.println("true");   /** You will get true **/
    }
0

NaN - not-a-number does not make any sense when performing an operation with the NAN.so number cannot equal.

0
source

All Articles