# NaN

Mathew Kuruvilla

Ranch Hand

Posts: 141

posted 14 years ago

- 0

1: Double a = new Double(Double.NaN);

2: Double b = new Double(Double.NaN);

3:

4: if( Double.NaN == Double.NaN )

5: System.out.println("True");

6: else

7: System.out.println("False");

8:

9: if( a.equals(b) )

10: System.out.println("True");

11: else

12: System.out.println("False");

I have not read the stuff about NaN. The above code is printing False

True.

What gives?

Is there anything else that I need to know about NaN?

2: Double b = new Double(Double.NaN);

3:

4: if( Double.NaN == Double.NaN )

5: System.out.println("True");

6: else

7: System.out.println("False");

8:

9: if( a.equals(b) )

10: System.out.println("True");

11: else

12: System.out.println("False");

I have not read the stuff about NaN. The above code is printing False

True.

What gives?

Is there anything else that I need to know about NaN?

Marilyn de Queiroz

Sheriff

Posts: 9059

12

posted 14 years ago

- 0

Lots of tricky stuff about NaN. Better read it first, then ask questions.

JavaBeginnersFaq

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt

Mathew Kuruvilla

Ranch Hand

Posts: 141

Mike Cunningham

Ranch Hand

Posts: 130

posted 14 years ago

- 0

NAN (not a number) is unordered. Comparing it to any other number, including itself (using the == comparison operator), always yields false. If you use the equals() method overridden by double from the object class it returns true because NAN is a constant. I'm not sure if my logic is accurate...but I was looking over the same subject this evening. Also, I was thinking that constants are stored on the heap. If that's the case...can someone confirm.

Thanks.

Thanks.

Marilyn de Queiroz

Sheriff

Posts: 9059

12

posted 14 years ago

- 0

NaN is unordered, so 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, and 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, and (x<y) == !(x>=y) will be false if x or y is NaN.JLS

Floating-point operators produce no exceptions (�11). An operation that overflows produces a signed infinity, an operation that underflows produces a denormalized value or a signed zero, and an operation that has no mathematically definite result produces NaN. All numeric operations with NaN as an operand produce NaN as a result. 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.

JLS

Also the API for the Math class

Floating-point operators produce no exceptions (�11). An operation that overflows produces a signed infinity, an operation that underflows produces a denormalized value or a signed zero, and an operation that has no mathematically definite result produces NaN. All numeric operations with NaN as an operand produce NaN as a result. 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.

JLS

Also the API for the Math class

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt

I agree. Here's the link: http://aspose.com/file-tools |