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... is this right? need some1 to clarify...

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default help... is this right? need some1 to clarify...

    double denom = 3;
    double pi;
    double x = -1;
    int i;
    double pi2 =0;
    double denom2 = 0;
    double pi3 = 0;
     
    pi = 4*(1-((double)1/denom));
    pi2 = pi;
     
    for (i=2; i<=300000 ; i++){
     
        pi3 = pi2;
        x = x*-1;
        denom = (denom+2);
        denom2 = x*((double)(1/denom));
        pi2 = pi3 + denom2;
     
     
     
     
           if (i%1000 == 0)
           System.out.println(i + "   " + pi2);
     
     
     
    }
     
     
     
        }
    }


    this code supposed to calculate this algorithm

    4*(1-(1/3)+(1/5)-(1/7)+(1/9).....)

    which is equal to pi.. but i dunno if this is right or what... it works but can somebody clarify?
    Last edited by copeg; November 7th, 2010 at 10:32 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: help... is this right? need some1 to clarify...

    it works but can somebody clarify?
    Please describe what clarification is needed. If it works and you don't understand...when all else fails, grab a pen and pencil and walk through the algorithm bit by bit (maybe not 30,000 times, but perhaps lower that loop value and println the output every loop, and check against the pen and pencil).

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: help... is this right? need some1 to clarify...

    it works my calculation works fine but when i run the program it doesnt come close to the value of pi which is 3.14 sumthing only up to 2.7 sumthing..

    am i doing the calculation wrong?

    i did... 4*(1-(1/3)) 1st then the result will be either added or subracted by an increasing denom by 2.....

    is that the proper way?