views:

970

answers:

3

Hi!

I want to implement math function power to double, can you advice algorithm for this?

I've reviewed sources of Java ME Open Source Software - Math but I want to implement it from the scratch.

Thank you!

+3  A: 

Perhaps this site is of some help: http://today.java.net/pub/a/today/2007/11/06/creating-java-me-math-pow-method.html

kusma
+1, everything one needs to know!
Carl Smotricz
+1 and accepted, thanks :)!
Max Gontar
+2  A: 

I don't know J2ME well enough to know, but do you have Math.log() and Math.exp() ?

Then you can simply use this relation:

x^y = exp(y * log(x))

If you don't have the aforementioned two functions, then you should start by implementing those. As far as I know, the above relation is the only reasonable way to compute x^y.


Update: I see the paper linked in kusman's answer shows an alternative way to do pow using the idea of a fractional exponent. Quite cool! But the paper also shows the "normal" way to do things via multiplication of the log, and shows you how to implement Taylor series for exp() and log().

Carl Smotricz
+1, thank you for answer!
Max Gontar
Not by much! But seriously, all floating point operations involving numbers other than "small" integers are subject to rounding errors. In this case, since log(5) is not an integer... it's to be expected. If you want to avoid rounding errors when your inputs happen to be integers, the fractional method will shine, of course.
Carl Smotricz
A: 

In java Programming Functions Math.log , Math.pow exist,But in mobile programming these two functions don't exist. I have the same problem too.I think we can access to source code of libraries in java and it may be a good way to write these functions for mobile applications.

Hosein