Results 1 to 5 of 5

Thread: [Python] Cannot determine Pi

  1. #1
    Join Date
    Feb 2009
    Posts
    2,155
    Mentioned
    4 Post(s)
    Quoted
    42 Post(s)

    Default [Python] Cannot determine Pi

    Here is the formula that we are to use for pi
    2fzX7Qb.png

    Here is my program code
    http://pastebin.com/ZQ9XeA3T


    And finally here is the debug
    Code:
    Enter a tolerance.00001
    Num1:  2.3094010767585034  Num2:  2.3863811126504535
    [1, -0.3333333333333333, 0.022222222222222223]
    Num1:  2.3863811126504535  Num2:  2.36805253267618
    [1, -0.3333333333333333, 0.022222222222222223, -0.005291005291005291]
    Num1:  2.36805253267618  Num2:  2.372804386743584
    [1, -0.3333333333333333, 0.022222222222222223, -0.005291005291005291, 0.0013717421124828531]
    Num1:  2.372804386743584  Num2:  2.3715084265433832
    [1, -0.3333333333333333, 0.022222222222222223, -0.005291005291005291, 0.0013717421124828531, -0.0003741114852225963]
    Num1:  2.3715084265433832  Num2:  2.371873953779337
    [1, -0.3333333333333333, 0.022222222222222223, -0.005291005291005291, 0.0013717421124828531, -0.0003741114852225963, 0.00010551862403714256]
    Num1:  2.371873953779337  Num2:  2.3717683570222836
    [1, -0.3333333333333333, 0.022222222222222223, -0.005291005291005291, 0.0013717421124828531, -0.0003741114852225963, 0.00010551862403714256, -3.0483158055174515e-05]
    Num1:  2.3717683570222836  Num2:  2.3717994148920054
    [1, -0.3333333333333333, 0.022222222222222223, -0.005291005291005291, 0.0013717421124828531, -0.0003741114852225963, 0.00010551862403714256, -3.0483158055174515e-05, 8.965634722110152e-06]
    Num1:  2.3717994148920054  Num2:  2.37179015201858
    [1, -0.3333333333333333, 0.022222222222222223, -0.005291005291005291, 0.0013717421124828531, -0.0003741114852225963, 0.00010551862403714256, -3.0483158055174515e-05, 8.965634722110152e-06, -2.673961232910045e-06]
    Num1:  2.37179015201858  Num2:  2.3717929455835813
    [1, -0.3333333333333333, 0.022222222222222223, -0.005291005291005291, 0.0013717421124828531, -0.0003741114852225963, 0.00010551862403714256, -3.0483158055174515e-05, 8.965634722110152e-06, -2.673961232910045e-06, 8.064327527823946e-07]
    Pi value 2.3717929455835813
    It seems to follow the formula fine, but does not come anywhere near pi.

    Am I wrong or is the formula?
    Last edited by JPHamlett; 07-05-2015 at 11:45 PM.

  2. #2
    Join Date
    May 2012
    Location
    Glorious Nippon
    Posts
    1,011
    Mentioned
    50 Post(s)
    Quoted
    505 Post(s)

    Default

    The formula is fine, I made a quick script in simba to test it.

    Simba Code:
    program new;
    var
      sum: extended;
      n: integer;
    begin
      for n := 0 to 10 do
      begin
        sum := sum + (pow(-1,n))/(pow(3,n)*(2*n+1));
        writeln(sum);
      end;
      sum := sum*6/sqrt(3);
      writeln(sum);
    end.
    output:
    Code:
    1
    0.888888888888889
    0.911111111111111
    0.905820105820106
    0.907191847932589
    0.906817736447366
    0.906923255071403
    0.906892771913348
    0.90690173754807
    0.906899063586837
    0.90689987001959
    3.14159330450308
    I don't know any python, but your code looks overly complicated. It's just a simple series that should only take a couple lines.

    EDIT: because I had nothing better to do..
    Simba Code:
    program pie;
    var
      c: extended := 6/sqrt(3);
      sum, term, error: extended;
      n: integer;
    begin
      clearDebug();
      error := 1;
      writeln(format('%-12s %-12s %-12s', ['pi est.', 'error est.', 'error']));
      while (error > 0.00001) do
      begin
        term := c*((pow(-1,n))/(pow(3,n)*(2*n+1)));
        inc(n);
        error := abs(c*((pow(-1,n))/(pow(3,n)*(2*n+1))));
        sum := sum + term;
        write(format('%9.9f; ', [sum]));
        write(format('%9.9f; ', [abs(error)]));
        writeln(format('%9.9f', [abs(pi - sum)]));
      end;
    end.
    Code:
    pi est.      error est.   error       
    3.464101615; 0.384900179; 0.322508962
    3.079201436; 0.076980036; 0.062391218
    3.156181472; 0.018328580; 0.014588818
    3.137852892; 0.004751854; 0.003739762
    3.142604746; 0.001295960; 0.001012092
    3.141308785; 0.000365527; 0.000283868
    3.141674313; 0.000105597; 0.000081659
    3.141568716; 0.000031058; 0.000023938
    3.141599774; 0.000009263; 0.000007120

  3. #3
    Join Date
    May 2014
    Posts
    633
    Mentioned
    8 Post(s)
    Quoted
    322 Post(s)

    Default

    Well definitely the second entry in values is wrong...it oughta be -1/9 not -1/3 right or am I completely wrong? 1/(3*3^1) = 1/9 not 1/3

  4. #4
    Join Date
    Feb 2009
    Posts
    2,155
    Mentioned
    4 Post(s)
    Quoted
    42 Post(s)

    Default

    My God! Thank you! That fixed the whole issue for me

  5. #5
    Join Date
    May 2014
    Posts
    633
    Mentioned
    8 Post(s)
    Quoted
    322 Post(s)

    Default

    Quote Originally Posted by JPHamlett View Post
    My God! Thank you! That fixed the whole issue for me
    No problem!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •