Categories
Technology

J2ME Math

While developing a financial calculator on the J2ME platform (MIDP2.0, CLDC1.1) i had the need for some “basic” math functions like pow() and log(). As it is, there’s nothing quite like that natively available on the platform which on one hand surprised me; on the other, got me quite excited. I’d have to implement my own 🙂 Geek.

Now there’s a lot of discussion online and i’m not going to repeat all that here about the various math libraries and the reasons why the implementations aren’t available. What i will link to however, is a friendly, but relatively in-depth review of implementing your own pow() function. Incidentally, this article also implements an “inline” function for determining ln(x) in the dissection on using the Taylor series algorithm.

Wikipedia also have a pretty decent discussion on the fundamentals of logarithms which lead to more insight on the implementations discussed above.

So if you plan on doing math on J2ME, plan ahead. You will need to Google for some math libraries, or be prepared to roll your own. I prefer the roll-your-own method unless you need _all_ the features of library X. Keep it trim. And i get to apply some years of text-book learning 🙂

Big plus is you can unit-test it and luckily the laws of math don’t change too often so once you’ve written it, porting to other platforms/frameworks/languages in future is straightforward. Where as the entire library you might have linked to is only available on that one platform/language.