wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 0.0 and -0.0 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 and -0.0" Watch "0.0 and -0.0" New topic
Author

0.0 and -0.0

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

System.out.println(0.0==-0.0);
System.out.println(Math.max(0.0,-0.0));
System.out.println(Math.min(0.0,-0.0));


prints out

true
0.0
-0.0

Isn't contradicting ? Any ideas ?
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Strange as it may look, positive and negative zeros are considered equal, hence == returns true.
On the other hand, even zero is a signed number, hence min and max returns different results.
This is one of my favourite gotcha!
Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
rajsim
Ranch Hand

Joined: May 31, 2000
Posts: 116
Strange.
However, this behaviour is specified in API documentation.
 
GeeCON Prague 2014
 
subject: 0.0 and -0.0