Java BigDecimal trigonometric methods

北城以北 提交于 2019-11-26 23:14:23

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.

cletus

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.

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

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

j4n bur53

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!