Java BigDecimal Power pow(BigDecimal b, int p, int q)

Here you can find the source of pow(BigDecimal b, int p, int q)

Description

Returns the result of bp/q as a BigDecimal .

License

Apache License

Parameter

Parameter Description
b The base
p The numerator of the exponent
q The denominator of the exponent

Exception

Parameter Description
IllegalArgumentException if the exponet is negative orthe base is negative.

Return

the result of bp/q

Declaration

public static BigDecimal pow(BigDecimal b, int p, int q) throws IllegalArgumentException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2014 Felipe Takiyama/*ww w . j  a  va  2s  .  c  o m*/
 * 
 * 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.
 ******************************************************************************/

import java.math.BigDecimal;

import java.math.MathContext;

public class Main {
    public static final MathContext CONTEXT = MathContext.DECIMAL64;
    public static int calls = 0;

    /**
     * Returns the result of <code>b<sup>p/q</sup></code> as a 
     * {@link BigDecimal}.
     * <p>
     * This method is not defined for negative numbers, so <code>b</code>
     * and <code>p/q</code> must be positive numbers. It throws a 
     * {@link IllegalArgumentException} when the exponent is negative or
     * the base is negative.
     * </p>
     * <p>
     * Exceptional cases:
     * <li> 0<sup>n</sup> = 0 for n > 0
     * <li> 0<sup>0</sup> = 1
     * </p>
     * <br>
     * 
     * @param b The base
     * @param p The numerator of the exponent
     * @param q The denominator of the exponent
     * @return the result of <code>b<sup>p/q</sup></code>
     * @throws IllegalArgumentException if the exponet is negative or
     * the base is negative.
     */
    public static BigDecimal pow(BigDecimal b, int p, int q) throws IllegalArgumentException {

        BigDecimal result;
        int sign = p * q;
        if (b.signum() == 0) {
            if (sign > 0) {
                result = BigDecimal.ZERO;
            } else if (sign == 0) {
                result = BigDecimal.ONE;
            } else {
                throw new IllegalArgumentException("0^n, n < 0 is undefined!");
            }
        } else if (b.signum() < 0 || sign < 0) {
            if (p % q == 0) {
                int exp = p / q;
                result = b.pow(exp, CONTEXT);
            } else {
                throw new IllegalArgumentException("Operation not defined for" + " negative numbers.");
            }
        } else {
            // separates p/q in two parts: whole (i) and decimal (d)
            int intPart = p / q;
            double decPart = ((double) p) / q - intPart;

            // calculates b^i using BigDecimal.pow()
            BigDecimal intPow = b.pow(intPart, CONTEXT);

            // calculates b^d using Math.pow()
            double bAsDouble = b.doubleValue();
            BigDecimal decPow = new BigDecimal(Math.pow(bAsDouble, decPart), CONTEXT);

            result = intPow.multiply(decPow, CONTEXT);
        }
        calls++;
        return result;
    }
}

Related

  1. decimalPow(BigDecimal number, BigDecimal power)
  2. pow(BigDecimal arg0, BigDecimal arg1)
  3. pow(BigDecimal base, BigDecimal exponent)
  4. pow(BigDecimal base, BigDecimal power)
  5. pow(BigDecimal one, int another)
  6. pow(BigDecimal savedValue, BigDecimal value)