• Post Reply Bookmark Topic Watch Topic
  • New Topic
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

The sign of zero

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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?
<pre>
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){
System.out.println("NEGATIVE_INFINITY");
}else{
System.out.println("POSITIVE_INFINITY");
}
}
}
</pre>
Thank you very much
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
As an integer -0's binary representation is as same as 0, by taking 2's compliment.
B Arul.
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct and here is a quote from Java language specification.
http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#9208
"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
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Bala and Huiying
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic