how to calculate square root a number?
Hi Folks!
Can anybody tell me is there any class which i can use to take square root of a number? I check BigDecimal & BigInteger but didnt see any method for square root. I need to calculate square root in my following code.
publicvoid calcSqrt(BigInteger in)
{
BigDecimal div, adding, divTwo;
BigInteger input = in;
//square root of input...
}
Thanks in advance
[576 byte] By [
asyed01a] at [2007-11-27 8:06:43]

> Check out the Math class at
> http://java.sun.com/j2se/1.5.0/docs/api/
>
> Math.sqrt(....)
That will only work for primitives. The OP mentioned BigIntegers.
@OP, have a look at this article:
http://www.merriampark.com/bigsqrt.htm
Good luck.
> That will only work for primitives. The OP mentioned
> BigIntegers.
I guess in this scenario we can take the value from the BigInteger(or any other wrapper class) by:
BigDecimal bigDecimal = new BigDecimal(4); // The value of 4 is just for the sake of example.
double val = Math.sqrt(bigDecimal.doubleValue());
..and there you are!! If anything wrong with the code please do suggest me the corrections. I will appreciate the response:).
Thank you.
> ..and there you are!! If anything wrong with the code
> please do suggest me the corrections. I will
> appreciate the response:).
> Thank you.
By using BigDecimal.doubleValue() you're back to primitives : you lose the benefit of BigDecimal's arbitrary-precision and take the risk of getting Double.INFINITY
Hi guys,Thanks for making me aware of that issue of loosing the benifit of BigDecimal over primitives. I am not sure what the benefits are, but now the task is mine to search the web for an answer.Thank you.