File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Hasanov Study list question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Hasanov Study list question" Watch "Hasanov Study list question" New topic
Author

Hasanov Study list question

Pres Brawner
Ranch Hand

Joined: Jan 18, 2001
Posts: 92
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>.
Tim Murphy
Greenhorn

Joined: Apr 11, 2002
Posts: 18
Maybe -1 is a numerical way of representing false
Pres Brawner
Ranch Hand

Joined: Jan 18, 2001
Posts: 92
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
Donald Yee
Greenhorn

Joined: Feb 22, 2002
Posts: 6
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 ]
Pres Brawner
Ranch Hand

Joined: Jan 18, 2001
Posts: 92
Duh!
I got it now.
d = infinity. (I thought it was NaN and hence the problem I was having with this.)
Thanks everyone.
Pres
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9043
    
  10
1/0.0 = positive infinity
-1/0.0 = negative infinity
0/0.0 = NaN


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

Joined: Jul 03, 2001
Posts: 2120
An this is a helpful piece of theory from JLS 5.1.3


SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hasanov Study list question
 
Similar Threads
The sign of zero
float point inexact result
number question
Assigning long value to float?
-0.0 and +0.0