/ 猿问

# Java中BigDecimal的平方根

2019-10-18 15:01:28

## 3 回答

public static BigDecimal sqrt(BigDecimal value) {

BigDecimal x = new BigDecimal(Math.sqrt(value.doubleValue()));

return x.add(new BigDecimal(value.subtract(x.multiply(x)).doubleValue() / (x.doubleValue() * 2.0)));

}

1 反对 回复 2019-10-18

(first 200 digits) 1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147

(first 200 digits) 1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206086685

private static final BigDecimal SQRT_DIG = new BigDecimal(150);

private static final BigDecimal SQRT_PRE = new BigDecimal(10).pow(SQRT_DIG.intValue());

/**

* Private utility method used to compute the square root of a BigDecimal.

*

* @author Luciano Culacciatti

* @url http://www.codeproject.com/Tips/257031/Implementing-SqrtRoot-in-BigDecimal

*/

private static BigDecimal sqrtNewtonRaphson  (BigDecimal c, BigDecimal xn, BigDecimal precision){

BigDecimal fpx = xn.multiply(new BigDecimal(2));

BigDecimal xn1 = fx.divide(fpx,2*SQRT_DIG.intValue(),RoundingMode.HALF_DOWN);

BigDecimal currentSquare = xn1.pow(2);

BigDecimal currentPrecision = currentSquare.subtract(c);

currentPrecision = currentPrecision.abs();

if (currentPrecision.compareTo(precision) <= -1){

return xn1;

}

return sqrtNewtonRaphson(c, xn1, precision);

}

/**

* Uses Newton Raphson to compute the square root of a BigDecimal.

*

* @author Luciano Culacciatti

* @url http://www.codeproject.com/Tips/257031/Implementing-SqrtRoot-in-BigDecimal

*/

public static BigDecimal bigSqrt(BigDecimal c){

return sqrtNewtonRaphson(c,new BigDecimal(1),new BigDecimal(1).divide(SQRT_PRE));

}

public static BigDecimal sqrt(BigDecimal A, final int SCALE) {

BigDecimal x0 = new BigDecimal("0");

BigDecimal x1 = new BigDecimal(Math.sqrt(A.doubleValue()));

while (!x0.equals(x1)) {

x0 = x1;

x1 = A.divide(x0, SCALE, ROUND_HALF_UP);

x1 = x1.divide(TWO, SCALE, ROUND_HALF_UP);

}

return x1;

}

• 3 回答
• 0 关注
• 401 浏览

0/150