-1

I am using j2me and I need to get quite precise exp() for values up to 4. Problem with the j2me is that it's Math library doesn't have pow() and exp() method. To solve this, I just used this method to implement pow():

public static double pow(double num1, double num2) {
  double result = 1;
  for (int i = 0; i < num2; i++)
    result *= num1;
  return result;
}

This enabled me to have exp functionality by using setting e as constant (2.718281828459045) and calling pow:

double calculation =  (20.386 - (5132.000 / (t + 273.15)));
System.out.println("calc: " + pow(2.71,calculation));
calculation = pow(2.7182818284590452,calculation) * 1.33;

My problem is that result is quite inaccurate, for example if I compare math.exp and my pow method for number 3,75, results are like this:

Pow function returns: 54.5980031309658

Math function returns: 42.52108200006278

So I would need advice, how to implement exp functionality in j2me environment with highest precision possible.

Jure
  • 799
  • 6
  • 25
  • Your function names no sense whatsoever for exponents that are not fractions. Are you surprised that this isn't correct? – duffymo Feb 01 '16 at 13:41
  • 1
    Possible duplicate of [J2ME power(double, double) math function implementation](http://stackoverflow.com/questions/2076913/j2me-powerdouble-double-math-function-implementation) – agold Feb 01 '16 at 13:43
  • @agold that duplicate has really bad and incomplete answers – Ferrybig Feb 01 '16 at 13:44
  • @Ferrybig: Yes you are right, but the [accepted answer](http://stackoverflow.com/a/2076930/1771479) (link only answer) has a good [link](https://community.oracle.com/docs/DOC-983720) which gives algorithms to implement a `pow` function. – agold Feb 01 '16 at 13:47
  • @duffymo Only name for function that you can see is pow function? What is wrong with that name? Function works correct for values 0,1, butt I have problemes with higher values. – Jure Feb 01 '16 at 13:48
  • No, your implementation is laughably wrong. Please tell me how you should calculate it for an easy one: exponent of 0.5. (aka square root). You need Newton iterative method. – duffymo Feb 01 '16 at 13:49
  • @duffymo I know what you mean know. I first tryed some library from link, that agold gave. But it wasn't any good, so tried this method and I didn't even notice that it uses integer values. – Jure Feb 01 '16 at 13:52
  • No, it uses doubles. Your algorithm is all wrong from beginning to end. Read up on Newton and iterative method. – duffymo Feb 01 '16 at 13:57
  • 1
    see related **Q/A**'s: [How Math.Pow (and so on) actualy works](http://stackoverflow.com/a/19072451/2521214) , [Power by squaring for negative exponents](http://stackoverflow.com/a/30962495/2521214) and [fixed point exp2,log2,pow](http://stackoverflow.com/a/18169727/2521214). Especially the first two will lead to solution ... the last is just implementation from one of my bignum class and without reading the first two links will not help – Spektre Feb 02 '16 at 08:10
  • @Spektre, thank you for you help. I managed to find answer in similar question. – Jure Feb 02 '16 at 10:10

1 Answers1

1

I helped my self with bharath answer in this question: How to get the power of a number in J2ME

Since exp method is just pow, where we use Euler's number for the first argument, I used bharath method:

public double powSqrt(double x, double y)
    {
        int den = 1024, num = (int)(y*den), iterations = 10;
        double n = Double.MAX_VALUE;

        while( n >= Double.MAX_VALUE && iterations > 1)
        {
            n = x;

            for( int i=1; i < num; i++ )n*=x;

            if( n >= Double.MAX_VALUE ) 
            {
                iterations--;
                den = (int)(den / 2);
                num = (int)(y*den);
            }
        }   

        for( int i = 0; i <iterations; i++ )n = Math.sqrt(n);

        return n;
    }

Method call:

calculation = powSqrt(2.7182818284590452,calculation) * 1.33;

Result is almost as good as Math.pow() method.

PS: I don't know if this is duplicated thread, if so you can delete it.

Community
  • 1
  • 1
Jure
  • 799
  • 6
  • 25