This week's book giveaway is in the Android forum.We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Head First Android this week in the Android forum!

# Double

Arathi Rajashekar
Ranch Hand

Joined: Nov 20, 2001
Posts: 112
Hi,
I have doubt in the following code

Why it gives as answer as positive infinity.
Won't the -0 be promoted to -0.0. What is the reason

Arathi<br />Sun Certified Java Programmer
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Arathi,
The answer is because you are using -0 instead of -0.. There is no such value -0 as an integer it is just 0 therefore the compiler performs the following:
-10. / -0
replaced with
-10. / 0
converted to
-10. / 0.

Only IEEE 754 recognizes a difference bewteen positive and negative zero!

If you had used the double negative zero you would get the result you expected without any replacement or conversion by the compiler.
Regards,
Manfred.
Darryl Failla
Ranch Hand

Joined: Oct 16, 2001
Posts: 129
In order for the operation to take place, -0 must be cast to a double. To do that, the int value -0 must first be evaluated. Since there is no such int as -0, the value is evaluated as 0. This is cast to 0.0, and the operation is performed; leaving a result of POSITIVE_INFINITY.

Darryl Failla
Sun Certified Java 2 Programmer
Arathi Rajashekar
Ranch Hand

Joined: Nov 20, 2001
Posts: 112
Originally posted by Manfred Leonhardt:
Hi Arathi,
The answer is because you are using -0 instead of -0.. There is no such value -0 as an integer it is just 0 therefore the compiler performs the following:
-10. / -0
replaced with
-10. / 0
converted to
-10. / 0.

Only IEEE 754 recognizes a difference bewteen positive and negative zero!

If you had used the double negative zero you would get the result you expected without any replacement or conversion by the compiler.
Regards,
Manfred.