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

Members have full access to the forums. Advertisements are removed for registered users.

# Thread: Help with understanding the following commented codes!

1. ## Help with understanding the following commented codes!

Hello, the code below is an extract from a small program which lets the user choose the number of iterations (for an approximation of PI/4). The program works, however i do not understand how it is working. Please help me understand the following lines...

``` for (int i=0; i<iteration; i++) { //i understand this
double sum = 1.0/(2.0*(i) + 1.0); //just the formula
if (positive) //do not understand this!!

output += sum; //same as output = output + sum
else
output -= sum; // same as output = output - sum
positive = !positive; // not sure how this is helping with the program!```

Thanks!  Reply With Quote

3. ## Re: Help with understanding the following commented codes!

Hello.

'Output' is the result. Here we have summation of elements that approximate PI/4.These series approximate Pi/4 with sum of elements with alternating signs.

If 'positive' is true then we add an element to 'output',
if 'positive' is false then we subtract the an element from 'output'.

Because the signs are alternating we must change the 'positive' value every time.
That is what we do with

`positive = !positive;`

If we choose iterations to be a big number, then we get a good approximation with this code. I added some definitions and print the output ,

```package mypack;

public class Main {

public static void main(String[] args) {
int iteration=1000;
double output=0;
boolean positive=true;

for (int i=0; i<iteration; i++) {
double sum = 1.0/(2.0*(i) + 1.0);

if (positive) //if positive we add otherwise we subtract
output += sum;
else
output -= sum;

positive = !positive; // changing the sign of the next summation
}

System.out.println("Approximated Pi/4="+output);

}

}```

Here the number of iterations was set for 1000, and if we run the code , we get
the result printed

`Approximated Pi/4=0.7851481634599485`

which is pretty close to real PI/4=0.785398163

Hope it helped,
Valentin  Reply With Quote

4. ## The Following User Says Thank You to Valen007 For This Useful Post:

u-will-neva-no (April 2nd, 2011)

5. ## Re: Help with understanding the following commented codes!

Thankyou very much Valetin, now i understand!  Reply With Quote