3

Given in input two BigDecimal, one for the base and one for the exponent:

BigDecimal base;
BigDecimal exponent;

Considering that they can have both positive and negative signums, how can I calculate the power base^exponent ?

I found the following method in the BigDecimal class, but it accepts only int as the exponent.

public BigDecimal pow(int n);

Then after an extra search I found https://github.com/aaiyer/SuanShu/blob/master/src/main/java/com/numericalmethod/suanshu/number/big/BigDecimalUtils.java#LC147

exploiting the equality z = x^y --> z = exp ( ln(x) * y )

but how can I calculate it in the case of negative base and exponents?

1Z10
  • 2,801
  • 7
  • 33
  • 82
  • 1
    Possible duplicate of [Java's BigDecimal.power(BigDecimal exponent): Is there a Java library that does it?](https://stackoverflow.com/questions/16441769/javas-bigdecimal-powerbigdecimal-exponent-is-there-a-java-library-that-does) – xingbin Aug 15 '18 at 14:40
  • 1
    Use `x^(-y) == 1/(x^y)`. For `-x` you'd better ensure `y` is an integer. – DodgyCodeException Aug 15 '18 at 14:51

0 Answers0