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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 0.0 and -0.0
 
Similar Threads
confusion on (==)
Majji #Q.28
Math functions
double question in srinivas majji's mock exam
-0.0 == 0.0 ? help