I’ve got an applet that has an accounting component which as you might imagine involves some basic adding and subtracting of numbers with up to two decimal places. I thought that such basic maths would be a cinch for computers and by extension AppleScript but, somehow, I’ve now discovered that to be naive.

I started noticing that some of the balances/values in my applet seemed off and first tried redoing the maths and double checking my app logic but that didn’t seem to fix/explain the issue.

I tried reading some academic articles on floating point numbers and some AppleScript documentation and read just enough of the former to start feeling sleepy (and give me a hint of what the issue might be) and decided just to play around in Script Debugger instead.

I don’t know whether AppleScript’s reals are floating point numbers or not and whether scientific notation is the same thing as floating points or a type of it or what, but basically if you run the following code you’ll see AppleScript has difficulty with add-ups and take-aways when the numbers are large enough and involve decimal places (at least those seem to be the issues).

```
set x to 100000.66
set y to 100000
log x - y
```

It seems I need to write my own basic maths functions. Thankfully I only need adding and subtracting, and it’s with currency so will only ever have up to two decimal places. So I’ve written the below.

##AppleScript Handler for Addition##

```
on sum of (x as number) to y as number
return (round ((x + y) * 100)) / 100
end sum
```

##AppleScript Handler for Subtraction##

```
on difference of x from y
return (round ((y - x) * 100)) / 100
end difference
```

I was a little surprised that there wasn’t more documentation and sample subroutines on this – at least I didn’t find much after one or two lazy Googles.

Are those handlers a sensible way to deal with this? If so I’ll now go through my app and replace all the basic maths operations with calls to those handlers. Will I run into any other weird issues using those handlers? Are there better ones?

Thanks for any wisdom or resources you can offer.