如何在J2ME中获得数字的强大功能

时间:2011-06-29 05:23:12

标签: java java-me pow

  

可能重复:
  J2ME power(double, double) math function implementation

我正在开发一个简单的j2me应用程序。在那里,我需要像java中的pow(double num1, double num2)那样获得数字的强大功能。 但据我所知,j2me不支持这种pow()方法。 任何有用的选择都表示赞赏。

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

使用Java开发移动设备的应用程序时,可能需要特定Java VM上没有的数学方法。您可以使用此代码。它会帮助你。

public double pow(double x, double y)
    {
        return powTaylor(x,y);
    }

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

    public double powDecay(double x, double y)
    {
        int num, den = 1001, s = 0;
        double n = x, z = Double.MAX_VALUE;

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

        while( z >= Double.MAX_VALUE )
        {
            den -=1;
            num = (int)(y*den);
            s = (num/den)+1;

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

        while( n > 0 )
        {
            double a = n;

            for( int i = 1; i < den; i++ )a *= n;

            if( (a-z) < .00001 || (z-a) > .00001 ) return n;

            n *= .9999;                          
        }

        return -1.0;
    }

    double powTaylor(double a, double b)
    {
        boolean gt1 = (Math.sqrt((a-1)*(a-1)) <= 1)? false:true; 
        int oc = -1,iter = 30;
        double p = a, x, x2, sumX, sumY;

        if( (b-Math.floor(b)) == 0 )
        {
            for( int i = 1; i < b; i++ )p *= a;
            return p;
        }

        x = (gt1)?(a /(a-1)):(a-1);
        sumX = (gt1)?(1/x):x;

        for( int i = 2; i < iter; i++ )
        {
            p = x;
            for( int j = 1; j < i; j++)p *= x;

            double xTemp = (gt1)?(1/(i*p)):(p/i);

            sumX = (gt1)?(sumX+xTemp):(sumX+(xTemp*oc));

            oc *= -1;
        }

        x2 = b * sumX;
        sumY = 1+x2;

        for( int i = 2; i <= iter; i++ )
        {
            p = x2;
            for( int j = 1; j < i; j++)p *= x2;

            int yTemp = 2;
            for( int j = i; j > 2; j-- )yTemp *= j;

            sumY += p/yTemp;
        }

        return sumY;
    }

答案 2 :(得分:0)

public static double pow(final double a, final double b) 
{

final int x = (int) (Double.doubleToLongBits(a) >> 32);

final int y = (int) (b * (x - 1072632447) + 1072632447);

return Double.longBitsToDouble(((long) y) << 32);
}

你也可以找到相关的信息

Creating a Java ME Math.pow() Method