public static BigDecimal sqrt ( @ Nonnull final BigDecimal x , final int scale ) { // Check that x >= 0. if ( x . signum ( ) < 0 ) { throw new IllegalArgumentException ( "x < 0: " + x ) ; } // n = x*(10^(2*scale)) final BigInteger n = x . movePointRight ( scale << 1 ) . toBigInteger ( ) ; // The first approximation is the upper half of n. final 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 ) ; }
Compute the square root of x to a given scale x &ge ; 0 . Use Newton s algorithm .