Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Math.min with "-0" and "+0"

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
When I am practicing Math class methods i got the foll. doubt.
double d1 = -0.0;
double d2 = +0.0;
s.o.p(Math.min(d1,d2)); // giving "-0.0" as i expected
but
double d1 = -0;
double d2 = +0;
s.o.p(Math.min(d1,d2)); // giving "0.0" why?
also for
int i1=-0;
int i2=+0; // giving "0" as the min value.
also
double d1 = -0.0;
s.o.p(d1); // giving "-0.0"
and
double d1 = -0;
s.o.p(d1); // giving "0.0"
I came to a small conclusion that int may not show "-0" and it will treat it as "0" if you give it. so some think relative to this happening for float values if decimal place is not there.
Please correct me.
thanks in advance
rajan
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Integer data types do not distinguish between -0 and +0 and hence, Math.min(-0,+0) or Math.max(-0,+0) gives 0 as the answer.
But then, JLS specifies that -0.0 and +0.0 are different and the former is strictly smaller than the latter. Hence the Math.min of these two give different results.
But then, when you say
double d1=-0;
System.out.println(d1);
it would print 0.0 because, -0 (since it doesn't have a decimal point) would be treated as an integer and put as 0. When assigning this to d1, it would be cast to a double value and hence 0.0 would be assigned to d1. Same would not be the case when you say
double d1=-0.0;
System.out.println(d1);
Hope this is clear.
But can someone tell me why this distinction of -0.0 and +0.0 present in double values alone and not in ints? Where is it applicable in real time?
Aparna
 
rajan raju
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Thanks aparna.
Now I got it.
rajan
 
You don't know me, but I've been looking all over the world for. Thanks to the help from this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic