Java 的 BigDecimal.power(BigDecimal exponent):是否有 Java 库可以做到这一点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16441769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:56:28  来源:igfitidea点击:

Java's BigDecimal.power(BigDecimal exponent): Is there a Java library that does it?

java

提问by Geoffrey De Smet

Java's BigDecimal.pow(int)method only accepts an integer parameter, no BigDecimalparameter.

Java的BigDecimal.pow(int)方法只接受一个整型参数,没有BigDecimal参数。

Is there a library, like Apache's commons-lang, that supports BigDecimal.pow(BigDecimal)? It should be able to do calculate "1.21".pow("0.5")to return "1.1".

是否有像 Apache 的 commons-lang 这样的库支持BigDecimal.pow(BigDecimal)?应该可以做计算"1.21".pow("0.5")返回"1.1"

采纳答案by higuaro

There is a Math.BigDecimal implementation of core mathematical functionswith source code available from the Cornell University Library here(also you can download the library as a tar.gz). Here is a sample of the library use:

有一个Math.BigDecimal实现的核心数学函数与来自康奈尔大学图书馆可用的源代码在这里(你也可以下载库作为tar.gz的)。这是库使用的示例:

import org.nevec.rjm.*;
import java.math.BigDecimal;

public class test {
    public static void main(String... args) {
        BigDecimal a = new BigDecimal("1.21");
        BigDecimal b = new BigDecimal("0.5");

        System.out.println(BigDecimalMath.pow(a, b).toString());
    }
}

Prints out:

打印出来:

1.1


Update

更新

The license information is now clear in the May 2015 update:

在 2015 年 5 月的更新中,许可证信息现在很清楚:

The full source code is made available under the LGPL v3.0.

完整的源代码在 LGPL v3.0 下可用。

回答by rajesh

Havent used, but saw suanshu. An open source version in github (https://github.com/nmdev2020/SuanShu).

没用过,看过酸书。github 中的开源版本 ( https://github.com/nmdev2020/SuanShu)。

BigDecimalUtils has a pow() which suits your needs

BigDecimalUtils 有一个适合您需求的 pow()

public static java.math.BigDecimal pow(java.math.BigDecimal a,
                                       java.math.BigDecimal b)
Compute a to the power of b.

回答by tstorms

The Apfloat library seems to be doing this, check the API docs.

Apfloat 库似乎正在这样做,请查看API 文档

ApcomplexMath.pow(Apcomplex z, Apcomplex w)

Apcomplex is a wrapper class for e.g. a BigDecimal.

Apcomplex 是一个包装类,例如 BigDecimal。