I have been trying for a few days to compute pi using the series expansion pi = 4(1-1/3+1/5-1/7...
but whenever I use the code I have below, it returns 4.0.
public class Pi {
public static void main (String args[]) {
double pie = 0.0;
int max = 1000;
for (int x = 1 ; x < max ; x = x + 2) {
if (x % 4 == 1) {
pie+= 1/x;
} else if (x % 4 == 3) {
pie-=1/x;
}
}
System.out.println(4*pie);
}
}
In this, I am computing pie
to a denominator below 1000. Pie
is the variable that stores my value created for pie
. At the end, it prints pi, but always returns 4.0.
Using the Debug feature in my IDE (Eclipse
), I see the value of pie
jumps to 4 from the initial value of 0, but then does not change for the rest of the times the program increments the value of x
in the for loop, it does not do anything to pi.