# could you tell me whats wrong....

• November 6th, 2010, 11:41 AM
gonfreecks
could you tell me whats wrong....
double denom = 3;
double pi;
double x = -1;
int i;

pi = 4*(1-((double)1/denom));

for (i=1 ; i<=30000 ; i++){
x = x*-1;
denom = (double)x*(denom+2);
pi = pi + denom;

System.out.println(i + " " + denom);

this code supposed to print denom at an alternating operation sequence in increasing value by 2.. but it only prints

5, -7 ,5 ,-3 , 5, -7 , 5,-3 over and over. helP!
• November 6th, 2010, 01:39 PM
gonfreecks
Re: could you tell me whats wrong....
double denom = 3;
double pi;
double x = -1;
int i;

for (i=1 ; i<=30000 ; i++){
pi = 4*(1-((double)1/denom));
x = x*-1;

if (i % 1000 == 0){

System.out.println(i + " " + pi);
denom = x*((double)1/(denom+2));
pi = pi + denom;

}
}

}
}

narrowed it down to this but still its not working
• November 6th, 2010, 03:32 PM
Zula
Re: could you tell me whats wrong....
I believe the problem is that switching the sign of denom makes it so when you ADD two each time, you are not always adding to the absolute value of the number, as adding to a negative gets it closer to 0
• November 6th, 2010, 04:35 PM
gonfreecks
Re: could you tell me whats wrong....
yeah nevermind i got it already thank you anyways.... :)