# Thread: URGENT: is there a pow() method in j2me?

1. 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..

2. 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.

3. 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;

4. then try the henson.midp.float or mathfp packages. Do a google search on em.

gluuck.

5. It's been over 4 years since this question was posted. Is there any inbuilt way of doing a pow() now?

6. 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;
}

7. Originally Posted by ingsaurabh
no there is no such function but one can implement it nd here is code
Actually, what you've written is "1 << exp".

An integer version of Math.pow() is simple enough:

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; }  ```
Hmmm... I think that's right.

Graham.

8. 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.

9. 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

10. Originally Posted by ingsaurabh
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
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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.