Lua Pi Calculator

  • FeynmanLogomaker
    17th Apr 2013 Member 0 Permalink

    --PI/4 == 1 - 1/3 + 1/5 - 1/7 + 1/9... - Just a note I made while developing it, it might still be useful...
    function pi(precision) -- Type in the console "pi(no. of steps)" - It takes 1000000+ to be fairly accurate
    pival = 1
    isNegative = 1
    k = 0
    calc = 3
    while k < precision do -- Rerun the calculation, adding accuracy each time, for precision # of steps.
    pival = pival - (1/calc) * isNegative -- This is the 1 - 1/3 + 1/5 - 1/7...part.
    calc = calc + 2 -- This is the 3, 5, 7 part.
    isNegative = -isNegative -- This is the + - alternation part.
    k = k + 1 -- How many steps it's run
    end
    tpt.log(pival * 4) -- Log the calculated value to console
    end

     

    It will probably give you an "Error - Script not responding": Ignore it. It actually is responding ('precision' # of times).

    This script may not be of much use, but I'm still really proud of it: No flaming please :)

     

    Gave me 3.1415926358123, but if you let it run longer, it will do better.

  • mniip
    17th Apr 2013 Developer 0 Permalink
    @FeynmanLogomaker (View Post)
    will not, no more dogits fit into the double precision floating point value
  • FeynmanLogomaker
    17th Apr 2013 Member 0 Permalink

    Well, pi is closer to 3.1415926535897 than  3.1415926358123...and how might you get more digits out of it?

  • mniip
    17th Apr 2013 Developer 0 Permalink
    @FeynmanLogomaker (View Post)
    use arbitary digit storage
  • FeynmanLogomaker
    17th Apr 2013 Member 0 Permalink

    How do you do that?

  • mniip
    17th Apr 2013 Developer 0 Permalink
    @FeynmanLogomaker (View Post)
    you store numbers as arrays of digits, then you define operations such as addition, subtraction, multiplication, division(the hardest one afaik)
  • jacob1
    17th Apr 2013 Developer 0 Permalink
    math.pi is 3.1415926535898. This might eventually get there, i'm not sure. due to float limitations probably not
  • FeynmanLogomaker
    18th Apr 2013 Member 0 Permalink

    OK, so it's just a novelty.