Java Number Power pow(double x, double y)

Here you can find the source of pow(double x, double y)

Description

pow

License

Open Source License

Declaration

public static final double pow(double x, double y) 

Method Source Code

//package com.java2s;

public class Main {
    public static final double pow(double x, double y) {
        return powTaylor(x, y);
    }//from   w w w  .  j  a v a 2 s .  c om

    private static final 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;
    }
}

Related

  1. Pow(double val, Object in)
  2. Pow(double value)
  3. pow(double value)
  4. pow(double x, double y)
  5. pow(double x, double y)
  6. pow(double x, int n)
  7. pow(double[] array, double exp)
  8. pow(double[][] x, int p)
  9. pow(final double[] values, final double exp)