Java BigDecimal 三角函数方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2173512/
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-10-29 19:52:05  来源:igfitidea点击:

Java BigDecimal trigonometric methods

javaprecisiontrigonometrybigdecimal

提问by Marco

I am developing a mathematical parser which is able to evaluate String like '5+b*sqrt(c^2)'. I am using ANTLR for the parsing and make good progress. Now I fell over the Java class BigDecimaland thought: hey, why not thinking about precision here.

我正在开发一个数学解析器,它能够像'5+b*sqrt(c^2)'. 我正在使用 ANTLR 进行解析并取得了良好的进展。现在我对 Java 类BigDecimal感到困惑并想:嘿,为什么不在这里考虑精度。

My problem is that the Java API does not provide trigonometric methods for BigDecimals like java.lang.Math. Do you know if there are any good math libraries like Apache Commons out there that deal with this problem?

我的问题是 Java API 没有为BigDecimals 类提供三角函数方法java.lang.Math。您知道是否有像 Apache Commons 这样的优秀数学库可以解决这个问题?

The other questions is how to realize the power method so that I can calculate 4.9 ^ 1.4 with BigDecimals. Is this possible?

另一个问题是如何实现幂方法,以便我可以用BigDecimals计算 4.9 ^ 1.4 。这可能吗?

A book request about numerical computing is also appreciated.

还感谢有关数值计算的书籍请求。

采纳答案by Scott Fines

ApFloatis a library which contains arbitrary-precision approximations of trigometric functions and non-integer powers both; however, it uses its own internal representations, rather than BigDecimaland BigInteger. I haven't used it before, so I can't vouch for its correctness or performance characteristics, but the api seems fairly complete.

ApFloat是一个包含三角函数和非整数幂的任意精度近似值的库;但是,它使用自己的内部表示,而不是BigDecimalBigInteger。我以前没有使用过它,所以我不能保证它的正确性或性能特征,但 api 似乎相当完整。

回答by cletus

BigDecimaldoes not provide these methods because BigDecimalmodels a rational number. Trigonometric functions, square roots and powers to non-integers (which I guess includes square roots) all generate irrational numbers.

BigDecimal不提供这些方法,因为BigDecimal对有理数建模。三角函数、平方根和非整数的幂(我猜包括平方根)都会产生无理数。

These can be approximated with an arbitrary-precision number but the exact value can't be stored in a BigDecimal. It's not really what they're for. If you're approximating something anyway, you may as well just use a double.

这些可以用任意精度的数字来近似,但确切的值不能存储在BigDecimal. 这不是他们真正的目的。如果您无论如何都要近似某些东西,您也可以只使用double.

回答by Eric Obermühlner

The big-math library provides all the standard advanced mathematical functions (pow, sqrt, log, sin, ...) for BigDecimal.

big-math 库为 BigDecimal 提供了所有标准的高级数学函数(pow、sqrt、log、sin 等)。

https://github.com/eobermuhlner/big-math

https://github.com/eobermuhlner/big-math

回答by Mostowski Collapse

Using an existing feature of Java BigDecimals, namely to allow limited precision arithmetic as described here, I recently implemented sqrt/1, exp/1, tan/1, etc.. for these number objects.

使用Java BigDecimals的的现有特征,即允许所描述的有限精度运算在这里,我最近实施的sqrt / 1,EXP / 1,棕褐色/ 1等。对于这些数量的对象。

The numeric algorithms themselve use Maclaurin and Taylor series, plus appropriate range reductions to assure enough speed and breadth of the series.

数值算法本身使用麦克劳林和泰勒级数,加上适当的范围缩减以确保级数足够的速度和广度。

Here is an example calculation, Ramanujan's Constant:

这是一个示例计算,拉马努金常数:

Jekejeke Prolog 2, Runtime Library 1.1.8
(c) 1985-2017, XLOG Technologies GmbH, Switzerland

?- use_module(library(stream/console)).
% 0 consults and 0 unloads in 0 ms.
Yes

?- X is mp(exp(pi*sqrt(163)), 60).
X = 0d262537412640768743.999999999999250072597198185688879353856320

The thingy was written in mixture of Prolog and Java. The speed and accuracy of it is still work in progress. The code is currently open source on GitHub.

这个东西是用 Prolog 和 Java 混合编写的。它的速度和准确性仍在进行中。该代码目前在GitHub开源

回答by Reverend Gonzo

Pretty much the best book on Numerical Computing would be Numerical Recipes

几乎关于数值计算的最好的书是数值食谱