Write a program called
ComputePI
to compute the value of π, using the following series expansion. You have to decide on the termination criterion used in the computation (such as the number of terms used or the magnitude of an additional term). Is this series suitable for computing π?
JDK maintains the value of π in a
double
constant called Math.PI
. Compare the values obtained and the Math.PI
, in percents of Math.PI
.
Hint: Add to sum if the denominator modulus 4 is 1, and subtract from sum if it is 3.
double sum = 0;
int maxDenom = 10000000;
for (int denom = 1; ..... ; denom = denom + 2) {
if (denom % 4 == 1) {
sum += ......;
} else if (denom % 4 == 3) {
sum -= ......;
} else {
System.out.println("The computer has gone crazy?!");
}
}
Comment your answers :)
0 comments:
Post a Comment