my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Math.min() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Math.min()" Watch "Math.min()" New topic
Author

Math.min()

Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
Hi!,
System.out.println(Math.min(-0,0));
Why does this print 0, instead of -0.0. I am asking becuase in the Math class -0 < 0 and in the same time the min() returns a double value.So there should be a fractional part as well. Don't you think so.
Thank you very much..


SCJP 1.4, SCMAD 1.0<br />SCWCD, SCBCD (in progress)
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
ransika, first of all the signatures for the Math.min() methods are:
min(double,double) returns double
min(float,float) returns float
min(long,long) returns long
min(int,int) returns int
And secondly, negative zero is not less that positive zero, as a matter of fact they are both equal.
System.out.println(-0 == 0);
Now when you call Math.min(-0,0) the 4th overloaded method is invoked (the one which return integer), thus outputting positive zero because integer and long values doesn't have an implementation for negative zero (float and double values do implement negative zero however).
Hope this helps.


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
Yan Lee
Ranch Hand

Joined: Sep 15, 2003
Posts: 94
From the JLS:
--------------
"Positive zero and negative zero compare equal; thus the result of the expression 0.0==-0.0 is true and the result of 0.0>-0.0 is false. But other operations can distinguish positive and negative zero; for example, 1.0/0.0 has the value positive infinity, while the value of 1.0/-0.0 is negative infinity."
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Math.min()
 
Similar Threads
substring problem
Math.min with "-0" and "+0"
How -0.0 and 0.0 makes the difference ?
q from Sahir's mock
Controling Text Length