I have a program which calculates the value of e, however it will not let me increase the accuracy of the calculation. Currently it is only accurate to about 5ish places and caps out with an error. I thought BigDecimal would allow me to do this type of operation. What am I doing wrong?
import java.math.*;
public class Main {
public static void main(String[] args) {
BigDecimal num = new BigDecimal(25000); //bigger the number, the harder and more accurately e is calculated
BigDecimal finl = (BigDecimal.ONE).add(((BigDecimal.ONE).divide(num))).pow(num.intValue());
System.out.println(finl.toString());
}
}
//My testing of diffrent values for "num":
//10,000 == ~1 second
//25,000 == ~8 seconds
//50,000 == ~31 seconds
//75,000 == BUG: "Non-terminating decimal expansion; no exact representable decimal result." :(
Edit Here is the exact error when I use a large number like 75000:
Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
at java.math.BigDecimal.divide(BigDecimal.java:1514)
at Benchmark.Main.main(Main.java:8)
Java Result: 1
Edited by: kavon89 on Apr 16, 2008 12:01 AM
Edited by: kavon89 on Apr 16, 2008 12:03 AM