private static BigDecimal _arctanTaylor ( final BigDecimal x , final int scale ) { final int sp1 = scale + 1 ; int i = 3 ; boolean addFlag = false ; BigDecimal power = x ; BigDecimal sum = x ; BigDecimal term ; // Convergence tolerance = 5*(10^-(scale+1)) final BigDecimal tolerance = BigDecimal . valueOf ( 5 ) . movePointLeft ( sp1 ) ; // Loop until the approximations converge // (two successive approximations are within the tolerance). do { // x^i power = power . multiply ( x ) . multiply ( x ) . setScale ( sp1 , RoundingMode . HALF_EVEN ) ; // (x^i)/i term = power . divide ( BigDecimal . valueOf ( i ) , sp1 , RoundingMode . HALF_EVEN ) ; // sum = sum +- (x^i)/i sum = addFlag ? sum . add ( term ) : sum . subtract ( term ) ; i += 2 ; addFlag = ! addFlag ; Thread . yield ( ) ; } while ( term . compareTo ( tolerance ) > 0 ) ; return sum ; }
Compute the arctangent of x to a given scale by the Taylor series |x| < 1