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

static public double pow(double x, double y) 

Method Source Code

//package com.java2s;
/**//from w w w. j av a2s.c om
 * <p>Title: Class for float-point calculations in J2ME applications CLDC 1.1</p>
 * <p>Description: Useful methods for float-point calculations which absent in native Math class</p>
 * <p>Copyright: Copyright (c) 2004 Nick Henson</p>
 * <p>Company: UNTEH</p>
 * <p>License: Free use only for non-commercial purpose</p>
 * <p>If you want to use all or part of this class for commercial applications then take into account these conditions:</p>
 * <p>1. I need a one copy of your product which includes my class with license key and so on</p>
 * <p>2. Please append my copyright information henson.midp.Float (C) by Nikolay Klimchuk on ?About? screen of your product</p>
 * <p>3. If you have web site please append link <a href=?http://henson.newmail.ru?>Nikolay Klimchuk</a> on the page with description of your product</p>
 * <p>That's all, thank you!</p>
 * @author Nikolay Klimchuk http://henson.newmail.ru
 * @version 0.5
 */

public class Main {
    /** ln(0.5) constant */
    final static public double LOGdiv2 = -0.6931471805599453094;

    static public double pow(double x, double y) {
        if (x == 0.)
            return 0.;
        if (x == 1.)
            return 1.;
        if (y == 0.)
            return 1.;
        if (y == 1.)
            return x;
        //
        long l = (long) Math.floor(y);
        boolean integerValue = (y == (double) l);
        //
        if (integerValue) {
            boolean neg = false;
            if (y < 0.)
                neg = true;
            //
            double result = x;
            for (long i = 1; i < (neg ? -l : l); i++)
                result = result * x;
            //
            if (neg)
                return 1. / result;
            else
                return result;
        } else {
            if (x > 0.)
                return exp(y * log(x));
            else
                return Double.NaN;
        }
    }

    static public int pow(int x, int y) {
        if (x == 0)
            return 0;
        if (x == 1)
            return 1;
        if (y == 0)
            return 1;
        if (y == 1)
            return x;

        int pow = x;
        for (int i = 2; i <= y; i++)
            pow *= x;

        return pow;
    }

    static public double exp(double x) {
        if (x == 0.)
            return 1.;
        //
        double f = 1;
        long d = 1;
        double k;
        boolean isless = (x < 0.);
        if (isless)
            x = -x;
        k = x / d;
        //
        for (long i = 2; i < 50; i++) {
            f = f + k;
            k = k * x / i;
        }
        //
        if (isless)
            return 1 / f;
        else
            return f;
    }

    static public double log(double x) {
        if (!(x > 0.))
            return Double.NaN;
        //
        if (x == 1.0)
            return 0.0;
        // Argument of _log must be (0; 1]
        if (x > 1.) {
            x = 1 / x;
            return -_log(x);
        }
        ;
        //
        return _log(x);
    }

    static private double _log(double x) {
        if (!(x > 0.))
            return Double.NaN;
        //
        double f = 0.0;
        //
        int appendix = 0;
        while (x > 0.0 && x <= 1.0) {
            x *= 2.0;
            appendix++;
        }
        //
        x /= 2.0;
        appendix--;
        //
        double y1 = x - 1.;
        double y2 = x + 1.;
        double y = y1 / y2;
        //
        double k = y;
        y2 = k * y;
        //
        for (long i = 1; i < 50; i += 2) {
            f += k / i;
            k *= y2;
        }
        //
        f *= 2.0;
        for (int i = 0; i < appendix; i++)
            f += LOGdiv2;
        //
        return f;
    }
}

Related

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