Java Utililty Methods BigDecimal Square Root

List of utility methods to do BigDecimal Square Root

Description

The list of methods to do BigDecimal Square Root are organized into topic(s).

Method

BigDecimalsqrt(BigDecimal number)
Calcualtes the square root of the number.
int digits; 
BigDecimal numberToBeSquareRooted;
BigDecimal iteration1;
BigDecimal iteration2;
BigDecimal temp1 = null;
BigDecimal temp2 = null; 
int extraPrecision = number.precision();
MathContext mc = new MathContext(extraPrecision, RoundingMode.HALF_UP);
...
BigDecimalsqrt(BigDecimal number, RoundingMode rounding)
sqrt
return sqrt(number, BigDecimalONE, rounding);
BigDecimalsqrt(BigDecimal original, int scale)
sqrt
BigDecimal temp = new BigDecimal(String.valueOf(original));
BigDecimal results = new BigDecimal("1.0");
results.setScale(scale + 2);
int loops = 0;
while (true) {
    if (loops >= 1) {
        temp = new BigDecimal(String.valueOf(results));
    temp.setScale(scale + 2, BigDecimal.ROUND_FLOOR);
    results = original.divide(temp, scale + 2, BigDecimal.ROUND_FLOOR).add(temp)
            .divide(new BigDecimal("2.0"), scale + 2, BigDecimal.ROUND_FLOOR);
    if (temp.equals(results))
        break;
    loops++;
return results.setScale(scale, BigDecimal.ROUND_HALF_UP);
BigDecimalsqrt(BigDecimal randicand)
Returns the square root of a BigDecimal.
MathContext context = new MathContext(randicand.precision());
BigDecimal two = new BigDecimal(2);
BigDecimal result = randicand.divide(two, context);
boolean finished = false;
int iterations = context.getPrecision() + 1;
int count = 0;
while (!finished && count < iterations) {
    BigDecimal next = randicand.divide(result, context);
...
BigDecimalsqrt(BigDecimal value)
sqrt
double val = Math.sqrt(value.doubleValue());
if (Double.isNaN(val) || Double.isInfinite(val))
    return value;
BigDecimal x = new BigDecimal(val);
return x.add(new BigDecimal(value.subtract(x.multiply(x)).doubleValue() / (x.doubleValue() * 2.0)));
BigDecimalsqrt(BigDecimal value, int decimalPlaces)
sqrt
BigDecimal zero = BigDecimal.ZERO.setScale(decimalPlaces + 10);
BigDecimal one = BigDecimal.ONE.setScale(decimalPlaces + 10);
BigDecimal two = new BigDecimal("2").setScale(decimalPlaces + 10);
BigDecimal maxerr = one.movePointLeft(decimalPlaces);
BigDecimal lower = zero;
BigDecimal upper = value.compareTo(one) <= 0 ? one : value;
BigDecimal mid = BigDecimal.ZERO;
while (true) {
...
BigDecimalsqrt(BigDecimal value, MathContext mc)
Extra precise sqrt function for use with BigDecimal class.
BigDecimal x = new BigDecimal(Math.sqrt(value.doubleValue()), mc);
return x.add(new BigDecimal(value.subtract(x.multiply(x)).doubleValue() / (x.doubleValue() * 2.0), mc));
BigDecimalsqrt(BigDecimal x)
Compute the square root of x to a given scale, x >= 0.
if (x.signum() < 0) {
    throw new ArithmeticException("x < 0");
BigInteger n = x.movePointRight(SCALE << 1).toBigInteger();
int bits = (n.bitLength() + 1) >> 1;
BigInteger ix = n.shiftRight(bits);
BigInteger ixPrev;
do {
...
ArrayListsqrt(final ArrayList data)
sqrt
final double sum = 0.0;
final ArrayList ret = new ArrayList();
for (int i = 0; i < data.size(); ++i) {
    final Double val = data.get(i).doubleValue() * data.get(i).doubleValue();
    ret.add(i, val);
return ret;
BigDecimalsqrtNewtonRaphson(BigDecimal c, BigDecimal xn, BigDecimal precision)
Private utility method used to compute the square root of a 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();
if (currentPrecision.compareTo(precision) <= -1) {
...