views:

235

answers:

3

Hi folks,

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 :)

Thanks for shouts and helping thoughts!

Regards from Germany

Marco

+1  A: 

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.

Big Square Roots uses the Newton methods to approximate square roots on BigDecimals.

cletus
A: 

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

Milan Ramaiya
Only for some definitions of best. 'broadest' possibly, but always check the algorithms against other sources, and never use the implementations directly - at least if they haven't updated the 1 based indexing in the C version using undefined behaviour.
Pete Kirkham
+1  A: 

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.

Scott Fines
Just found it myself and have seen that it supports pretty much everything I need. I will keep you posted if everything works
Marco