Android : Java - Math Calculations - BigDecimal

on Monday, April 13, 2015


My question is related to math and how one should go about calculating "financial" mathematical equations with Java with emphasis on Android development. According to Philip Barker✝ the author of "Java Methods for Financial Engineering" developers should take special care with regards to accuracy and error when dealing with numerical approximations. Below is a quote from his book which should make things clearer for this discussion.



"Since we are largely dealing with numeric approximations or iterative convergence to a desired solution the discussion of accuracy and error are important.


In the decimal system irrational (e.g. √2) and transcendental (e.g. 𝛑) numbers cannot have a precise representation, most rational numbers are also not represented precisely, in decimal notation. Representing 1∕3 as a decimal can be approximated by 0.3 or 0.333333333 or some other arbitrarily large representation. Providing a decimal value for 1∕3, means representing the division as a floating point number.”



In order to make things simpler for development my question is the following:


1- Given that I know what my calculations should expect with regards to "scale" and "precision"✝ are there any drawbacks from making all calculations within my app with BigDecimal type? I want to keep things simple and this seems to work as far as financial equations are concerned (e.g. ROI, Compound Interest Rate, etc…). What do you think?


2- I noticed that the core Java library does not provide methods for calculating BigDecimals when the answer falls within the "irrational" or "transcendental" spectrum. This is probably because Sun or Oracle decided to leave the “burden of proof” to the developer to extend in its own application what is considered to be acceptable in terms of numerical accuracy. However, I discovered a BigBecimal library that seems to implement most of the methods omitted in the core library related to irrational and transcendental numbers. Its a library found at the Cornell University Library… you can get there by clicking here or here for the files. Would this library allow me to calculate financial equations without having to cast outside BigDecimal type except at the result toString()?


Any feedback on these questions would be highly appreciated.


✝ His book is a great read if you are interested in Java Financial Math.


✝✝ If you are unfamiliar with “scale & precision” you can read a great answer here.


0 comments:

Post a Comment