I am trying to use the BigDecimal.pow(int i)
with very big base and exponents, however I'm getting an ArithmeticException: Underflow
error.
To simply put it, the code is:
BigDecimal base = BigDecimal.valueOf(2147483645.4141948);
BigDecimal product = base.pow(987654321);
System.out.println("product = " + product.toPlainString());
Yes, this is a Project Euler problem. However I know my numbers are correct. This is not a mathematical problem, it is purely me not understanding why BigDecimal.pow(int i)
is giving me an ArithmeticException: Underflow
.
I know that BigDecimal
's scale
is a 32-bit int
but is there any way at all to bypass this and calculate such a big value? If it helps, I do plan on flooring the product and modding it by 100000000
since I only want the last 8 digits. If there is any other way I could do this mathematically, I'd like a hint.
Stack trace:
Exception in thread "main" java.lang.ArithmeticException: Underflow
at java.math.BigDecimal.checkScale(BigDecimal.java:3841)
at java.math.BigDecimal.pow(BigDecimal.java:2013)
at test.main(test.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Process finished with exit code 1
Thanks.