Allowed coercions
- String
- list with one item
- integer (if there is no fractional part)
- number (a synonym)
Syntax
set theNumber to 25.6 as real
Description
A
real is a positive or negative number that can
include a decimal point, such as -512.5 or 3.14159265359 (the value
of the pi predefined variable is a
real value type). Use real when
you want a variable to store a very high number. The largest positive
value that a real number can reach with AppleScript 1.4.3 on OS 9 is
1.797693E+308. This very large number, however, can safely be
exceeded with real data types under AppleScript
1.5.5/1.6 (Mac OS 9.1 and OS X).
Examples
The second line of this example raises an error in AppleScript
Version 1.4.3 and Script Editor: "The result of a
numeric operation was too large":
set theVar to 1.797693E+308
set theVar to theVar + 1
At any rate, this is a giant number. To use scientific notation,
follow the real or floating-point number with the
letter "e" (upper- or lowercase)
along with an integer like 20 (e.g., 2.0e20 or
2.0e+20; the "+" is optional, any
"-" sign is not). If the
integer is a positive
n,then the number is equal
to real number * 10n;
if n is negative then the number is equal to
real number * 10-n.
AppleScript converts to scientific notation the
real numbers that are greater than or equal to
10,000.0 when the script is compiled. My machine also converts to
scientific notation numbers that are less than or equal to 0.001, but
the AppleScript Language Guide for AppleScript
1.3.7 ascribes this behavior to numbers less than or equal
to 0.0001.
 |
AppleScript will automatically use the real value
type for all numbers with decimal points. The results of math
operations that use the / or ÷
operators are always reals. The results of
calculations using *, +,
-, ^, and
mod operators are reals or
integers depending on the magnitude of the results
or whether the operands are reals or not.
(Operators are covered later in Chapter 4.)
|
|
You can coerce an integer to a
real, and AppleScript will drop a
".0" on the end of the number.
However, you cannot coerce a real to an
integer if the real has an
actual fractional part. For instance, 6.3 cannot be coerced to an
integer, but 6.0 can. In other words, AppleScript
does not automatically chuck out the fractional part of a
real when coercing it to an
integer. It first determines whether the coercion
is legal or not. This code is an illustration of this process:
set theVar to 6.3
set theVar to theVar + 0.7 (* theVar is now 7.0 so it can be coerced to an
integer *)
set theVar to theVar as integer
log theVar
log class of theVar -- the class is integer
|