Java BigDecimal 三角方法

发布于 2024-08-20 06:39:20 字数 363 浏览 3 评论 0原文

我正在开发一个数学解析器,它能够评估像 '5+b*sqrt(c^2)' 这样的字符串。我正在使用 ANTLR 进行解析并取得了良好的进展。现在我对 Java 类 BigDecimal 产生了兴趣,并想:嘿,为什么不考虑这里的精度呢。

我的问题是,Java API 没有为 BigDecimal 提供三角方法,例如 java.lang.Math。您知道是否有像 Apache Commons 这样好的数学库可以解决这个问题?

其他问题是如何实现幂方法,以便我可以用 BigDecimal 计算 4.9 ^ 1.4。这可能吗?

关于数值计算的书籍请求也将受到赞赏。

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 BigDecimal and thought: hey, why not thinking about precision here.

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?

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

A book request about numerical computing is also appreciated.

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

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

发布评论

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

评论(5

土豪我们做朋友吧 2024-08-27 06:39:20

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

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

静谧幽蓝 2024-08-27 06:39:20

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

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

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

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.

忘年祭陌 2024-08-27 06:39:20

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

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

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

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

哽咽笑 2024-08-27 06:39:20

关于数值计算的最好的书几乎是Numerical Recipes

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

双马尾 2024-08-27 06:39:20

使用Java BigDecimals的现有功能,即允许有限精度算术,如此处所述,我最近实现了 sqrt/1, exp /1、tan/1 等。对于这些数字对象。

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

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

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

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

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.

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.

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