Is there a method in j2me that raises a number to a specified exponent..just like the Math.pow() int the standard edition?
is there an operator for this?
Thanks..
Is there a method in j2me that raises a number to a specified exponent..just like the Math.pow() int the standard edition?
is there an operator for this?
Thanks..
afaik, no. But it should be easy to calculate the value using a simple for-loop.
There are other packages like mathfp and henson.midp.float to do floating point math, but if it's a simple pow(int a,int b), then I'd use a for loop.
thank you for your quick response. its not so simple actually. i was planning to raise an integer to a double value:
int pixel ^ 0.9;
then try the henson.midp.float or mathfp packages. Do a google search on em.
gluuck.
It's been over 4 years since this question was posted. Is there any inbuilt way of doing a pow() now?
no there is no such function but one can implement it nd here is code
long pow(long exp)
{
long result=1;
for(int i=0; i<exp ; i++)
{
result *= 2;
}
return result;
}
Regards,
Saurabh
Actually, what you've written is "1 << exp".
An integer version of Math.pow() is simple enough:
Hmmm... I think that's right.PHP Code:long pow(int a, int b) {
if (b < 0) {
throw new IllegalArgumentException("negative powers not supported");
}
long r = 1;
for (; b > 0; b--) {
r *= a;
}
return r;
}
Graham.
well if you feel like working a bit to get an accurate result, then you can search a Tylor series for a function a^x (a been the value, and x been the parameter), then just calculate the value until n==5 the accuracy should be 0.000001 perhaps even more accurate then this.
Thanks,
Adam Zehavi.
Actually I my requirements are different I have implemented code according to my requirements and I never said to implement my code ofcourse use it or modify to suit your needs
Regards,
Saurabh
Actually Graham is totally right, and what Saurabh has implemented is NOT equivalent to Math.pow()
What Saurabh has shown is how to produce a power of 2 only. e.g. 2^0, 2^1, 2^2, 2^3 etc... (1, 2, 4, 8, etc...).
Graham shows the correct way of calculating powers in the form a^b (e.g. where if a = 2 for all calculations, you get Saurabh's solution).
I thought it had best be clarified incase someone reading this thread tries to implement the first (and incorrect) solution shown in this thread.