• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Math.min()

 
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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."
 
reply
    Bookmark Topic Watch Topic
  • New Topic