programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Devaka Cooray
• Liutauras Vilda
• Jeanne Boyarsky
• Bear Bibeault
Sheriffs:
• Paul Clapham
• Knute Snortum
• Rob Spoor
Saloon Keepers:
• Tim Moores
• Ron McLeod
• Piet Souris
• Stephan van Hulst
• Carey Brown
Bartenders:
• Tim Holloway
• Frits Walraven
• Ganesh Patekar

# Rules Roundup Q 113 Answer Nitpick

Greenhorn
Posts: 2
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:
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.

Ranch Hand
Posts: 139

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.

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

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
Dave,

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!