Page 1 of 3 123 LastLast
Results 1 to 25 of 61

Thread: Pi Script?

  1. #1
    Join Date
    Dec 2011
    Location
    U.S.A.
    Posts
    635
    Mentioned
    5 Post(s)
    Quoted
    249 Post(s)

    Default Pi Script?

    I am trying to make a script to calculate pi. I am having toubles, but i am trying to use Leibniz's formula to do it. I have barely started, yet I am already getting errors.. Here's my code so far:
    Simba Code:
    Program MathProblems;
    var
    Answer:Extended;

    procedure PieMaker;
    var
    i:Integer;
    x, d, e:Extended;
    begin
    i := 7;
    x := ((2/3)+(1/5))-(1/i);
    IncEx(i, 2);
    d := (x) + (1/i);
    Answer:= d;
    Writeln(''+IntToStr(Answer)+'');
    end;

    begin
    end.

  2. #2
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    there is a pi solver under competitions i cant link you to it atm though not on computer

  3. #3
    Join Date
    Nov 2012
    Posts
    141
    Mentioned
    0 Post(s)
    Quoted
    43 Post(s)

    Default

    So, you are trying to convert answer, which is of type extended, as an integer to a string.
    So, you need to find a function that will convert type extended to a string. I'm looking around.

    EDIT: FloatToStr() is what you want to use in place of IntToStr().

  4. #4
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by Neodymium View Post
    So, you are trying to convert answer, which is of type extended, as an integer to a string.
    So, you need to find a function that will convert type extended to a string. I'm looking around.
    tostr(varhere)

  5. #5
    Join Date
    Dec 2011
    Location
    U.S.A.
    Posts
    635
    Mentioned
    5 Post(s)
    Quoted
    249 Post(s)

    Default

    Ah. Yes I know there is already a script for it, but I am bored and wanted to make my own.
    Ah Thanks
    Last edited by Sawyer; 03-15-2013 at 02:49 AM.

  6. #6
    Join Date
    Nov 2012
    Posts
    141
    Mentioned
    0 Post(s)
    Quoted
    43 Post(s)

    Default

    Quote Originally Posted by RJJ95 View Post
    tostr(varhere)
    That works too I guess, I didn't know there was a generic string converter.
    I always have to go though the trouble of typing inttostr(). ;|

  7. #7
    Join Date
    Dec 2011
    Location
    U.S.A.
    Posts
    635
    Mentioned
    5 Post(s)
    Quoted
    249 Post(s)

    Default

    Ah. Yes I know there is already a script for it, but I am bored and wanted to make my own.
    How does this look? (Using Leibniz's formula) It still gives me
    "Exception in Script: Operator expected at line 18, column 1' (Line 18 is the Answer := (y + 2/3))
    Simba Code:
    Program MathProblems;
    var
    Answer:Extended;

    procedure PieMaker;
    var
    i:Integer;
    x, d, e, y:Extended;
    begin
      i := 7;
      x := 5;
      repeat
        e := (1/x) - (1/i);
        IncEx(i, 2);
        IncEx(x, 2);
        y := (1/x) + (1/i);
      until((x = 999))
    Answer:= (y + (2/3));
    ToStr(Answer);
    Writeln(''+FloatToStr(Answer)+'');
    end;

    begin
    PieMaker;
    end.
    Last edited by Sawyer; 03-15-2013 at 02:58 AM.

  8. #8
    Join Date
    Nov 2012
    Posts
    141
    Mentioned
    0 Post(s)
    Quoted
    43 Post(s)

    Default

    Quote Originally Posted by Sawyer View Post
    Ah. Yes I know there is already a script for it, but I am bored and wanted to make my own.
    How does this look? (Using Leibniz's formula)
    -snip-
    Now you are trying to increment a float as an integer, I think you need to do something like
    Simba Code:
    x := x + 2;
    instead. I've run it just to test it out, and I get: "0.001001001001", in case you wanted to know.

  9. #9
    Join Date
    Dec 2011
    Location
    U.S.A.
    Posts
    635
    Mentioned
    5 Post(s)
    Quoted
    249 Post(s)

    Default

    GAh... The 2/3 is missing.. darnit.. I'll work on it.
    Leibniz's formula:
    9e804b8a1a11e442be93fed1d52205a9.png

    Note: X and I should not be extended, but integers :\
    Last edited by Sawyer; 03-15-2013 at 03:09 AM.

  10. #10
    Join Date
    Nov 2012
    Posts
    141
    Mentioned
    0 Post(s)
    Quoted
    43 Post(s)

    Default

    Quote Originally Posted by Sawyer View Post
    GAh... The 2/3 is missing.. darnit.. I'll work on it.
    Leibniz's formula:
    9e804b8a1a11e442be93fed1d52205a9.png

    Note: X and I should not be extended, but integers :\
    Oh, so this is a series? I wonder if Simba has anything for that. :P

    So I changed x to an integer as well, and put back the IncEx() call, but now I'm getting "0".
    Something else is amiss.

  11. #11
    Join Date
    Dec 2011
    Location
    U.S.A.
    Posts
    635
    Mentioned
    5 Post(s)
    Quoted
    249 Post(s)

    Default

    Simba Code:
    Program MathProblems;
    var
    Answer:Extended;

    procedure PieMaker;
    var
    i, x:Integer;
    d, e, y:Extended;
    begin
      i := 7;
      x := 5;
      repeat
        e := (1/x) - (1/i);
        IncEx(i, 2);
        y := e + (1/i);
        IncEx(i, 2);
        d := y - (1/i);
        Answer := ((d + 2/3)*4)
      until(i = 999);
      FloatToStr(Answer);
      Writeln(''+FloatToStr(Answer)+'');
    end;

    begin
    PieMaker;
    end.
    EDIT: Got 3.46265459826847 with this,
    Last edited by Sawyer; 03-15-2013 at 03:25 AM.

  12. #12
    Join Date
    Nov 2012
    Posts
    141
    Mentioned
    0 Post(s)
    Quoted
    43 Post(s)

    Default

    Quote Originally Posted by Sawyer View Post
    -snip-
    This I think would do it, but I am not sure how to writeln the answer.
    Are you sure? I know you assign a value to Answer before the repeat loop, but you don't do anything with it afterwards.

    Also, for writing the answer, I just threw in WriteLn(FloatToStr(Answer)) at the end of the main loop.

  13. #13
    Join Date
    Dec 2011
    Location
    U.S.A.
    Posts
    635
    Mentioned
    5 Post(s)
    Quoted
    249 Post(s)

    Default

    Quote Originally Posted by Sawyer View Post
    Simba Code:
    Program MathProblems;
    var
    Answer:Extended;

    procedure PieMaker;
    var
    i, x:Integer;
    d, e, y, k:Extended;
    begin
      i := 7;
      x := 5;
      k := 1 - (1/3);
      e := (k) + (1/x) - (1/i);
        repeat
        IncEx(x, 2);
        IncEx(i, 2);
        y := e + (1/i);
        IncEx(i, 2);
        IncEx(x, 2);
        d := y - (1/i);
        Answer := ((d)*4)
      until(i = 999);
      FloatToStr(Answer);
      Writeln(''+FloatToStr(Answer)+'');
    end;

    begin
    PieMaker;
    end.
    EDIT: Got 3.46265459826847 with this,
    The formula is (1 - 1/3 + 1/5 - 1/7 + 1/9 ... = pi/4) ... it could be possible that this is impossible to do..
    Last edited by Sawyer; 03-15-2013 at 03:39 AM.

  14. #14
    Join Date
    Nov 2012
    Posts
    141
    Mentioned
    0 Post(s)
    Quoted
    43 Post(s)

    Default

    Out of curiosity, I looked for Leibniz's formula on Wolfram. Didn't find it, but I did find another formula:

    So, I just used that and came up with:
    Simba Code:
    Program MathProblems;
    var
    Answer:Extended;

    procedure PieMaker;
    var
      cycles, i: Integer;
    begin
      cycles := 300;
      for i := 0 to cycles do
        Answer := Answer + (pow(-1, i) / ((2 * i) + 1));
      Answer := Answer * 4;
    end;

    begin
    PieMaker;
    Writeln(''+FloatToStr(Answer)+'');
    end.
    Just another way to do this I guess.

    After changing the number of cycles to 10000, I get "3.14169264359054". Getting close. xD

    Edit: Currently trying out 100 million cycles, its been about 3 minutes since the program started and my laptop is kicking into gear. :P

    Edit 2: I'm fairly certain this is the same formula as your's, just in the form of a summation.

    Edit 3: STILL GOING.

    Edit 4: After 100 million cycles and almost 20 minutes, I got "3.14159266358979".
    Last edited by Neodymium; 03-15-2013 at 04:12 AM.

  15. #15
    Join Date
    Dec 2011
    Location
    U.S.A.
    Posts
    635
    Mentioned
    5 Post(s)
    Quoted
    249 Post(s)

    Default

    Nice! I'm going to hit the sack but I'll work on mine tomorrow. Nice job!

  16. #16
    Join Date
    Dec 2011
    Location
    U.S.A.
    Posts
    635
    Mentioned
    5 Post(s)
    Quoted
    249 Post(s)

    Default

    Interesting. It seems after my experiment with changing variables, I broke my simba :/ I redownloaded and copied most of my folders over though.

  17. #17
    Join Date
    Dec 2011
    Location
    U.S.A.
    Posts
    635
    Mentioned
    5 Post(s)
    Quoted
    249 Post(s)

    Default

    Quote Originally Posted by core View Post
    I don't think that should happen... Did you manage to get it fixed?
    I just redownloaded simba, copied various extensions/includes and my scripts over. I'm not sure what happened... After I was playing around with the math, I just kept getting "Exception in Script: Operator expected at line 453, column 93 in file "C:\Simba\Includes\SRL\SRL\core\gametab.simba" for ever single script. The scripts worked with the other simba however.

  18. #18
    Join Date
    Sep 2010
    Location
    Finland
    Posts
    298
    Mentioned
    8 Post(s)
    Quoted
    37 Post(s)

    Default

    Quote Originally Posted by Sawyer View Post
    I just redownloaded simba, copied various extensions/includes and my scripts over. I'm not sure what happened... After I was playing around with the math, I just kept getting "Exception in Script: Operator expected at line 453, column 93 in file "C:\Simba\Includes\SRL\SRL\core\gametab.simba" for ever single script. The scripts worked with the other simba however.
    Make sure that interpreter is NOT set to Lape. Found under Script -> Interpreter.
    Last edited by Smidqe; 03-16-2013 at 07:09 PM.
    Rusting away

  19. #19
    Join Date
    Dec 2011
    Location
    U.S.A.
    Posts
    635
    Mentioned
    5 Post(s)
    Quoted
    249 Post(s)

    Default

    Oh Duh I feel dumb. *facepalm* Thanks

  20. #20
    Join Date
    Dec 2007
    Location
    192.168.1.73
    Posts
    2,439
    Mentioned
    6 Post(s)
    Quoted
    119 Post(s)

    Default

    No one has gone recursive?

    I'll edit this post in a second with a recursive one for lols, shame that for loops can only increment by 1 or -1 in Pascal

  21. #21
    Join Date
    Dec 2007
    Location
    192.168.1.73
    Posts
    2,439
    Mentioned
    6 Post(s)
    Quoted
    119 Post(s)

    Default

    Quote Originally Posted by core View Post
    You can manually increment/decrement the variables using IncEx(variable,amount) and DecEx(variable, amount), or variable := variable +/- amt.

    I tried to do a recursive implementation, but kept hitting out of memory errors on large numbers of cycles. Looking forward to your solution!
    I know, but I enjoy using for loops, although not necessary for doing this recursively.

    However, I seem to have run into a dead end, my code only ever seems to output 4.0, rather than the actual value of pi. I've even got to the point of using a pen and paper, and drawn out trace tables, and my algorithm should work. Makes me think I've done something wrong with types.

    Sorry about the poor code, I haven't actually programmed in quite some time:
    Code:
    program new;
    var
    i: integer;
    pi: extended;
    
    const
    depth = 120;
    
    function pi2: extended;
    begin
      inc(i);
      if (i = depth) then
        result := 1 / ((2 * i) + 1)
      else
        if ((i mod 2) = 0) then
          result := (1 / ((2 * i) + 1)) + pi2
        else
          result := (1 / ((2 * i) + 1)) - pi2;
    end;
    
    begin
      pi := (4 * (1 - pi2));
      writeln(floattostr(pi));
    end.

  22. #22
    Join Date
    Dec 2007
    Location
    192.168.1.73
    Posts
    2,439
    Mentioned
    6 Post(s)
    Quoted
    119 Post(s)

    Default

    Quote Originally Posted by core View Post
    One error is that you're losing precision in your expressions.
    If you change all of your to you should get results of the correct type... but I think there might be some other issue I haven't found yet. I'll update this post if I do.

    Edits:
    That does seem to have fixed it, I have a feeling it would be a type issue. Now it just turns out my maths was off

  23. #23
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    You can all go home now:

    source: http://www.mathpropress.com/stan/bib...phy/spigot.pdf
    edited by me

    Simba Code:
    Program Pi_Spigot;

    const
      n   = 1000;
      len = 10*n div 3;

    var
      i, j, k, q, x, nines, predigit: integer;
      tm: integer;
      a: array[1..len] of longint;
      res: String;


    begin
      tm := getSystemTime;
      for j := 1 to len do
        a[j] := 2;                 {Start with 2s}
      nines := 0;
      predigit := 0;               {First predigit is a 0}
      for j := 1 to n do
      begin
        q := 0;
        for i := len downto 1 do   {Work backwards}
        begin
          x    := 10*a[i] + q*i;
          a[i] := x mod (2*i - 1);
          q    := x div (2*i - 1);
        end;
        a[1] := q mod 10;
        q := q div 10;
        if q = 9 then
          nines := nines + 1
        else
          if q = 10 then
          begin
              res := res + ToStr(predigit+1);
              for k := 1 to nines do
                res := res + ToStr(0);            {zeros}
              predigit := 0;
              nines := 0
                end
                else
                begin
              res := res + ToStr(predigit);
              predigit := q;
              if nines <> 0 then
              begin
                for k := 1 to nines do
                  res := res + ToStr(9);
                nines := 0
              end
          end
      end;
      res := res + ToStr(predigit);
      writeln('took ' + ToStr(getSystemTime- tm) + ' ms');
      writeln(res);
    end.
    Working on: Tithe Farmer

  24. #24
    Join Date
    Dec 2011
    Location
    U.S.A.
    Posts
    635
    Mentioned
    5 Post(s)
    Quoted
    249 Post(s)

    Default

    Eh... Does that use Leibniz's Formula?

  25. #25
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    No, let me write it one sec

    e:

    okay, here it is:

    Simba Code:
    function PiLeib(): Extended;
    var
      I:Integer;
    begin
      for I := 0 to 100000000 do
      begin
        Result := Result + (1.0 / ((I shl 2) + 1));
        Result := Result - (1.0 / ((I shl 2) + 3));
      end;

      Result := Result * 4;
    end;
    Last edited by masterBB; 03-17-2013 at 04:43 PM.
    Working on: Tithe Farmer

Page 1 of 3 123 LastLast

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
  •