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.

Results 1 to 3 of 3

Thread: Help with understanding the following commented codes!

  1. #1
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Post 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!


  2. #2
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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
    Last edited by Valen007; April 2nd, 2011 at 03:41 PM.

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

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

  4. #3
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Default Re: Help with understanding the following commented codes!

    Thankyou very much Valetin, now i understand!

Similar Threads

  1. Please Help What's Wrong with my Codes!
    By bertzki10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 25th, 2011, 11:03 AM
  2. Having trouble understanding what teacher said for build Tree.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 16th, 2010, 08:22 PM
  3. Need help understanding method calls and such
    By hackman2007 in forum Java Theory & Questions
    Replies: 8
    Last Post: July 14th, 2010, 08:18 AM
  4. Need help understanding GUI screen layouts and labels
    By Bill_H in forum AWT / Java Swing
    Replies: 3
    Last Post: December 2nd, 2009, 11:50 PM
  5. Help understanding this code
    By Zepx in forum Java Theory & Questions
    Replies: 2
    Last Post: October 20th, 2009, 10:18 AM