Java BigDecimal Square Root sqrt(BigDecimal x)

Here you can find the source of sqrt(BigDecimal x)

Description

Compute the square root of x to a given scale, x >= 0.

License

Apache License

Parameter

Parameter Description
x the value of x

Return

the result value

Declaration

public static BigDecimal sqrt(BigDecimal x) 

Method Source Code


//package com.java2s;
/*//from  w  w w .  j  a  v a2s  . co m
 * $Id$
 *
 * Copyright 2013 Valentyn Kolesnikov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.math.BigDecimal;
import java.math.BigInteger;

public class Main {
    private static final int SCALE = 18;

    /**
     * Compute the square root of x to a given scale, x >= 0.
     * Use Newton's algorithm.
     * @param x the value of x
     * @return the result value
     */
    public static BigDecimal sqrt(BigDecimal x) {
        // Check that x >= 0.
        if (x.signum() < 0) {
            throw new ArithmeticException("x < 0");
        }

        // n = x*(10^(2*SCALE))
        BigInteger n = x.movePointRight(SCALE << 1).toBigInteger();

        // The first approximation is the upper half of n.
        int bits = (n.bitLength() + 1) >> 1;
        BigInteger ix = n.shiftRight(bits);
        BigInteger ixPrev;

        // Loop until the approximations converge
        // (two successive approximations are equal after rounding).
        do {
            ixPrev = ix;

            // x = (x + n/x)/2
            ix = ix.add(n.divide(ix)).shiftRight(1);

            Thread.yield();
        } while (ix.compareTo(ixPrev) != 0);

        return new BigDecimal(ix, SCALE);
    }
}

Related

  1. sqrt(BigDecimal original, int scale)
  2. sqrt(BigDecimal randicand)
  3. sqrt(BigDecimal value)
  4. sqrt(BigDecimal value, int decimalPlaces)
  5. sqrt(BigDecimal value, MathContext mc)
  6. sqrt(final ArrayList data)
  7. sqrtNewtonRaphson(BigDecimal c, BigDecimal xn, BigDecimal precision)
  8. sqrtProcedure(MathContext mc, int digits, BigDecimal numberToBeSquareRooted, BigDecimal iteration1, BigDecimal iteration2, BigDecimal temp1, BigDecimal temp2)