## Recommended Posts

I have found a small ruby script on the internet that calculates pi. Using what (limited) ruby knowledge I have I tried to convert it to AutoIt, and failed. The code looks absolutely fine but it only returns "3000000000..." instead of "31415926535..." Here is what I have so far:

```#cs
#!/usr/local/bin/ruby
k, a, b, a1, b1 = 2, 4, 1, 12, 4
loop do
p, q, k = k*k, 2*k+1, k+1
a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
d, d1 = a/b, a1/b1
while d == d1
print d
\$stdout.flush
a, a1 = 10*(a%b), 10*(a1%b1)
d, d1 = a/b, a1/b1
end
end
#ce

\$pi = ""
\$k = 2
\$a = 4
\$b = 1
\$a1 = 12
\$b1 = 4
While 1
\$p = \$k*\$k
\$q = 2*\$k+1
\$k = \$k+1
\$a = \$a1
\$b = \$b1
\$a1 = \$p*\$a+\$q*\$a1
\$b1 = \$p*\$b+\$q*\$b1
\$d = \$a/\$b
\$d1 = \$a1/\$b1
While \$d = \$d1
\$pi &= \$d
MsgBox(0, "pi", \$pi)
\$a = 10*Mod(\$a,\$b)
\$a1 = 10*Mod(\$a1,\$b1)
\$d = \$a/\$b
\$d1 = \$a1/\$b1
Wend
Wend```

Any good ruby coders out there that want to help? Edited by erifash

##### Share on other sites

why should you want to calculate PI in autoit My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll

##### Share on other sites

why should you want to calculate PI in autoit I don't think it's been done yet and I really want to figure out why it isn't working. ##### Share on other sites

```\$PI = 22 / 7
MsgBox(0,"",\$PI)```

##### Share on other sites

```\$PI = 22 / 7
MsgBox(0,"",\$PI)```
I am trying to calculate pi to millions of decimal places, not hardcode it. Sorry if my intentions were unclear. ##### Share on other sites

autoIt isnt that acurate is it ?

Well, it's a different type of algorithm that doesn't need to have extreme amounts of decimals. I'm not sure how it does it but it works in ruby!

EDIT: Sorry, I get LUA and Ruby confused... changed.

Here is some output from the ruby script:

```3141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067

9821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819

6442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127

3724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609

4330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491

2983...```
And here is some output from the autoit script:
```3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

0000000000000000...```
Edited by erifash

##### Share on other sites

autoIt isnt that acurate is it ?

it's doesn't offer that level of precision by default, but you can set it up to do some long division that's not dependant on the actual numbers. example... you have a while loop that calls another function with 2 numbers, the other function just returns a result. multiply the remainder by 10, divide by the same number again, appending the results over and over and over, until you hit the string len limit of 2,147,483,647, then start a new string and continue the process. the 22/7 thing wouldn't work though, would it? my calc returns 3.1428571428571428571428571428571 which is already off as of the thousandths place...

##### Share on other sites

it's doesn't offer that level of precision by default, but you can set it up to do some long division that's not dependant on the actual numbers. example... you have a while loop that calls another function with 2 numbers, the other function just returns a result. multiply the remainder by 10, divide by the same number again, appending the results over and over and over, until you hit the string len limit of 2,147,483,647, then start a new string and continue the process. the 22/7 thing wouldn't work though, would it? my calc returns 3.1428571428571428571428571428571 which is already off as of the thousandths place...

don't try to do this recursively though if that was your intent, because of the 384 depth recursion limit with autoit...

##### Share on other sites

The original method I used was to write to a file. When it didn't work I took out all the fancy stuff I added and put in debug messageboxes. It only gets one digit at a time.

Edited by erifash

##### Share on other sites

The origional method I used was to write to a file. When it didn't work I took out all the fancy stuff I added and put in debug messageboxes.

what numbers are you starting with for diameter and circumference? I can whip something up for you

##### Share on other sites

I don't even know how the ruby code works, all I did was convert it to AutoIt. Sorry I can't help you there.

##### Share on other sites

Well I see what's wrong, but I don't get how the ruby script can make it right....

\$p = \$k*\$k ; 4

\$q = (2*\$k)+1 ; 5

\$k = \$k+1 ; 3

\$a = \$a1 ; 12

\$b = \$b1 ; 4

\$a1 = (\$p*\$a) + (\$q*\$a1) ; 108

\$b1 = (\$p*\$ + (\$q*\$b1) ; 36

\$d = \$a/\$b ; 3

\$d1 = \$a1/\$b1 ; 3

These are all the values when the script first runs the outside while loop.

The thing is, with the inside loop...

\$a = 10 * Mod (\$a,\$ \$a1 = 10 * Mod (\$a1,\$b1)

\$d = \$a/\$b

\$d1 = \$a1/\$b1

b and b1 never change, so when a%b returns 0 (12/4 divides evenly), and a1%b1 returns 0 (108/36 divides evenly as well), the loop is stuck. So, something's weird with ruby. That's my answer.

##### Share on other sites

OK I kinda get it. Ruby evaluates these two lines at the same time.

\$a = \$a1 ; 12

\$a1 = (\$p*\$a) + (\$q*\$a1) ; 108

Instead of getting a first and then plugging it into the a1 equation, it uses the a as defined above. I'm assuming it does the same with the b1 line. Weird.

##### Share on other sites

Ok I tried again. This time I tried doing:

\$d = Int (\$a/\$ \$d1 = Int (\$a1/\$b1)

to see if it was ruby's default behavior. It worked for a few digits (which is better than just 1), but then totally died. So I still think ruby has some weird thinking.

Wait... it is still using the values from the top in this case... man ruby confuses me. If I didn't like pi so much, I would have killed this program long ago.

Edited by greenmachine

##### Share on other sites

OK I kinda get it. Ruby evaluates these two lines at the same time.

\$a = \$a1 ; 12

\$a1 = (\$p*\$a) + (\$q*\$a1) ; 108

Instead of getting a first and then plugging it into the a1 equation, it uses the a as defined above. I'm assuming it does the same with the b1 line. Weird.

In that case, this might be closer to the behavior:
```Dim \$pi = "", \$k = 2, \$a = 4, \$b = 1, \$a1 = 12, \$b1 = 4
While 1
Dim \$p = \$k*\$k, \$q = 2*\$k+1, \$k = \$k+1

; Notice the temporary values
Dim \$_a = \$a, \$_b = \$b, \$_a1 = \$a1, \$_b1 = \$b1
\$a = \$_a1
\$b = \$_b1
\$a1 = \$p*\$_a+\$q*\$_a1
\$b1 = \$p*\$_b+\$q*\$_b1
\$d = Int(\$a/\$b)
\$d1 = Int(\$a1/\$b1)
While \$d = \$d1
\$pi &= \$d
MsgBox(0, "pi", \$pi)

\$a = 10*Mod(\$a,\$b)
\$a1 = 10*Mod(\$a1,\$b1)
\$d = Int(\$a/\$b)
\$d1 = Int(\$a1/\$b1)
Wend
Wend```

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

##### Share on other sites

Close, but only because it outputs 1 digit at a time. They're way off though. I don't know a thing about ruby, I'm just trying to decipher it from the script, so my findings probably aren't accurate yet.

##### Share on other sites

I was talking to my friend (kevin) today about it and he said that ruby does some weird things with division:

```3 / 4 = 0
3.0 / 4.0 = 0.75```
In ruby you have to put the decimal place if you want to divide floats. The pi ruby script is using integer division which returns either the ceiling or floor of the number. Kevin is trying to figure out in which instances it does this. He had it working a month ago but he deleted it by mistake. @CyberSlug I believe that you came the closest, now all we need to do is fix the division issue.

@Larry Thank you but C# scares me... Edited by erifash

##### Share on other sites

When I was looking at it it seemed to always do Floor. But.. then when I put it into code I got as far as 3.141592653 and then I got like.. 9.13453465234762457 e^-237. Something totally wrong and out of nowhere. So... yeah it's ugly.

## Create an account

Register a new account

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...