Using Numbers with Units
In SenseTalk, you can use numbers, either as numerals or text, for many types of operations and comparisons. Numbers often represent a quantity of some particular type, not merely an abstract value. For instance, the area of a rectangle might be referenced as "9 square inches" or an account balance might contain $698.53.
SenseTalk lets you associate a unit type such as inches or dollars with a number to add meaning and relevance. Assignments to variables and calculations using those values maintain and convert units as needed.
Working with Units
To specify a numeric value with a unit type, you include the unit name after the value. (The exception to this rule is the dollar symbol — $ — as described below.) SenseTalk recognizes singular, plural, and abbreviated unit names as well as "square" and "cubic" modifiers for area and volume:
put 3 ft --> "3 feet"
set weight to four grams
put 9 sq in --> "9 square inches"
Note: You do not include a period with unit abbreviations.
You can use chained values of the same type, with one value after another or with values joined by and
. Such representations are commonly used for weight or distance measurements:
put 6 ft 3 in --> "6.25 feet"
set weight to 2 pounds and 3 ounces
put weight --> "2.1875 pounds"
As you begin to see in the examples above, you can mix units in your expressions. Units are preserved or converted as needed in assignments and calculations:
put 3 ft into width
add 1 yard to width -- 1 yard is automatically converted to 3 ft in order to add it to the existing width
put width --> "6 feet"
put width * 4 feet into area
put area --> "24 square feet"
add 2 liters to area -- throws a mismatched units exception
You can combine units to form complex unit types such as speed (distance per timeInterval) or volume (length times width times depth):
set speed to 25 mi/hr
put speed --> "25 miles per hour"
put 3 hrs 45 min * speed --> "93.75 miles"
set accelerationOfGravity to 32 ft/s^2
set flow to 5 gallons per minute
set clickRate to 500/hr
set price to $5.96 per lb -- same as: 5.96 dollars per pound
set weight to 4 oz
put price * weight --> "$1.49"
A Note on Unit Compatibility
SenseTalk converts units to combine values when it makes sense (as in combining 3 feet and 1 yard to get 6 feet). However, if you try to combine units that can't logically be combined (such as a flat area measurement and a volume measurement), you will get an error.
Units must be compatible (represent the same type of measure) to be added, subtracted or compared. That is, a unit of length (for example) can be added to another unit of length, even if the specific unit type is different (meters + feet + inches = OK!). These types are said to be compatible.
However, attempting to perform addition, subtraction, or comparison with incompatible units will throw an exception:
put average(4 in, 5 ft, 1 meter) --> 0.8752 meters
put average(4 in, 5 ft, 1 pint) -- throws an exception because of differing unit types
Unit Conversions
As mentioned above, SenseTalk will automatically convert values from one unit type to another. This ability can be particularly useful in mathematical operations. You can also explicitly convert units from one type to another.
Addition, Subtraction
SenseTalk automatically converts values to compatible units for addition and subtraction operations, and to compare two values for equality. For addition and subtraction, the resulting value will favor one unit type over another based on the internal ordering of the units in the system.
For example, adding inches and centimeters gives the result in centimeters regardless of the order of the operands. Adding meters and centimeters results in meters. However, you can use the as
operator to specify the result type:
put 8 inches + 3 cm -- converts 8 inches to centimeters and adds 3 centimeters to it,
-- so prints: "23.32 centimeters"
put (8 inches + 3 cm) as inches -- as above, but converts to inches before output,
-- so prints: "9.181102 inches"
Addition or subtraction of a number with a unit type and a plain number (i.e., a number without a unit type) results in an error.
Multiplication, Division
When multiplying and dividing with units, whether the unit is maintained or converted is a little more complicated. You can use plain numbers to multiply or divide numbers with units, and the result preserves the unit type:
set height to 20 feet
set width to 3 times height
put width --> 60 feet
set var1 to 2
put 10 hours / var1 --> 5 hours
When dividing where both numbers include units, values with compatible unit types are converted to the same unit, just as for addition. The units, however, are dropped from the results:
Put 4 yards divided by 2 feet -- Prints: "6" (4 yards can be divided into 6 2-foot segments)
When numbers with units are multiplied, whether compatible or incompatible, the units are kept and a complex unit type is formed. For example, dividing miles by hours will keep both units, resulting in a value with the complex unit type miles per hour:
put 500 miles divided by 4 hours --> 125 miles per hour
put 4 yard * 2 feet --> 24 square feet
put 2 liters times 2 liters --> 4 liters^2