Java BigDecimal Square Root sqrt(BigDecimal value, MathContext mc)

Here you can find the source of sqrt(BigDecimal value, MathContext mc)

Description

Extra precise sqrt function for use with BigDecimal class.

License

GNU General Public License

Parameter

Parameter Description
value a parameter
mc a parameter

Return

square root of value

Declaration

public static final BigDecimal sqrt(BigDecimal value, MathContext mc) 

Method Source Code

//package com.java2s;
/*/*from w  w  w  . j  a va2  s .com*/
 * Copyright 2014 Jon N. Marsh.
    
 * Because small portions of this software are derived from Apache Commons Math
 * and GNU Scientific Library routines, it is licensed under GPLv3, which is
 * compatible with Apache Software License 2.0:
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses/.
 */

import java.math.BigDecimal;
import java.math.MathContext;

public class Main {
    /**
     * Extra precise sqrt function for use with BigDecimal class. Uses Newton's
     * method to roughly double the number of significant digits of typical
     * floating-point sqrt function. (This gem was found on StackOverflow.com)
     *
     * @param value
     * @param mc
     * @return square root of {@code value}
     */
    public static final BigDecimal sqrt(BigDecimal value, MathContext mc) {
        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));
    }
}

Related

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