×

Discussion Board

Results 1 to 10 of 10
  1. #1
    Registered User
    Join Date
    Dec 2004
    Location
    quezon city philippines
    Posts
    25

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

    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. #2
    Regular Contributor
    Join Date
    Mar 2005
    Posts
    249
    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. #3
    Registered User
    Join Date
    Dec 2004
    Location
    quezon city philippines
    Posts
    25
    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. #4
    Regular Contributor
    Join Date
    Mar 2005
    Posts
    249
    then try the henson.midp.float or mathfp packages. Do a google search on em.

    gluuck.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    7

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

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

  6. #6
    Super Contributor
    Join Date
    Apr 2009
    Posts
    572

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

    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

  7. #7
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

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

    Quote Originally Posted by ingsaurabh View Post
    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 aint b) {
        if (
    0) {
            throw new 
    IllegalArgumentException("negative powers not supported");
        }
        
    long r 1;
        for (; 
    0b--) {
            
    *= a;
        }
        return 
    r;

    Hmmm... I think that's right.

    Graham.

  8. #8
    Regular Contributor
    Join Date
    Jun 2009
    Location
    Tel-Aviv Israel
    Posts
    410

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

    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.

  9. #9
    Super Contributor
    Join Date
    Apr 2009
    Posts
    572

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

    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

  10. #10
    Registered User
    Join Date
    Jun 2007
    Posts
    8

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

    Quote Originally Posted by ingsaurabh View Post
    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
  •  
×