Hey Everyone.

I’m new here, and hope maybe someone will be able to help me out. I used Xcode and Interface Builder to design an interface, and wrote the code in applescript. Here it is:

```
on clicked theObject
tell window of theObject
try
set theID to contents of text field "iterations" as number
set a to 0
end try
repeat with i from 0 to theID
try
set b to ((4 / ((8 * i) + 1)) - (2 / ((8 * i) + 4)) - (1 / ((8 * i) + 5)) - (1 / ((8 * i) + 6))) * ((0.0625) ^ i) as real
set a to a + b as real
end try
end repeat
try
set contents of text field "pi" to a
end try
end tell
end clicked
on should quit after last window closed theObject
return true
end should quit after last window closed
```

Here’s the thing, the above works perfectly, except when displayed in the text box, it only shows 15 digits of the decimal, which essentially defeats the purpose of the application. Any ideas on how I can make it show up to the decimal of my choosing, or until it terminates (even better)? Thanks in advance!

You’re displaying a real number. That has the number of digits that real numbers have. If I understand correctly, you want to calculate pi to an arbitrary number of decimal places, correct?

Whatever algorithm you use has to figure out what the next decimal place number is, and append it to the previous result, both being text strings (using the “a & b” syntax).

pseudo-code:

set pistring to “3.14159265”

repeat with i from 9 to n

set digitstring to my next_digit(i) as text – calculate the next digit somehow

set pistring to pistring & digitstring

end repeat

to next_digit(i)

– put code here that figures out the next decimal place

return theDigit as text

end next_digit

you have to do it as text because the mantissa of floating point numbers (real) are limited to the machine’s length of single and double-precision floats.

I’m no math guy though - there may be a way to do it with Apple’s “Bignum” math libraries. Applescript probably isn’t going to be too fast if you are planning on doing bazillions of decimal places.

Hey, thanks for the reply

I’m sorry, but I think my post was a little misleading. Applescript is truncating the decimal at 15 decimal places. The calculation, when done out on paper has many, many more decimal places (both correct to a point and incorrect there after. The higher the “iterations” the more accurate it is). So assuming that, how can I get it to output the result of the function in its completion? As Text?

AppleScript uses double-precision (64-bit) for all “real” values. You cannot get more than 64 bits of information. Try this and you will see what I mean:

```
pi as real
```

or

```
22/7 as real
```

You will get about 12 decimal places. That is all that 64 bits can represent.

To generate huge numbers of decimal places, or more than 15, you will have to research for an algorithm that just computes the next decimal digit. Then append that to the end as I said above.

Okay. Thanks for the help. Do you know if I decided to switch up and write it in C, if it would work the way I want it to?

Nope. No matter what language you use, real numbers are still 64 bits - that’s the hardware. You need a different algorithm - one that does not require you to try to hold the whole number in memory as a number. Google “calculating pi” .

Actually, here is a link to Apple’s Bignum API which can do arithmetic on numbers up to 1048 bits. If that is enough for your project it might be just the thing. This link is a zipped folder with C code in it.

http://developer.apple.com/hardware/ve/downloads/vBigNumPB.sit.hqx