Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Thread: could you tell me whats wrong....

1. 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!

2. 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

3. 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

4. Re: could you tell me whats wrong....

yeah nevermind i got it already thank you anyways....