Java Number Power pow(double a, int b)

Here you can find the source of pow(double a, int b)

Description

Computes a^b for integer exponents.

License

Apache License

Parameter

Parameter Description
a base for exponentiation
b integral value of exponent

Return

a raised to the b th power

Declaration

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

Method Source Code

//package com.java2s;
/*// w ww  .  jav a2  s. c o m
 * Copyright 2014 Jon N. Marsh.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Computes {@code a^b} for integer exponents. Works for both positive and
     * negative values of the exponent {@code b}.
     *
     * @param   a   base for exponentiation
     * @param   b   integral value of exponent
     * @return   {@code a} raised to the {@code b}<sup>th</sup> power
     */
    public static final double pow(double a, int b) {
        if (b < 0.0) {
            a = 1.0 / a;
            b *= -1;
        }
        double result = 1.0;
        while (b != 0) {
            if ((b & 1) == 1) {
                result *= a;
            }
            b >>= 1;
            a *= a;
        }

        return result;
    }
}

Related

  1. pow(double a, double b)
  2. pow(double a, double b)
  3. pow(double a, double b)
  4. pow(double a, double b)
  5. pow(double base, double exp)
  6. pow(double base, int exp)
  7. pow(double base, int exp)
  8. pow(double base, int exponent)