• 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:

Double

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Double c = new Double(0.0);
Double d = new Double(-0.0);
System.out.println(c); //prints 0.0
System.out.println(d); // prints -0.0
System.out.println(c.equals(d)); //prints true

double d1 = 0.0 ;
double d2 =-0.0;
System.out.println(d1==d2); //prints flase
Can someone explain this.

Thankyou!!
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Floating-point types in Java follow IEEE754 standard.
http://www.xdweb.net/~dibblego/miscellaneous/documents/IEEE754.pdf

java.lang.Double follows its API Specification.
Here is an excerpt:


Note that in most cases, for two instances of class Double, d1 and d2, the value of d1.equals(d2) is true if and only if

d1.doubleValue() == d2.doubleValue()
also has the value true. However, there are two exceptions:

If d1 and d2 both represent Double.NaN, then the equals method returns true, even though Double.NaN==Double.NaN has the value false.
If d1 represents +0.0 while d2 represents -0.0, or vice versa, the equal test has the value false, even though +0.0==-0.0 has the value true.
This definition allows hash tables to operate properly.



Your assertion that the following code produces false is incorrect:



It prints true.
[ February 09, 2005: Message edited by: Tony Morris ]
 
neel sri
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, My bad .. I meant this ..
System.out.println(c.equals(d)); //prints false
System.out.println(d1==d2); //prints true

Ended up writing asking the otherway..


anyways thanks for the reply, I got it now.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic