使用 BigDecimalMath 类输出Java BigDecimal任意精度
big-math 源码地址: https://github.com/eobermuhlner/big-math
在 Maven 项目中引入 : big-math
<dependency>
<groupId>ch.obermuhlner</groupId>
<artifactId>big-math</artifactId>
<version>2.3.0</version>
</dependency>
BigDecimalMath 类为以下方面提供了高效且准确的实现:
log(BigDecimal, MathContext)
exp(BigDecimal, MathContext)
pow(BigDecimal, BigDecimal, MathContext)
calculates x^ysqrt(BigDecimal, MathContext)
root(BigDecimal, BigDecimal, MathContext)
calculates the n'th root of xsin(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 forlong
yfactorial(int)
calculates n!bernoulli(int)
calculates Bernoulli numberspi(MathContext)
calculates pi to an arbitrary precisione(MathContext)
calculates e to an arbitrary precisiontoBigDecimal(String)
creates aBigDecimal
from string representation (faster thanBigDecimal(String)
)mantissa(BigDecimal)
extracts the mantissa from aBigDecimal
(mantissa * 10^exponent)exponent(BigDecimal)
extracts the exponent from aBigDecimal
(mantissa * 10^exponent)integralPart(BigDecimal)
extracts the integral part from aBigDecimal
(everything before the decimal point)fractionalPart(BigDecimal)
extracts the fractional part from aBigDecimal
(everything after the decimal point)isIntValue(BigDecimal)
checks whether theBigDecimal
can be represented as anint
valueisDoubleValue(BigDecimal)
checks whether theBigDecimal
can be represented as adouble
valueroundWithTrailingZeroes(BigDecimal, MathContext)
rounds aBigDecimal
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
- 正在更新中..................................