Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login

# NaN in Double class

Natalie Ap
Ranch Hand

Joined: Jan 09, 2009
Posts: 49
Hello all!

I am so confused about the following outputs! In the api code for Double that i saw had the following comment before the declaration of NaN constant:

If this is so, how come the two values in LHS and RHS are unequal?? The type of NaN is a primitive double, not an object reference..

Fiona
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9466

20

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.

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Natalie Ap
Ranch Hand

Joined: Jan 09, 2009
Posts: 49
Ankit Garg wrote:I think this excerpt from the JLS answers your question

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.

Hi Ankit,

But did not quite understand the last part of the paragraph.
They say, (x<y) == !(x>=y) will be false if x or y is NaN.
But this also applies to the following:

Say,

x = 0;
y = 1;

(x<y) -> 0 < 1 -> true
(x>=y) -> 0 >= 1 -> false
hence the lhs and rhs are not equal and hence (x<y) == !(x>=y) is false even though neither of them are NaN

Thanks
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9466

20

Pats read the statement again. Its (x<y) == !(x>=y) and not (x<y) == (x>=y)
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 561

Can you tell me from where did you get this type of question.is it a SCJP question ??
Please tell me from which book you got this question.

Can you give me an example with regards to positive and negative infinity. ??

--
Deepak Lal

When The Going Gets Tougher,The Tougher gets Going
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 20535

50

If this is so, how come the two values in LHS and RHS are unequal?? The type of NaN is a primitive double, not an object reference..

The simple answer here is.... The IEEE floating point standard defines it as so -- NaN does not equal NaN. And Java is simply supporting the standard.

As a side note, the equals() method will report it as equal, if they are Double objects. This is in direct violation of the standard, and the JavaDoc states it as so. The reason this is necessary is because it will break the usability of the certain collections if NaN does not equal NaN.

Henry

Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 561

Hi Henry,
Could you please provide me an example for Nan when used with Double. ?
Is this a SCJP question,if so which book demonstrated this concept.

Help provided will be highly appreciated
--
Deepak Lal
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8898

5
Hi Guys,

Interesting topic, but NOT on the exam.

hth,

Bert

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 561

Hi Bert ,
Could you please give us an example atleast on NaN used with Double class.??

--
Deepak Lal

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 20535

50

Deepak Lal wrote:Hi Bert ,
Could you please give us an example atleast on NaN used with Double class.??

No offense here -- but you are a SCJP candidate. It would take less than a minute to create a method, that creates two Double instances (assigned to NaN), and test the equals() method. What is it that is preventing you from writing this simple example?

Henry

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

subject: NaN in Double class