在 Maven 项目中引入 : big-math使用 BigDecimalMath 类输出Java BigDecimal任意精度
big-math 源码地址: https://github.com/eobermuhlner/big-math
BigDecimalMath 类为以下方面提供了高效且准确的实现:ch.obermuhlner big-math2.3.0
log(BigDecimal, MathContext) exp(BigDecimal, MathContext) pow(BigDecimal, BigDecimal, MathContext) calculates x^y sqrt(BigDecimal, MathContext) root(BigDecimal, BigDecimal, MathContext) calculates the n'th root of x sin(BigDecimal, MathContext) cos(BigDecimal, MathContext) tan(BigDecimal, MathContext) asin(BigDecimal, MathContext) acos(BigDecimal, MathContext) atan(BigDecimal, MathContext) atan2(BigDecimal, BigDecimal, MathContext) sinh(BigDecimal, MathContext) cosh(BigDecimal, MathContext) tanh(BigDecimal, MathContext) asinh(BigDecimal, MathContext) acosh(BigDecimal, MathContext) atanh(BigDecimal, MathContext) pow(BigDecimal, long, MathContext) calculates x^y for long y factorial(int) calculates n! bernoulli(int) calculates Bernoulli numbers pi(MathContext) calculates pi to an arbitrary precision e(MathContext) calculates e to an arbitrary precision toBigDecimal(String) creates a BigDecimal from string representation (faster than BigDecimal(String) ) mantissa(BigDecimal) extracts the mantissa from a BigDecimal (mantissa * 10^exponent) exponent(BigDecimal) extracts the exponent from a BigDecimal (mantissa * 10^exponent) integralPart(BigDecimal) extracts the integral part from a BigDecimal (everything before the decimal point) fractionalPart(BigDecimal) extracts the fractional part from a BigDecimal (everything after the decimal point) isIntValue(BigDecimal) checks whether the BigDecimal can be represented as an int value isDoublevalue(BigDecimal) checks whether the BigDecimal can be represented as a double value roundWithTrailingZeroes(BigDecimal, MathContext) rounds a BigDecimal to an arbitrary precision with trailing zeroes. 实例
BigDecimal 幂运算:
求 12^(1/12) :
@Test
public void bigMathPowTest() {
MathContext mathContext = new MathContext(17); //指定计算结果的精度
BigDecimal a =new BigDecimal("12");
BigDecimal z= new BigDecimal("1");
BigDecimal m= new BigDecimal("12");
BigDecimal p= z.divide(m,18, RoundingMode.HALF_UP);
System.out.println(BigDecimalMath.pow(a,p,mathContext));
}
将在控制台上产生以下输出:
1.2300755055779713
正在更新中..................................



