Search code examples
javabigdecimal

How to calculate power with no integer bigdecimal expoent?


Trying to calculate (a+b)^n where n is a real value in a BigDecimal variable, but BigDecimal.pow is designed for accept only integer values.


Solution

  • If the input is within the magnitude range supported by double, and you do not need more than 15 significant digits in the result, convert (a+b) and n to double, use Math.pow, and convert the result back to BigDecimal.