6

我正在编写代码来实现这个算法:

公式

但是,即使使用 MathContext(1000),我也会收到此错误:

Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
at java.math.BigDecimal.divide(BigDecimal.java:1603)
at picalculator.PiCalculator.calculatePi(PiCalculator.java:59)
at picalculator.PiCalculator.main(PiCalculator.java:25)
Java Result: 1

使用此方法时:

public static void calculatePi() {
    BigInteger firstFactorial;
    BigInteger secondFactorial;
    BigInteger firstMultiplication;
    BigInteger firstExponent;
    BigInteger secondExponent;
    int firstNumber = 1103;
    BigInteger firstAddition;
    BigDecimal currentPi = BigDecimal.ONE;
    BigDecimal pi = BigDecimal.ONE;
    BigDecimal one = BigDecimal.ONE;
    int secondNumber = 2;
    double thirdNumber = Math.sqrt(2.0);
    int fourthNumber = 9801;
    BigDecimal prefix = BigDecimal.ONE;

    for(int i=1;i<4;i++){
        firstFactorial = factorial(4*i);
        secondFactorial = factorial(i);
        firstMultiplication = BigInteger.valueOf(26390*i);
        firstExponent = exponent(secondFactorial, 4);
        secondExponent = exponent(BigInteger.valueOf(396),4*i);
        firstAddition = BigInteger.valueOf(firstNumber).add(firstMultiplication);
        currentPi = currentPi.add(new BigDecimal(firstFactorial.multiply(firstAddition)).divide(new BigDecimal(firstExponent.multiply(secondExponent)), new MathContext(10000)));
    }

    prefix =new BigDecimal(secondNumber*thirdNumber);
    prefix = prefix.divide(new BigDecimal(fourthNumber), new MathContext(1000));

    currentPi = currentPi.multiply(prefix, new MathContext(1000));

    pi = one.divide(currentPi);

    System.out.println("Pi is: " + pi);

    return;
}

我已经证明了阶乘(a);和 exponent(a,b) 分别准确地返回 a 的阶乘和 a^b 的结果。

有谁知道如何解决这一问题?

4

2 回答 2

5

你需要

pi = one.divide(currentPi, new MathContext(1000));

因为结果几乎可以肯定是重复小数。

考虑

BigDecimal a = new BigDecimal("4");
BigDecimal b = new BigDecimal("3");

BigDecimal c = a.divide(b)                         // java.lang.ArithmeticException: Non-terminating decimal expansion
BigDecimal c = a.divide(b, new MathContext(10));   // No exception
于 2012-03-20T04:29:32.073 回答
3

您可能更喜欢使用不同版本的divide。它使您可以更好地控制返回的 BigDecimal 的最终比例。而对于您的版本,最终比例取决于股息和除数的比例。

int scale = 3;
BigDecimal result = ONE.divide(new BigDecimal("3"), scale, RoundingMode.HALF_UP);
// result is 0.333
于 2012-03-20T09:05:57.000 回答