如何在 Java 中对 BigDecimal 进行分数幂?

发布于 2024-09-16 08:05:21 字数 555 浏览 12 评论 0原文

在我的小项目中,我只需要对非常大的数字执行类似 Math.pow(7777.66, 5555.44) 的操作。我遇到了一些解决方案:

  • 使用 double - 但数字太大
  • 使用 BigDecimal.pow 但不支持小数
  • 使用 X^(A+B )=X^A*X^B 公式(B 是第二个数字的余数),但同样不支持 big X 或 big A 因为我仍然转换为 double
  • 使用某种泰勒级数算法或类似的东西 - 我不太擅长数学,所以如果我找不到任何解决方案(某些库),这是我的最后一个选择或公式(A+B)^(C+D))。

有谁知道图书馆或简单的解决方案?我发现很多人都遇到同样的问题...

ps 我发现一些名为 ApFloat 的库声称可以近似执行此操作,但我得到的结果非常近似,甚至 8^2 也给了我 60...

In my little project, I need to do something like Math.pow(7777.66, 5555.44) only with VERY big numbers. I came across a few solutions:

  • Use double - but the numbers are too big
  • Use BigDecimal.pow but no support for fractional
  • Use the X^(A+B)=X^A*X^B formula (B is the remainder of the second num), but again no support for big X or big A because I still convert to double
  • Use some kind of Taylor series algorithm or something like that - I'm not very good at math so this one is my last option if I don't find any solutions (some libraries or formula for (A+B)^(C+D)).

Does anyone know of a library or an easy solution? I figured that many people deal with the same problem...

p.s.
I found some library called ApFloat that claims to do it approximately, but the results I got were so approximate that even 8^2 gave me 60...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

糖果控 2024-09-23 08:05:21

对于 1.7976931348623157E308 (Double.MAX_VALUE) 下的参数但支持百万位数字的结果的解决方案:

由于 double 支持高达 MAX_VALUE 的数字(例如,double 中的 100! 如下所示:9.332621544394415E157),因此使用 BigDecimal 没有问题.doubleValue()。但你不应该只做 Math.pow(double, double) 因为如果结果大于 MAX_VALUE 你只会得到无穷大。 SO:使用公式 X^(A+B)=X^A*X^B 将计算分成两个幂,大幂使用 BigDecimal.pow,小幂(第二个参数的余数)使用 Math。 pow,然后乘以。 X 将被复制为 DOUBLE - 确保它不大于 MAX_VALUE,A 将是 INT(最大 2147483647,但 BigDecimal.pow 无论如何都不支持超过 10 亿的整数),B 将是 double,始终小于 1。这样您可以执行以下操作(忽略我的私有常量等):

    int signOf2 = n2.signum();
    try {
        // Perform X^(A+B)=X^A*X^B (B = remainder)
        double dn1 = n1.doubleValue();
        // Compare the same row of digits according to context
        if (!CalculatorUtils.isEqual(n1, dn1))
            throw new Exception(); // Cannot convert n1 to double
        n2 = n2.multiply(new BigDecimal(signOf2)); // n2 is now positive
        BigDecimal remainderOf2 = n2.remainder(BigDecimal.ONE);
        BigDecimal n2IntPart = n2.subtract(remainderOf2);
        // Calculate big part of the power using context -
        // bigger range and performance but lower accuracy
        BigDecimal intPow = n1.pow(n2IntPart.intValueExact(),
                CalculatorConstants.DEFAULT_CONTEXT);
        BigDecimal doublePow =
            new BigDecimal(Math.pow(dn1, remainderOf2.doubleValue()));
        result = intPow.multiply(doublePow);
    } catch (Exception e) {
        if (e instanceof CalculatorException)
            throw (CalculatorException) e;
        throw new CalculatorException(
            CalculatorConstants.Errors.UNSUPPORTED_NUMBER_ +
                "power!");
    }
    // Fix negative power
    if (signOf2 == -1)
        result = BigDecimal.ONE.divide(result, CalculatorConstants.BIG_SCALE,
                RoundingMode.HALF_UP);

结果示例:

50!^10! = 12.50911317862076252364259*10^233996181

50!^0.06 = 7395.788659356498101260513

The solution for arguments under 1.7976931348623157E308 (Double.MAX_VALUE) but supporting results with MILLIONS of digits:

Since double supports numbers up to MAX_VALUE (for example, 100! in double looks like this: 9.332621544394415E157), there is no problem to use BigDecimal.doubleValue(). But you shouldn't just do Math.pow(double, double) because if the result is bigger than MAX_VALUE you will just get infinity. SO: use the formula X^(A+B)=X^A*X^B to separate the calculation to TWO powers, the big, using BigDecimal.pow, and the small (remainder of the 2nd argument), using Math.pow, then multiply. X will be copied to DOUBLE - make sure it's not bigger than MAX_VALUE, A will be INT (maximum 2147483647 but the BigDecimal.pow doesn't support integers more than a billion anyway), and B will be double, always less than 1. This way you can do the following (ignore my private constants etc):

    int signOf2 = n2.signum();
    try {
        // Perform X^(A+B)=X^A*X^B (B = remainder)
        double dn1 = n1.doubleValue();
        // Compare the same row of digits according to context
        if (!CalculatorUtils.isEqual(n1, dn1))
            throw new Exception(); // Cannot convert n1 to double
        n2 = n2.multiply(new BigDecimal(signOf2)); // n2 is now positive
        BigDecimal remainderOf2 = n2.remainder(BigDecimal.ONE);
        BigDecimal n2IntPart = n2.subtract(remainderOf2);
        // Calculate big part of the power using context -
        // bigger range and performance but lower accuracy
        BigDecimal intPow = n1.pow(n2IntPart.intValueExact(),
                CalculatorConstants.DEFAULT_CONTEXT);
        BigDecimal doublePow =
            new BigDecimal(Math.pow(dn1, remainderOf2.doubleValue()));
        result = intPow.multiply(doublePow);
    } catch (Exception e) {
        if (e instanceof CalculatorException)
            throw (CalculatorException) e;
        throw new CalculatorException(
            CalculatorConstants.Errors.UNSUPPORTED_NUMBER_ +
                "power!");
    }
    // Fix negative power
    if (signOf2 == -1)
        result = BigDecimal.ONE.divide(result, CalculatorConstants.BIG_SCALE,
                RoundingMode.HALF_UP);

Results examples:

50!^10! = 12.50911317862076252364259*10^233996181

50!^0.06 = 7395.788659356498101260513
调妓 2024-09-23 08:05:21

根据 MIT 许可发布的 big-math 库有一个简单的静态帮助器 BigDecimalMath .log(BigDecimal, MathContext) 用于日志和 BigDecimal 中未包含的许多其他函数。使用非常简单,并且有大量基准测试数据来比较性能。

The big-math library released under MIT license has a simple static helper BigDecimalMath.log(BigDecimal, MathContext) for log and many other functions not included with BigDecimal. Very simple to use and has lots of benchmarking data to compare performance.

盛装女皇 2024-09-23 08:05:21

指数 = 对数。

看一下 BigDecimal 的对数

Exponents = logarithms.

Take a look at Logarithm of a BigDecimal

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文