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 -0.0==0.0 why true?? 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 "-0.0==0.0 why true??" Watch "-0.0==0.0 why true??" New topic
Author

-0.0==0.0 why true??

Harvinder Singh
Ranch Hand

Joined: Feb 14, 2003
Posts: 90
/**
Hi Ranchers,
Why both the statement -0.0==0.0 and 0.0==0.0 returns true even when
the double support the concept of negative zero?
*/
class NegativeZero{
static void negative_zero(){
//-0.0 output
System.out.println(Math.min(-0.0,0.0));
//why -0.0==0.0 returns true
if(Math.min(-0.0,0.0)==0.0)
System.out.println("-0.0 means something to float and double");
else
System.out.println("This is absured");
}
public static void main(String args[]){
negative_zero();
}
}


Hard work beats talent<br />when talent doesn't work hard.<p> - Tim Notke
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Hi Harwinder.
You might find the answer to your question in the IEEE Standard for Binary Floating-Point Arithmetic 754-1985.
Here is a guess. 0.0 and -0.0 represent the same point on the number line.

There are two source code representations, but there is only one point.
Harvinder Singh
Ranch Hand

Joined: Feb 14, 2003
Posts: 90
Thanks Marlene, I think for the certification that much info is OK.After the certification I will go deep. And if I will find any problem I will
again disturb u
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Harvinder,
Yes, it's out-of-scope to be researching the IEEE 754 standard. What I should have said is, I think the Java implementation of floating point numbers is based on the IEEE 754 standard, and I think 0.0 == -0.0 in the standard.
I once tried looking for the IEEE 754 standard, out of curiosity. I would have had to spend a lot of money to subscribe to be a member of IEEE. So I gave up.
For us the important thing to know is that 0.0 == -0.0, but
Float f1 = new Float(0.0);
Float f2 = new Float(-0.0);
f1.equals(f2) == false
Look up Float.equals in the Java API.
[ January 21, 2004: Message edited by: Marlene Miller ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: -0.0==0.0 why true??