• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Math.sqrt()

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Check following code.
class test
{
public static void main(String [] args) {
System.out.println(Math.sqrt(-0));
System.out.println(Math.sqrt(+0));
System.out.println(Math.sqrt(-0.0));
System.out.println(Math.sqrt(+0.0));
}
}
prints
0.0,
0.0
-0.0
0.0
Why Math.sqrt(-0) differs from Math.sqrt(-0.0)?
Thanks,
Usha
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
0.0 is a double. Both floats and doubles suffer from imprecision when operated on. You can only guarantee that the answer will be CLOSE to that number. To be truely acurrate they would have to have an infinite number of decimal places - not likely.
So when you take a square root of -0.0 you get something CLOSE to 0, but whatever variance there is is in the negative direction.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic