2
我使用的楚德諾夫斯基算法來計算PI:爲什麼BigDecimal的這個分區返回0?
下面是代碼:
import java.math.BigDecimal;
import java.math.MathContext;
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int summationUpperLimit;
int precision;
System.out.println("Enter the summation upper limit: ");
summationUpperLimit = reader.nextInt();
System.out.println("Enter the precision: ");
precision = reader.nextInt();
System.out.println(calculatePI(summationUpperLimit, precision));
}
private static int calculateFactorial(int n) {
int factorial = 1;
for (; n > 1; n--) {
factorial = factorial * n;
}
return factorial;
}
private static BigDecimal calculatePI(int summationUpperLimit, int precision) {
BigDecimal reciprocalOfPI = BigDecimal.ZERO;
reciprocalOfPI.setScale(precision - 1, BigDecimal.ROUND_HALF_UP);
for (int k = 0; k <= summationUpperLimit; k++) {
BigDecimal numerator = BigDecimal.valueOf(12 * Math.pow(-1, k) * calculateFactorial(6 * k) * (13591409 + 545140134 * k));
numerator.setScale(precision - 1, BigDecimal.ROUND_HALF_UP);
BigDecimal denominator = BigDecimal.valueOf(calculateFactorial(3 * k) + Math.pow(calculateFactorial(k), 3) * Math.pow(640320, 3 * k + 1.5));
denominator.setScale(precision - 1, BigDecimal.ROUND_HALF_UP);
// The issue is the line below:
reciprocalOfPI = reciprocalOfPI.add(numerator.divide(denominator, BigDecimal.ROUND_HALF_UP));
}
return reciprocalOfPI.pow(-1, MathContext.DECIMAL128);
}
}
我設置以下輸入:
summationUpperLimit = 0
precision = 100
在調試模式下,我檢查了輸出:
numerator = 163096908
denominator = 512384048.99600077
reciprocalOfPI = 0 (this value was taken after the division)
163096908/512384048.99600077不等於0爲什麼表達reciprocalOfPI = reciprocalOfPI.add(numerator.divide(denominator, BigDecimal.ROUND_HALF_UP));
將reciprocalOfPI
設置爲0?
我的設置scale = precision - 1
理由:
精度是數字的總數。比例是小數點後的位數。
在PI中,小數點前只有1位數字。
precision = scale + 1
因此scale = precision - 1
對'reciprocalOfPI','numerator'和'denominator'進行修改似乎需要解決它。 – user2948708
哦,是的。我錯過了其他變量的相同錯誤。 –