Java中BigDecimal的平方根 [英] Square root of BigDecimal in Java

查看:55
本文介绍了Java中BigDecimal的平方根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以仅使用 Java API 而不是定制的 100 行算法来计算 Java 中 BigDecimal 的平方根吗?

解决方案

我用过这个,效果很好.以下是该算法如何在高层次上工作的示例.

我很想知道下面定义的准确程度.这是来自官方来源的 sqrt(2):

<预> <代码>(前200位)1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147

这里使用的是我在下面概述的方法,SQRT_DIG 等于 150:

<预> <代码>(前200位)1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206086685

第一个偏差出现在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());/*** 用于计算 BigDecimal 平方根的私有实用方法.** @author Luciano Culacciatti* @url http://www.codeproject.com/Tips/257031/Implementing-SqrtRoot-in-BigDecimal*/私有静态 BigDecimal sqrtNewtonRaphson(BigDecimal c、BigDecimal xn、BigDecimal 精度){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();如果 (currentPrecision.compareTo(precision) <= -1){返回 xn1;}返回 sqrtNewtonRaphson(c, xn1, precision);}/*** 使用 Newton Raphson 计算 BigDecimal 的平方根.** @author Luciano Culacciatti* @url http://www.codeproject.com/Tips/257031/Implementing-SqrtRoot-in-BigDecimal*/公共静态 BigDecimal bigSqrt(BigDecimal c){返回 sqrtNewtonRaphson(c,new BigDecimal(1),new BigDecimal(1).divide(SQRT_PRE));}

请务必查看 barwnikk 的回答.它更简洁,似乎提供了同样或更好的精度.

Can we compute the square root of a BigDecimal in Java by using only the Java API and not a custom-made 100-line algorithm?

解决方案

I've used this and it works quite well. Here's an example of how the algorithm works at a high level.

Edit: I was curious to see just how accurate this was as defined below. Here is the sqrt(2) from an official source:

(first 200 digits) 1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147

and here it is using the approach I outline below with SQRT_DIG equal to 150:

(first 200 digits) 1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206086685

The first deviation occurs after 195 digits of precision. Use at your own risk if you need such a high level of precision as this.

Changing SQRT_DIG to 1000 yielded 1570 digits of precision.

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));
}

be sure to check out barwnikk's answer. it's more concise and seemingly offers as good or better precision.

这篇关于Java中BigDecimal的平方根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆