Java Bigdecimal Trigonometric Methods

Java BigDecimal trigonometric methods

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.

How do you get accurate results for sine values with BigDecimal in Java?

As Erwin recommended, I found a library that work for my needs.
BigDecimalMath
It has a generous license and fixed my problem with these particular arrays when I set the accuracy to 1074 decimal places, which is the maximum absolute value of the negative exponent of a Java primitive double value.

Thank you again for your help Erwin!

Higher precision doubles and trigonometric functions in Java

I'm pretty sure primitives won't get you there, but the BigDecimal class is as good as it gets (for everything except trigonometry).

For trigonometric functions, however, you will have to resort to an external library, like APFloat (see this previous question).

Arbitrary pre-set precision decimals (Almost like BigDecimal)

I would suggest you an intermediate way in between the proposals you made:

The Apfloat library is a good way to try with, check out the API docs.

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

So here you would have a big freedom on overriding and enhancing new methods just applying this class.

Kotlin Calculating with BigDecimal vs Double

In the BigDecimal variant, try replacing erg += (zaehler / nenner) with erg += (zaehler.divide(nenner, 20, RoundingMode.HALF_EVEN))

I suspect that the defaults for scaling the division results (as described here https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/math/BigDecimal.html) are not what you want.


BTW - I assume that performance is not part of the exercise, otherwise your implementation of factorial is a low hanging fruit.



Related Topics



Leave a reply



Submit