MacOS – How to make AppleScript count exactly

applescriptmacos

How do I make AppleScript count exactly?

According to AppleScript, the following equation would be true:
89723443578920345892 / 2 = 448617217894600000000

That, though, is not true. What can I do in order to make AppleScript actually count without rounding it afterward?

Best Answer

The command line utility bc is capable of arbitrary precision mathematics, and you can call a command line utility from AppleScript and get a value from it.

For example:

set myVariable to do shell script "echo \"89723443578920345892 / 2\" | bc"

Gives this result: 44861721789460172946

To pass command line arguments as AppleScript variables:

set myNumber1 to "89723443578920345892"
set myNumber2 to "2"
set myVariable to do shell script "echo " & myNumber1 & " / " & myNumber2 & "| bc"

But this gets us back to the original problem, AppleScript represents large numbers as a float, so I had to pass the value as a string. So depending on where you get the very large number from, this could be problematic or a non-solution.