Log in

View Full Version : Power Function


tyandjel94
02-10-2012, 17:23
I was wondering the best way to make a power function for any number. I came up with the conclusion of using Newton's root finding Method which computes is a method for finding successively better approximations to the roots (or zeroes) of a real-valued function. I was able to find that this worked perfectly on a calculator, but it didn't work when you program it. This is because the long, double, and float data types are not long enough. A bigDecimal is needed, but the FRC Java which is Java ME doesn't not have that class. Is there a way to work around this? Thanks a lot.

F22Rapture
02-10-2012, 17:44
Isn't there already a power function in Java.lang.Math?

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Math.html#pow%28double,%20double%29

tyandjel94
02-10-2012, 17:48
Yes in regular Java but not FRC version. Also in regular java its function is native so the class has no copyable code.

Ether
02-10-2012, 17:52
I was wondering the best way to make a power function for any number. I came up with the conclusion of using Newton's root finding Method which computes is a method for finding successively better approximations to the roots (or zeroes) of a real-valued function. I was able to find that this worked perfectly on a calculator, but it didn't work when you program it. This is because the long, double, and float data types are not long enough. A bigDecimal is needed, but the FRC Java which is Java ME doesn't not have that class. Is there a way to work around this? Thanks a lot.

I suspect you did not program it correctly. Double precision floats should have adequate resolution for this.

But there may be a better way. Does the Java FRC runtime math library support logarithms and exponentiation?

If so, there's a simple solution:

You want to compute:

power = base^x

... for any positive real base and x, correct?

To do so, substitute e^ln(base) for base to get:

power = e^(x*ln(base))

tyandjel94
02-10-2012, 18:05
No it doesn't have log or exponent functions. And no the number of precision has to be more than the max value of double (1.7976931348623157 *10^308). If it's not then the program keeps running down to the base value, then get a runtime error.

Ether
02-10-2012, 19:06
No it doesn't have log or exponent functions.

http://www.chiefdelphi.com/forums/showpost.php?p=1087763&postcount=4

Most of the underlying math primitives (e.g. forward and inverse trig, exponents, square roots, etc.) are present in the FRC Java implementation - they went ahead and implemented more than the bare bones J2ME spec requires (albeit in a different package structure than J2SE...you need to do some digging to find where some of the classes live)

tyandjel94
02-10-2012, 19:48
Ok thanks. I found it under squawk. Do you know if there are arrayLists, decimalFormat or java.net classes? Thanks again for your help.