0

k=1 から始まり k=100 で終わるこのアルゴリズムを計算するプログラムを作成しました。

方式

作成したコードは次のとおりです。

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

    for(int i=1;i<101;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);
        summationPi = firstFactorial.intValue()*firstAddition.intValue();
        summationPi /= firstExponent.intValue()*secondExponent.intValue();
        currentPi += summationPi;
    }

    prefix = secondNumber*thirdNumber;
    prefix = prefix/fourthNumber;

    summationPi = summationPi*prefix;

    pi = 1/summationPi;

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

    return;
}

関数 exponent(a,b); a^b の結果を返します。関数 factorial(a) は、a の階乗を返します。これらの機能が両方とも完全に機能することを証明しました。しかし、このコードは不思議なことに "NaN" を返しているようです。何かがゼロで割られるときにこれが起こることは理解していますが、何かがゼロで割られるポイントを見つけることができませんでした。これを引き起こす他の何かがありますか/私は間違っていますか?

注: for ステートメントでは、アルゴリズムで i を k として使用しています。

前もって感謝します!

4

2 に答える 2

2

問題:

これらの行は、エラーが発生している可能性があります。

summationPi = firstFactorial.intValue()*firstAddition.intValue();
summationPi /= firstExponent.intValue()*secondExponent.intValue();

その理由は、完全な値を返すことが保証されていない を呼び出しintValue()ているためです (は 32 ビットのデータしか保持できないためです。これは、結果をではなくとして保存することにも関係する可能性があります)。BigIntegerintdoubleBigDecimal

次に、その可能なNaN値を取り、除算の除数として使用します。

解決:

BigDecimal currentPi = BigDecimal.ONE;

currentPi = currentPi.add(
  new BigDecimal(firstFactorial.multiply(firstAddition))
    .divide(new BigDecimal(firstExponent.multiply(secondExponent)), new MathContext(10000)));

summationPi複数の行を 1 つに結合することで削除できることに注意してください。また、メソッドMathContextで出てくるは に設定されており、これは任意の精度に変更できます。divide()10000

の詳細についてはBigDecimalAPI を確認してください

于 2012-03-15T19:26:01.230 に答える
0

この問題の原因は次の行にあります。

summationPi /= firstExponent.intValue()*secondExponent.intValue();

ここで、secondExponent の値は、i が増加するにつれて大きくなり、intValue() メソッドを使用してその int 値を取得すると、0 になります。

于 2012-03-15T22:12:54.560 に答える