Here you can find the source of sqrtProcedure(MathContext mc, int digits, BigDecimal numberToBeSquareRooted, BigDecimal iteration1, BigDecimal iteration2, BigDecimal temp1, BigDecimal temp2)
Parameter | Description |
---|---|
mc | a parameter |
digits | a parameter |
numberToBeSquareRooted | a parameter |
iteration1 | a parameter |
iteration2 | a parameter |
temp1 | a parameter |
temp2 | a parameter |
private static BigDecimal sqrtProcedure(MathContext mc, int digits, BigDecimal numberToBeSquareRooted, BigDecimal iteration1, BigDecimal iteration2, BigDecimal temp1, BigDecimal temp2)
//package com.java2s; //License from project: Open Source License import java.math.BigDecimal; import java.math.MathContext; public class Main { public static final BigDecimal TWO = BigDecimal.valueOf(2); /**// w w w. j av a 2s. co m * Square root by coupled Newton iteration, sqrtProcedure() is the iteration part I adopted the Algorithm from the * book "Pi-unleashed", so now it looks more natural I give sparse math comments from the book, it assumes argument * mc precision >= 1 * * @param mc * @param digits * @param numberToBeSquareRooted * @param iteration1 * @param iteration2 * @param temp1 * @param temp2 * @return */ private static BigDecimal sqrtProcedure(MathContext mc, int digits, BigDecimal numberToBeSquareRooted, BigDecimal iteration1, BigDecimal iteration2, BigDecimal temp1, BigDecimal temp2) { // next v // g = 1 - 2*x*v temp1 = BigDecimal.ONE.subtract(TWO.multiply(iteration1, mc).multiply(iteration2, mc), mc); iteration2 = iteration2.add(temp1.multiply(iteration2, mc), mc); // v += g*v ~ 1/2/sqrt(d) // next x temp2 = numberToBeSquareRooted.subtract(iteration1.multiply(iteration1, mc), mc); // e = d - x^2 iteration1 = iteration1.add(temp2.multiply(iteration2, mc), mc); // x += e*v ~ sqrt(d) // increase precision int m = mc.getPrecision(); if (m < 2) m++; else m = m * 2 - 1; // next Newton iteration supplies so many exact digits if (m < 2 * digits) // digits limit not yet reached? { mc = new MathContext(m, mc.getRoundingMode()); // apply new precision sqrtProcedure(mc, digits, numberToBeSquareRooted, iteration1, iteration2, temp1, temp2); // next iteration } return iteration1; // returns the iterated square roots } }