Java Number Power pow(int base, int exponent)

Here you can find the source of pow(int base, int exponent)

Description

Returns the value of the first argument raised to the power of the second argument.

License

Open Source License

Declaration

public static int pow(int base, int exponent) 

Method Source Code

//package com.java2s;
/*/*from w  w w  .j  a v  a 2 s  .  c  o  m*/
 * MathUtil.java
 *
 * Copyright (C) 2005-2007 Tommi Laukkanen
 * http://www.substanceofcode.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

public class Main {
    /**
     * Returns the value of the first argument raised to the
     * power of the second argument.
     *
     * @author Mario Sansone
     */
    public static int pow(int base, int exponent) {
        boolean reciprocal = false;
        if (exponent < 0) {
            reciprocal = true;
        }
        int result = 1;
        while (exponent-- > 0) {
            result *= base;
        }
        return reciprocal ? 1 / result : result;
    }

    public static double pow(double base, int exponent) {
        boolean reciprocal = false;
        if (exponent < 0) {
            reciprocal = true;
        }
        double result = 1;
        while (exponent-- > 0) {
            result *= base;
        }
        return reciprocal ? 1 / result : result;
    }

    /**
     * Replacement for missing Math.pow(double, double)
     * @param x
     * @param y
     * @return
     */
    public static double pow(double x, double y) {
        //Convert the real power to a fractional form
        int den = 1024; //declare the denominator to be 1024

        /*Conveniently 2^10=1024, so taking the square root 10
        times will yield our estimate for n.  In our example
        n^3=8^2    n^1024 = 8^683.*/

        int num = (int) (y * den); // declare numerator

        int iterations = 10; /*declare the number of square root
                             iterations associated with our denominator, 1024.*/

        double n = Double.MAX_VALUE; /* we initialize our         
                                     estimate, setting it to max*/

        while (n >= Double.MAX_VALUE && iterations > 1) {
            /*  We try to set our estimate equal to the right
            hand side of the equation (e.g., 8^2048).  If this
            number is too large, we will have to rescale. */

            n = x;

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

            /*here, we handle the condition where our starting
            point is too large*/
            if (n >= Double.MAX_VALUE) {
                iterations--; /*reduce the iterations by one*/

                den = (int) (den / 2); /*redefine the denominator*/

                num = (int) (y * den); //redefine the numerator
            }
        }

        /*************************************************
         ** We now have an appropriately sized right-hand-side.
         ** Starting with this estimate for n, we proceed.
         **************************************************/
        for (int i = 0; i < iterations; i++) {
            n = Math.sqrt(n);
        }

        // Return our estimate
        return n;
    }
}

Related

  1. pow(int a, int b)
  2. pow(int a, int b, int modulus)
  3. pow(int b, int e)
  4. pow(int base, int exp)
  5. pow(int base, int exp)
  6. pow(int base, int power)
  7. pow(int i, int pow)
  8. pow(int leftOp, int rightOp)
  9. pow(int n, int p)