File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

0.0 and -0.0

 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 5782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
rajsim
Ranch Hand
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strange.
However, this behaviour is specified in API documentation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic