使用任意精度的高级 Java BigDecimal 数学函数(pow、sqrt、log、sin...)。

介绍了如何使用 big-math 库中的 BigDecimalMath 类进行 Java 中的高精度数学运算,包括指数、对数、三角函数等,并展示了如何通过依赖管理引入该库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用 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^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
  • 正在更新中..................................

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SummerGao.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值