# Rules Roundup Q 113 Answer Nitpick

David Sutter

Greenhorn

Posts: 2

posted 15 years ago

I was playing Rules Roundup, and encountered Question #113, which is:

What happens when you have this in your code: double x;x=24.0/0;

I forgot that integer types and floating-point types are treated differently, so I answered incorrectly. But in the explanation, it says:

Answer: Compiles and runs

Floating point numbers don't produce

a divide-by-zero ArithmeticException.

They give a result which is a Not a

Number value.

It seems to me that that explanation is somewhat misleading. Perhaps a more accurate answer would be to say that the result is Double.POSITIVE_INFINITY.

I say that because a subsequent test with "(x == Double.NaN)" is false, but "(x == Double.POSITIVE_INFINITY)" is true.

As an aside: I've been told that checking to see if any value is equal to Double.NaN is false, and indeed "(Double.NaN == Double.NaN)" is false! Any explanation for that? I searched the JLS, but could not find anything on the topic of comparisons to Double.NaN in there.

What happens when you have this in your code: double x;x=24.0/0;

I forgot that integer types and floating-point types are treated differently, so I answered incorrectly. But in the explanation, it says:

Answer: Compiles and runs

Floating point numbers don't produce

a divide-by-zero ArithmeticException.

They give a result which is a Not a

Number value.

It seems to me that that explanation is somewhat misleading. Perhaps a more accurate answer would be to say that the result is Double.POSITIVE_INFINITY.

I say that because a subsequent test with "(x == Double.NaN)" is false, but "(x == Double.POSITIVE_INFINITY)" is true.

As an aside: I've been told that checking to see if any value is equal to Double.NaN is false, and indeed "(Double.NaN == Double.NaN)" is false! Any explanation for that? I searched the JLS, but could not find anything on the topic of comparisons to Double.NaN in there.

Nain Hwu

Ranch Hand

Posts: 139

posted 15 years ago

I think your answer is correct.

As for comparison NaN, you can check JDK API on Double.equal()

method. It explans the == also.

In fact, I only know followings are true:

1. Double.NaN != Double.NaN

2. Double.isNaN(Double.NaN)

3. Double.NaN.equals(Double.NaN)

[This message has been edited by Nain Hwu (edited October 09, 2001).]

[This message has been edited by Nain Hwu (edited October 09, 2001).]

It seems to me that that explanation is somewhat misleading. Perhaps a

more accurate answer would be to say that the result is

Double.POSITIVE_INFINITY.

I think your answer is correct.

As for comparison NaN, you can check JDK API on Double.equal()

method. It explans the == also.

In fact, I only know followings are true:

1. Double.NaN != Double.NaN

2. Double.isNaN(Double.NaN)

3. Double.NaN.equals(Double.NaN)

[This message has been edited by Nain Hwu (edited October 09, 2001).]

[This message has been edited by Nain Hwu (edited October 09, 2001).]

David Sutter

Greenhorn

Posts: 2

posted 15 years ago

Thanks for the tips on comparisons to NaN. Having played around with them, I'm now willing to say that the answer given to Q 113 is actually wrong, not merely misleading, since Double.isNaN(x) returns false. It seems that POSITIVE_INFINITY is, according to Java, a number (i.e. it is not Not a Number).

BTW, Double.NaN.equals(Double.NaN) doesn't compile, because Double.NaN is of type "double" (the primitive) not of type "Double" (the wrapper class). Thus Double.NaN.equals() is invalid, and passing Double.NaN as the parameter is invalid because it is expecting a parameter of type Object, but a double (the primitive type) cannot be converted into an Object.

However, it is logically correct, and the equals() method does compare NaNs correctly, because if you have:

Double C = new Double(Double.NaN);

Double D = new Double(Double.NaN);

then "(C.equals(D))" returns true.

Thanks,

Dave

Originally posted by Nain Hwu:

As for comparison NaN, you can check JDK API on Double.equal()

method. It explans the == also.

In fact, I only know followings are true:

1. Double.NaN != Double.NaN

2. Double.isNaN(Double.NaN)

3. Double.NaN.equals(Double.NaN)

Thanks for the tips on comparisons to NaN. Having played around with them, I'm now willing to say that the answer given to Q 113 is actually wrong, not merely misleading, since Double.isNaN(x) returns false. It seems that POSITIVE_INFINITY is, according to Java, a number (i.e. it is not Not a Number).

BTW, Double.NaN.equals(Double.NaN) doesn't compile, because Double.NaN is of type "double" (the primitive) not of type "Double" (the wrapper class). Thus Double.NaN.equals() is invalid, and passing Double.NaN as the parameter is invalid because it is expecting a parameter of type Object, but a double (the primitive type) cannot be converted into an Object.

However, it is logically correct, and the equals() method does compare NaNs correctly, because if you have:

Double C = new Double(Double.NaN);

Double D = new Double(Double.NaN);

then "(C.equals(D))" returns true.

Thanks,

Dave

Nain Hwu

Ranch Hand

Posts: 139

posted 15 years ago

Dave,

You are right. It's my mistake!

BTW, Double.NaN.equals(Double.NaN) doesn't compile, because Double.NaN is of type "double" (the primitive) not of type "Double" (the wrapper class). Thus Double.NaN.equals() is invalid, and passing Double.NaN as the parameter is invalid

because it is expecting a parameter of type Object, but a double (the primitive type) cannot be converted into an Object.

You are right. It's my mistake!