Jump to content

Calculate Pi


Pakku
 Share

Recommended Posts

Hi all,

On Wikipedia I found a page about Pi and how to calculate this.

So i converted the C code on Wikipedia for calculating Pi in AutoIt code and here it is!

The first one is just a script which calculates Pi until you are 80 years or something.

The second is a UDF.

The script:

$a = 0
$b = 0
$c = 0
$start = TimerInit()

While 1
    $a += 1
    $b += 1
    $c += 1/($a*$a)
    If $b = 100000 Then
        $b = 0
        ConsoleWrite(Sqrt($c*6))
        ConsoleWrite(@CRLF)
        ConsoleWrite("   " & Round(TimerDiff($start)) / 1000 & " seconds")
        ConsoleWrite(@CRLF)
        $start = TimerInit()
    EndIf
WEnd

Feedback is welcome :)

EDIT: Busti optimized the loop in the UDF

Edited by Pakku
Link to comment
Share on other sites

Hi,

good one, thanks!

i changed 1000 in 100000 because it is more precise than 1000

Edited by Pakku
Link to comment
Share on other sites

Thank all!

The script returns a number with 14 digits, is there a way to let the script return it's answer with about 100 or something digits?

Edited by Pakku
Link to comment
Share on other sites

is there a way to let the script return it's answer with about 100 or something digits?

; Michel Claveau Informatique

msgbox(0,"Pi 100 car",_pi())
msgbox(0,"Pi 10 car",_pi(10))
msgbox(0,"Pi 1000 car",_pi(1000))

Func _pi($nb=100)
    $pi= '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421
170679'
    $pi=$pi&'821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303
8196'
    $pi=$pi&'442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914
1273'
    $pi=$pi&'724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511
6094'
    $pi=$pi&'330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119
4912'
    $pi=$pi&'983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940
5132'
    $pi=$pi&'000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258
9235'
    $pi=$pi&'420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963
1859'
    $pi=$pi&'502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914
7303'
    $pi=$pi&'598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420
1989'
    Return(StringLeft($pi,$nb))
EndFunc
Link to comment
Share on other sites

Hi!

Another release, only for test the forum, with big line.

; Michel Claveau Informatique

msgbox(0,"Pi 100 car",pi())
msgbox(0,"Pi 10 car",pi(10))
msgbox(0,"Pi 1000 car",pi(1000))

Func pi($nb=100)
    $pi='3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421
1706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493
0381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249
1412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415
1160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301
1949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669
4051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892
5892354201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609
6318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669
1473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164
201989'
    Return(StringLeft($pi,$nb))
EndFunc
Link to comment
Share on other sites

Hi,

That isn't realy what i ment, i ment: is there a way to CALCULATE the value of Pi with more than 15 characters.

According to the help file it can't but maybe we can find an other way to do this?

Edited by Pakku
Link to comment
Share on other sites

You'd have to use a string representation of the number and manipulate that- the precision of floating point is too little for your needs.

#)

Link to comment
Share on other sites

Hi,

next question: how to do that, i have never done that before

Edited by Pakku
Link to comment
Share on other sites

Just so everyones clear.... 5 decimals of pi is about as far as most people will ever need... bothering to go beyond that is a matter of semantics for 99.9999999999999999999999999999999999999999999999999999999999999999999999999999999999% of the population if you get my drift.

Link to comment
Share on other sites

Just so everyones clear.... 5 decimals of pi is about as far as most people will ever need... bothering to go beyond that is a matter of semantics for 99.9999999999999999999999999999999999999999999999999999999999999999999999999999999999% of the population if you get my drift.

Why would you else calculate Pi for, the fun thing of this script is to calculate Pi over more than 5 decimals, but 15 isn't enough for me.

Link to comment
Share on other sites

Why would you else calculate Pi for, the fun thing of this script is to calculate Pi over more than 5 decimals, but 15 isn't enough for me.

I think so, too! I'd link to habe a script, what calculates PI to endless decimals (you should set the number of decimals before) :)

[CENTER]Sorry for my bad English... ;)[/CENTER]

Link to comment
Share on other sites

Sorry for my bad English... :)

your english isn't that bad as mine, but do you or any one have an idee how to do this

Edited by Pakku
Link to comment
Share on other sites

This was fun to write, although it doesn't show how to calculate Pi in more places..

MsgBox(0, "This is a demonstration of calculating Pi", "Actual Pi: 3.1415926535897932384626433832795" & @CRLF & @CRLF & "Arjan: "& Pi() & @CRLF & "Gregory: " & Gregory() & @CRLF & "Newton: " & Newton() & @CRLF & "Manadar: " & Manadar()  & @CRLF &  "Wallis: " & Wallis())

Func Pi($calc = 100000)
    Local $b = 0
    For $a = 1 To $calc
        $b += 1/($a*$a)
    Next
    Return Sqrt($b*6)
EndFunc

Func Wallis() ;methos is faulty from 20 and up
    ;requires integral calculation... I'm clueless?!? Starting on integral calculation in a month at school ^o)
    
    ;http://www.escape.com/~paulg53/math/pi/wallis/index.html
    Local $numerator = 1
    Local $dividor = 1
    For $x = 1 to 61
        If $x/2 = Int($x/2) Then
            $numerator *= $x * $x
        Else
            $dividor *= $x * $x
        EndIf
        ;MsgBox(0, "Faulty from 20", $numerator & @CRLF & "---------------------------------" & @CRLF & $dividor)
    Next
    Return ($numerator/$dividor)*2
EndFunc

Func Gregory() ;Leibniz and Machin
    ;http://www.escape.com/~paulg53/math/pi/greg/index.html
    Local $pi = 4*ATan(1/5)-ATan(1/239)
    Return $pi*4
EndFunc

Func Newton()
    ;http://www.escape.com/~paulg53/math/pi/newton/index.html
    $a = Sqrt(3)/23
    $b = Sqrt(3)/32
    $c = $a + $b
    $d = 24 * $c
    Return $d
EndFunc

Func Manadar()
    ; lol, this is totally random! I want Apple3.14159265358
    Return Sin(0.5)*6.5
EndFunc

A lot more stuff on Pi that i have to try! http://www.escape.com/~paulg53/math/pi/links.html

Edited by Manadar
Link to comment
Share on other sites

It would also be nice if it could output the indivudual decimals as they are calculated. Like type them into notepad or something.

http://www.autoitking.co.nr Site is DOWN | My deviantART | No Topic Topic - Don't do it!-------------------- UDF's/Scripts:AutoIt: [BenEditor 3.6] [_ShutDown()]PHP: [CommentScript]Web Based AutoIt: [MemStats] [HTML to AU3] [User LogIn and SignUp script]
Link to comment
Share on other sites

This was fun to write, although it doesn't show how to calculate Pi in more places..

MsgBox(0, "This is a demonstration of calculating Pi", "Actual Pi: 3.1415926535897932384626433832795" & @CRLF & @CRLF & "Arjan: "& Pi() & @CRLF & "Gregory: " & Gregory() & @CRLF & "Newton: " & Newton() & @CRLF & "Manadar: " & Manadar()  & @CRLF &  "Wallis: " & Wallis())

Func Pi($calc = 100000)
    Local $b = 0
    For $a = 1 To $calc
        $b += 1/($a*$a)
    Next
    Return Sqrt($b*6)
EndFunc

Func Wallis() ;methos is faulty from 20 and up
    ;requires integral calculation... I'm clueless?!? Starting on integral calculation in a month at school ^o)
    
    ;http://www.escape.com/~paulg53/math/pi/wallis/index.html
    Local $numerator = 1
    Local $dividor = 1
    For $x = 1 to 61
        If $x/2 = Int($x/2) Then
            $numerator *= $x * $x
        Else
            $dividor *= $x * $x
        EndIf
        ;MsgBox(0, "Faulty from 20", $numerator & @CRLF & "---------------------------------" & @CRLF & $dividor)
    Next
    Return ($numerator/$dividor)*2
EndFunc

Func Gregory() ;Leibniz and Machin
    ;http://www.escape.com/~paulg53/math/pi/greg/index.html
    Local $pi = 4*ATan(1/5)-ATan(1/239)
    Return $pi*4
EndFunc

Func Newton()
    ;http://www.escape.com/~paulg53/math/pi/newton/index.html
    $a = Sqrt(3)/23
    $b = Sqrt(3)/32
    $c = $a + $b
    $d = 24 * $c
    Return $d
EndFunc

Func Manadar()
    ; lol, this is totally random! I want Apple3.14159265358
    Return Sin(0.5)*6.5
EndFunc

A lot more stuff on Pi that i have to try! http://www.escape.com/~paulg53/math/pi/links.html

Nicely done!
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...