5

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

NaN = "NaN" stands for "not a number". "Nan" is produced if a floating point operation has some input parameters that cause the operation to produce 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 was trying 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 x==y is false ?

Community
  • 1
  • 1
Rahul Agrawal
  • 8,913
  • 18
  • 47
  • 59

4 Answers4

3

NaN is a concept, not a value or a number. Since that concept can represent multiple non-real-number values (imaginary, 0/0, etc) it doesn't make sense to say that any particular NaN is equal to any other NaN.

Similarly you can't say that Double::NEGATIVE_INFINITY equals itself, since infinity is not a number either.

maerics
  • 151,642
  • 46
  • 269
  • 291
1

The equality operator == returns false if either operand is NaN.

JSL sayes about NaN:

  • The numerical comparison operators <, <=, >, and >= return false if either or both operands are NaN (§15.20.1).
  • The equality operator == returns false if either operand is NaN.
  • In particular, (x=y) will be false if x or y is NaN.
  • The inequality operator != returns true if either operand is NaN (§15.21.1).

In particular, x!=x is true if and only if x is NaN.

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
0

Two NaN values cannot be equal to each other.. You don't know what undefined value they represent.. And also you don't know how they are represented..

As quoted in JLS -

the special number NaN (not-a-number) is unordered and has the following characters:

  • The numerical comparison operators <, <=, >, and >= always return false if either or both operands are NaN.
  • The equality operator == returns false if either operand is NaN.
  • The inequality operator != returns true if either operand is NaN .

If you define them like this: -

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

    if (a == b) {
        System.out.println("true");   /** You will get true **/
    }
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • this is not really right, It is equivalent to the value returned by Double.longBitsToDouble(0x7ff8000000000000L). – CloudyMarble Sep 28 '12 at 04:17
  • @MeNoMore.. For which part you are saying this.. I guess for the second line -> You don't know what undefined value they represent.. May be you are right, because I'm not sure about this.. – Rohit Jain Sep 28 '12 at 04:24
  • I meant the representation part too -> "you don't know how they are represented". you may not now hat leaded to the NaN, but Java declares their value and you can show them you will get NaN, which is their representation. – CloudyMarble Sep 28 '12 at 04:45
  • Ok.. Thanks MeNoMore.. Never faced `NaN` Face-to-Face before.. So, don't know many things about it.. – Rohit Jain Sep 28 '12 at 04:57
  • -1: you should specify the source if you take something from that source. The description is taken from the [Java SE Specification](http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3) – Prakash K Sep 28 '12 at 05:06
  • @PrakashK.. Sorry forgot that.. Added the source.. – Rohit Jain Sep 28 '12 at 05:10
  • @PrakashK: Are you removing your -1 after he mentioned the source? – CloudyMarble Sep 28 '12 at 05:23
  • @Rohit Jain Me niether :) i just read it in the JSL as i answered this question thats why i know it. +1 – CloudyMarble Sep 28 '12 at 05:23
  • @MeNoMore.. HaHa :) .. Thanks - for supporting a +1 Vote.. – Rohit Jain Sep 28 '12 at 05:25
  • @MeNoMore Should I not remove it? – Prakash K Sep 28 '12 at 05:27
  • this is really old I know but I'm pretty shocked: how could ever two instances of ```Double``` initialized with ```new``` keyword and then compared to each other using ```==``` be ```true```??? -1 for this – spi Jan 15 '18 at 11:03
0

NaN - not-a-number doesnt make any sense with when doing a operation with NAN number.so cant equal.

Kalla
  • 165
  • 2
  • 12