This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hasanov was kind enough to contribute study questions the other day. Many thanks to you! Here is something that caught my attention: double d = 1/0.0; byte b = (byte)d; //OK, b=-1 byte b = (byte)Double.NaN; //OK, b=0
Why is this? In d above d = NaN. Why does it get translated into -1 in one instance and 0 in the other? I have read the Math API, I've gone to the JLS http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#9208 I have looked at the Double API. Please no requests to "look it up". Any assistance would be greatly appreciated. Pres Those who don't <want to help by providing an answer>, <should not post>.
Hmmm. I don't think so. NaN is a numerical constant (from the Double API) A Not-a-Number (NaN) value of type double. It is equal to the value returned by Double.longBitsToDouble(0x7ff8000000000000L). Why would it return -1 in one case and 0 in the other? You are comparing NaN in both. Any other ideas out there? Thanks, Pres
Originally posted by Pres Brawner: Hmmm. I don't think so. NaN is a numerical constant (from the Double API) A Not-a-Number (NaN) value of type double. It is equal to the value returned by Double.longBitsToDouble(0x7ff8000000000000L). Why would it return -1 in one case and 0 in the other? You are comparing NaN in both. Any other ideas out there? Thanks, Pres
Well, when in doubt, write code? Here's some interesting output I got (1/0.0) = (0x7FF0000000000000) (Double.NaN) = (0x7FF8000000000000) (Double.NEGATIVE_INFINITY) = (0xFFF0000000000000) (Double.POSITIVE_INFINITY) = (0x7FF0000000000000) (Double.MAX_VALUE) = (0x7FEFFFFFFFFFFFFF) (Double.MIN_VALUE) = (0x000000000000001) What's interesting is that 1/0.0 gives a positive infinity response (jdk 1.4.0/windows) instead of NaN. It's a difference of one bit, but casting to byte, how do you get from 1111 1111 to 0000 0000 ? I think I'll dig around some more unless someone figures it out before me Edit: Mistyped the value for negative infinity [ April 11, 2002: Message edited by: Donald Yee ] Went back and did some other stuff. Oddly, I get strange results if I cast double to long versus the Double.doubleToLongBits. (int)(1/0.0) = 0x7FFFFFFF (long)(1/0.0) = 0x7FFFFFFFFFFFFFFF (float)(1/0.0) = Infinity According to the IEEE 754 standard, infinity should be coming out as stated in the Double API. However, I think (can't remember for sure) that when floats/doubles do arithmetic (esp. multiplication and division) with small numbers, things can get wacky like this. Perhaps someone else can confirm that possibility. [ April 11, 2002: Message edited by: Donald Yee ]
Joined: Jan 18, 2001
Duh! I got it now. d = infinity. (I thought it was NaN and hence the problem I was having with this.) Thanks everyone. Pres