Java BigDecimal 三角方法
我正在开发一个数学解析器,它能够评估像 '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 BigDecimal
s 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 BigDecimal
s. Is this possible?
A book request about numerical computing is also appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
ApFloat 是一个包含三角函数和非整数幂的任意精度近似值的库;但是,它使用自己的内部表示形式,而不是
BigDecimal
和BigInteger
。我以前没有使用过它,所以我不能保证它的正确性或性能特征,但 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
andBigInteger
. I haven't used it before, so I can't vouch for its correctness or performance characteristics, but the api seems fairly complete.BigDecimal
不提供这些方法,因为BigDecimal
模拟有理数。三角函数、平方根和非整数的幂(我猜包括平方根)都会生成无理数。这些可以用任意精度的数字来近似,但精确值不能存储在 BigDecimal 中。这并不是他们真正的目的。如果您无论如何都在近似某些东西,您也可以只使用
double
。BigDecimal
does not provide these methods becauseBigDecimal
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 adouble
.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
关于数值计算的最好的书几乎是Numerical Recipes
Pretty much the best book on Numerical Computing would be Numerical Recipes
使用Java BigDecimals的现有功能,即允许有限精度算术,如此处所述,我最近实现了 sqrt/1, exp /1、tan/1 等。对于这些数字对象。
数值算法本身使用麦克劳林和泰勒级数,加上适当的范围缩小,以确保级数有足够的速度和广度。
这是一个计算示例,拉马努金常数:
这个东西是用 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:
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.