Arithmetic Calculations in SenseTalk
You can perform mathematical operations in SenseTalk by using mathematical operators as well as a variety of commands and functions. For detailed information about using operators, see the Expressions page, which describes the basics of using expressions in SenseTalk and links to pages for specific mathematical operators (+, -, *, and other operators) and other useful concepts such as comparisons and logical operators.
SenseTalk's arithmetic commands and functions also provide useful tools for working with numbers. Arithmetic Commands can be used to modify numeric values stored in containers. These commands perform the same arithmetic functions as the +, -, *, and / operators. The difference is that these commands take one of their operands from a container and store the result of the calculation back into that container. Arithmetic Functions operate on numbers. You can use these functions to manipulate numbers in a variety of ways.
In a topic related to arithmetic calculations, Points and Rectangles describes how SenseTalk understands the concepts of geometric points and rectangles. Any list of two numbers, or a text string consisting of two numbers separated by a comma, can be treated as a point. The functions explained on the Points and Rectangles page can be used to extract the various component values of a point or rectangle.
Arithmetic Commands
There are four arithmetic commands:
Use them to modify values stored in containers. These commands perform the same arithmetic functions as the +, -, *, and / operators. The difference is that these commands take one of their operands from a container and store the result of the calculation back into that container.
Add
Command
Behavior: Use the add
command when you want to add a number to the value in a container, replacing the value in the container by the sum. Lists of values can be added, provided that both the source and destination lists contain the same number of items. Each item from the source list is added to the corresponding item of the destination container. NumExpr is a source expression. It can be a number, any formula, or another container. Chunk is a chunk expression describing part of a container (lines, words, items, or characters). Container is any container.
Syntax:
add numExpr to {chunk of} container
Examples
add amount to dollarsVariable
add 37 to item 2 of line 3 of scores
add speed * time to item 1 of distances
add [10,5] to centerPoint
Related:
Subtract
Command
Behavior: Use the subtract
command when you want to subtract a number from the value in a container, replacing the value in the container by the result. Lists of values can be subtracted, provided that both the source and destination lists contain the same number of items. Each item from the source list is subtracted from the corresponding item of the destination container. NumExpr is a source expression. It can be a number, any formula, or another container. Chunk is a chunk expression describing part of a container (lines, words, items, or characters). Container is any container.
Syntax:
subtract numExpr from {chunk of} container
Examples
subtract checkAmt from accountBalance
subtract 1 from property CountDown of gameController
subtract rate * pmt from line 4 of amortization
subtract [1,2] from boxDimensions
Related:
Multiply
Command
Behavior: Use the multiply
command when you want to multiply a value in a container by another number, replacing the value in the container by the product. Lists of values can be multiplied, provided that both the source and destination lists contain the same number of items, or that the source is a single value. Each item in the destination container is multiplied by the corresponding item of the source list, or by the source value. NumExpr is a source expression. It can be a number, any formula, or another container. Chunk is a chunk expression describing part of a container (lines, words, items, or characters). Container is any container.
Syntax:
multiply {chunk of} container by numExpr
Examples
multiply score by weightingFactor
multiply accountBalance by 1 + interestRate
multiply item 3 of line x of table by 2