This week's book / license giveaways are in the JDBC and Relational Databases and Java in General forums.
We're giving away four copies each of PostGIS in Action and Java Advanced Topics Training and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes The sign of zero Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "The sign of zero" Watch "The sign of zero" New topic

The sign of zero

Mafalda Alabort

Joined: Mar 02, 2001
Posts: 24
I compiled and run this code. It seems that 100.0/-0 s positive and 100.0/-0.0 is negative. So there is a negative zero for doubles but not for ints?. Is there any explanation for this?
public class A1{
public static void main(String [] buff){
//double d = 100.0/-0.0; // outputs negative infinity
double d = 100.0/-0; // outputs positive infinity
if(d == Double.NEGATIVE_INFINITY){
Thank you very much
Bala Arul

Joined: Feb 09, 2001
Posts: 29
As an integer -0's binary representation is as same as 0, by taking 2's compliment.
B Arul.
huiying li
Ranch Hand

Joined: Feb 12, 2001
Posts: 68
You are correct and here is a quote from Java language specification.
"Positive zero and negative zero compare equal; thus the result of the expression 0.0==-0.0 is true and the result of 0.0>-0.0 is false. But other operations can distinguish positive and negative zero; for example, 1.0/0.0 has the value positive infinity, while the value of 1.0/-0.0 is negative infinity."
You should also read the API documentation for Double class method equal().
Hope this helps.

Mafalda Alabort

Joined: Mar 02, 2001
Posts: 24
Thank you Bala and Huiying
I agree. Here's the link:
subject: The sign of zero