Java Utililty Methods BigDecimal Power

List of utility methods to do BigDecimal Power

Description

The list of methods to do BigDecimal Power are organized into topic(s).

Method

BigDecimalpow(BigDecimal arg0, BigDecimal arg1)
pow
try {
    BigInteger bi = arg1.toBigIntegerExact();
    return arg0.pow(bi.intValue(), MC_100);
} catch (ArithmeticException ex) {
    int n = arg1.intValue();
    switch (arg1.signum()) {
    case 0: 
        if (arg0.compareTo(BigDecimal.ZERO) != 0) {
...
BigDecimalpow(BigDecimal b, int p, int q)
Returns the result of bp/q as a BigDecimal .
BigDecimal result;
int sign = p * q;
if (b.signum() == 0) {
    if (sign > 0) {
        result = BigDecimal.ZERO;
    } else if (sign == 0) {
        result = BigDecimal.ONE;
    } else {
...
BigDecimalpow(BigDecimal base, BigDecimal exponent)
pow
BigDecimal result = BigDecimal.ZERO;
int signOf2 = exponent.signum();
double dn1 = base.doubleValue();
BigDecimal n2 = exponent.multiply(new BigDecimal(signOf2)); 
BigDecimal remainderOf2 = n2.remainder(BigDecimal.ONE);
BigDecimal n2IntPart = n2.subtract(remainderOf2);
BigDecimal intPow = base.pow(n2IntPart.intValueExact());
BigDecimal doublePow = new BigDecimal(Math.pow(dn1, remainderOf2.doubleValue()));
...
BigDecimalpow(BigDecimal base, BigDecimal power)
Raises the base to the power and returns the result.
int precisionBase = base.precision();
int precisionPower = power.precision();
int precision = precisionBase < precisionPower ? precisionBase : precisionPower;
MathContext context = new MathContext(precision, RoundingMode.HALF_DOWN);
return pow(base, power, context);
BigDecimalpow(BigDecimal one, int another)
pow
return one.pow(another, _SCALE);
BigDecimalpow(BigDecimal savedValue, BigDecimal value)
pow
BigDecimal result = null;
result = exp(ln(savedValue, 32).multiply(value), 32);
return result;
BigDecimalpower(BigDecimal base, int exponent)
Returns the exponentiation of the given base raised to power of the given exponent.
if (base == null)
    return null;
return base.pow(exponent);
BigDecimalpower(final BigDecimal value, final long power)

This method returns the value multiplied by itself a number of times equal to the power.

Internally, it uses bit-shifting and recursion to only perform 2 * ceil(lg(value))or less multiplications.

if (power == 0) {
    return BigDecimal.ONE;
BigDecimal lowerValue = power(value, power >>> 1);
lowerValue = lowerValue.multiply(lowerValue);
if ((power & 0x1l) == 0x1l) {
    return lowerValue.multiply(value);
} else {
...
BigDecimaldecimalPow(BigDecimal number, BigDecimal power)
Pow for two decimals
return new BigDecimal(Math.pow(number.doubleValue(), power.doubleValue()));