• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

-0.0==0.0 why true??

 
Harvinder Singh
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/**
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();
}
}
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic