private static BigDecimal sqrtNewtonRaphson ( final BigDecimal c , final BigDecimal xn , final BigDecimal precision ) { BigDecimal fx = xn . pow ( 2 ) . add ( c . negate ( ) ) ; BigDecimal fpx = xn . multiply ( new BigDecimal ( 2 ) ) ; BigDecimal xn1 = fx . divide ( fpx , 2 * SQRT_DIG . intValue ( ) , RoundingMode . HALF_DOWN ) ; xn1 = xn . add ( xn1 . negate ( ) ) ; BigDecimal currentSquare = xn1 . pow ( 2 ) ; BigDecimal currentPrecision = currentSquare . subtract ( c ) ; currentPrecision = currentPrecision . abs ( ) ; if ( currentPrecision . compareTo ( precision ) <= - 1 ) { return xn1 ; } return sqrtNewtonRaphson ( c , xn1 , precision ) ; }
Private utility method used to compute the square root of a BigDecimal .