use*_*200 55 java bigdecimal square-root
我们是否可以BigDecimal仅使用Java API而不是定制的100行算法来计算Java中的平方根?
hav*_*ked 31
我用过这个,效果很好. 这是算法如何在高级别工作的示例.
编辑:我很想知道下面定义的准确程度.以下是来自官方来源的sqrt(2):
(first 200 digits) 1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147
Run Code Online (Sandbox Code Playgroud)
在这里它使用我在下面概述的方法SQRT_DIG等于150:
(first 200 digits) 1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206086685
Run Code Online (Sandbox Code Playgroud)
第一个偏差发生在195位精度之后.如果您需要如此高的精确度,请自担风险.
更改SQRT_DIG为1000会产生1570位精度.
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 fx = xn.pow(2).add(c.negate());
BigDecimal fpx = xn.multiply(new BigDecimal(2));
BigDecimal xn1 = fx.divide(fpx,2*SQRT_DIG.intValue(),RoundingMode.HALF_DOWN);
xn1 = xn.add(xn1.negate());
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));
}
Run Code Online (Sandbox Code Playgroud)
一定要看看barwnikk的答案.它更简洁,看似提供更好或更好的精度.
bar*_*ikk 28
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.add(x0);
x1 = x1.divide(TWO, SCALE, ROUND_HALF_UP);
}
return x1;
}
Run Code Online (Sandbox Code Playgroud)
这项工作完美!速度超过65536位!
小智 8
通过使用Karp的技巧,这可以在没有仅仅两行的循环的情况下实现,给出32位精度:
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)));
}
Run Code Online (Sandbox Code Playgroud)
如果只需要找到整数平方根 - 这两种方法都可以使用.
牛顿的方法 - 即使对于1000位数BigInteger也非常快:
public static BigInteger sqrtN(BigInteger in) {
final BigInteger TWO = BigInteger.valueOf(2);
int c;
// Significantly speed-up algorithm by proper select of initial approximation
// As square root has 2 times less digits as original value
// we can start with 2^(length of N1 / 2)
BigInteger n0 = TWO.pow(in.bitLength() / 2);
// Value of approximate value on previous step
BigInteger np = in;
do {
// next approximation step: n0 = (n0 + in/n0) / 2
n0 = n0.add(in.divide(n0)).divide(TWO);
// compare current approximation with previous step
c = np.compareTo(n0);
// save value as previous approximation
np = n0;
// finish when previous step is equal to current
} while (c != 0);
return n0;
}
Run Code Online (Sandbox Code Playgroud)
public static BigInteger sqrtD(final BigInteger in) {
final BigInteger TWO = BigInteger.valueOf(2);
BigInteger n0, n1, m, m2, l;
int c;
// Init segment
n0 = BigInteger.ZERO;
n1 = in;
do {
// length of segment
l = n1.subtract(n0);
// middle of segment
m = l.divide(TWO).add(n0);
// compare m^2 with in
c = m.pow(2).compareTo(in);
if (c == 0) {
// exact value is found
break;
} else if (c > 0) {
// m^2 is bigger than in - choose left half of segment
n1 = m;
} else {
// m^2 is smaller than in - choose right half of segment
n0 = m;
}
// finish if length of segment is 1, i.e. approximate value is found
} while (l.compareTo(BigInteger.ONE) > 0);
return m;
}
Run Code Online (Sandbox Code Playgroud)
Nim*_*sky -3
BigDecimal.valueOf(Math.sqrt(myBigDecimal.doubleValue()));
Run Code Online (Sandbox Code Playgroud)