Java BigDecimal Square Root sqrt(BigDecimal number, RoundingMode rounding)

Here you can find the source of sqrt(BigDecimal number, RoundingMode rounding)

Description

sqrt

License

Apache License

Declaration

public static BigDecimal sqrt(BigDecimal number, RoundingMode rounding) 

Method Source Code

//package com.java2s;
/***************************************************************************
 * Copyright (c) 2016 the WESSBAS project
 *
 * 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./*from w  w  w  .  j  av a2  s  . c  o m*/
 ***************************************************************************/

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

public class Main {
    protected static BigInteger BigIntegerZERO = BigInteger.ZERO;
    protected static BigInteger BigIntegerONE = BigInteger.ONE;
    protected static BigInteger BigIntegerTWO = BigInteger.valueOf(2);
    protected static BigDecimal BigDecimalZERO = BigDecimal.ZERO;
    protected static BigDecimal BigDecimalONE = BigDecimal.ONE;
    protected static BigDecimal BigDecimalTWO = new BigDecimal(2);

    public static BigInteger sqrt(BigInteger number) {
        return sqrt(number, BigIntegerONE);
    }

    public static BigDecimal sqrt(BigDecimal number, RoundingMode rounding) {
        return sqrt(number, BigDecimalONE, rounding);
    }

    protected static BigInteger sqrt(BigInteger number, BigInteger guess) {
        // ((n/g) + g)/2: until same result twice in a row
        //    BigInteger result = number.divide(guess).add(guess).divide(BigIntegerTWO);
        //    if(result.compareTo(guess) == 0)
        //      return result;
        //
        //    return sqrt(number, result);

        // redoing this to avoid StackOverFlow
        BigInteger result = BigIntegerZERO;
        BigInteger flipA = result;
        BigInteger flipB = result;
        boolean first = true;
        while (result.compareTo(guess) != 0) {
            if (!first)
                guess = result;
            else
                first = false;

            result = number.divide(guess).add(guess).divide(BigIntegerTWO);
            // handle flip flops
            if (result.equals(flipB))
                return flipA;

            flipB = flipA;
            flipA = result;
        }
        return result;

    }

    public static BigDecimal sqrt(BigDecimal number, BigDecimal guess, RoundingMode rounding) {
        BigDecimal result = BigDecimalZERO;
        BigDecimal flipA = result;
        BigDecimal flipB = result;
        boolean first = true;
        while (result.compareTo(guess) != 0) {
            if (!first)
                guess = result;
            else
                first = false;

            result = number.divide(guess, rounding).add(guess).divide(BigDecimalTWO, rounding);
            // handle flip flops
            if (result.equals(flipB))
                return flipA;

            flipB = flipA;
            flipA = result;
        }
        return result;
    }
}

Related

  1. sqrt(BigDecimal number)
  2. sqrt(BigDecimal original, int scale)
  3. sqrt(BigDecimal randicand)
  4. sqrt(BigDecimal value)
  5. sqrt(BigDecimal value, int decimalPlaces)