Java BigDecimal Square Root sqrt(BigDecimal randicand)

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

Description

Returns the square root of a BigDecimal.

License

Apache License

Parameter

Parameter Description
BigDecimalrandicand the input BigDecimal randicand

Return

the result

Declaration

static public BigDecimal sqrt(BigDecimal randicand) 

Method Source Code

//package com.java2s;
/**//ww  w. j a va 2 s .  c  om
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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;
import java.math.MathContext;

public class Main {
    /**
     * Returns the square root of a big integer
     * @param    BigInteger randicand    the input big integer randicand
     * @return                the result
     */
    public static BigInteger sqrt(BigInteger randicand) {

        BigInteger current = BigInteger.ZERO.setBit(randicand.bitLength() / 2);
        BigInteger next = current;

        while (true) {

            BigInteger result = current.add(randicand.divide(current)).shiftRight(1);

            if (result.equals(current) || result.equals(next)) {
                return result;
            }

            next = current;
            current = result;
        }
    }

    /**
     * Returns the square root of a BigDecimal.
     * @param    BigDecimal randicand    the input BigDecimal randicand
     * @return                 the result
     */
    static public BigDecimal sqrt(BigDecimal randicand) {

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

            next = next.add(result);
            next = next.divide(two, context);

            finished = next.equals(result);
            result = next;

            iterations++;
        }

        return result;
    }

    /**
     * Returns the square root of a BigDecimal.
     * @param    BigDecimal randicand    the input BigDecimal randicand
     * @param    MathContextcontext      the math context
     * @return                 the result
     */
    static public BigDecimal sqrt(BigDecimal randicand, MathContext context) {

        BigDecimal two = new BigDecimal(2, context);

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

            next = next.add(result);
            next = next.divide(two, context);

            finished = next.equals(result);
            result = next;

            iterations++;
        }

        return result;
    }
}

Related

  1. sqrt(BigDecimal number)
  2. sqrt(BigDecimal number, RoundingMode rounding)
  3. sqrt(BigDecimal original, int scale)
  4. sqrt(BigDecimal value)
  5. sqrt(BigDecimal value, int decimalPlaces)
  6. sqrt(BigDecimal value, MathContext mc)
  7. sqrt(BigDecimal x)