Arithmetic Operators
The arithmetic operators combine numbers to get new numbers in accordance with the usual rules of arithmetic. As in most computer languages, multiplication and division take precedence over addition and subtraction (in the absence of parentheses). So, for example:
3 + 4 * 2 -- 11 3 * 4 + 2 -- 14
An operand that is a list consisting of one number will be coerced to a number. An operand that is a string representing a number, or a list consisting of one such string, will be coerced to a number.
The class of the result of the addition, subtraction, multiplication, and remainder operators is as follows: the result is an integer if the first operand is an integer and if the second operand either is an integer or is a real that can be coerced to an integer without loss of information. Otherwise, the result is a real.
Warning
If you have some programming experience and you know a language such as C or LISP
(where if either arithmetic operand is a real, the result is a real), you're going to find AppleScript's behavior surprising. Think of it like this. For the result to be a real, it is not enough that one operand be a real; the first operand must be a real, or else the second operand must have a significant fractional part, not merely a decimal point. Saying x * 1.0, for example, is not a way of making sure you've got a real. When in doubt, coerce explicitly.
Do not blame AppleScript for the phenomena inherent in doing floating-point arithmetic in any language on any ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access