Integer, Real, and Number
The integer and real datatypes are the numeric classes.
class of 1 -- integer class of 1.1 -- real
A literal integer is a series of digits, possibly preceded by a minus
sign. The maximum integer is 536870911
, positive
or negative, which, as everyone knows, is
229-1. Any integer value outside this
range is implicitly coerced to a real.
A literal real is a series of digits with a decimal point, possibly
preceded by a minus sign. You may also use
“scientific notation”:
that’s a number followed by small or capital
e
, possibly followed by a plus sign or a minus
sign, followed by an integer; AppleScript might rewrite a scientific
notation number for you, but in any case it will always be a real.
For example:
1e2 -- rewritten: 100.0 2.1e26 -- rewritten: 2.1E+26
You can’t include a comma as a thousands separator in a literal number.
The class number
is purely for purposes of coercion. In
some situations you can use it to ask AppleScript to coerce to
whichever numeric datatype, integer or real, is appropriate. This is
nice because it saves you from having to worry
about which is appropriate.
class of ("1" as number) -- integer class of ("1.1" as number) -- real
Warning
However, number
cannot be used in every situation
where a numeric coercion is possible. For example, true as
integer
is legal, but true as number
is not. I regard this as a bug.
An integer is four bytes. A dictionary may occasionally mention a
class small integer
, which is two bytes (ranging from ...
Get AppleScript: The Definitive Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.