Two Laptop Bag
The moose likes Beginning Java and the fly likes How do I square root a Double? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do I square root a Double?" Watch "How do I square root a Double?" New topic

How do I square root a Double?

Conrad McLaughlin

Joined: Jan 31, 2006
Posts: 27
How come when I try and square root a Double I get the same answer?


Double VL = 2.0;
VL = Math.sqrt(VL);
System.out.println("VL =" + VL);

Why is the System output:
VL = 2.0
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

If you run that exact code in JDK 1.5.0 or later, it will print

VL =1.4142135623730951

In earlier versions of Java it won't compile.

So there's some difference between the code you're showing us and the code you actually ran -- try putting just this code into an appropriate main() and running it; then try to figure out how your actual code is different.

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: How do I square root a Double?
It's not a secret anymore!