4

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?

7

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.

  • can I include the variable in command line counting? I need the variable to set itself to itself/2. – Prokop Hanzl Nov 3 '17 at 6:08
  • 1
    I've submitted an edit that includes passing the numbers from variables to the command line, but I ran into the problem of AppleScript representing the numbers as a float. – David Rouse Nov 3 '17 at 12:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .