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
Related:
Divide
Command
Behavior: Use the divide
command when you want to divide a value in a container by another number, replacing the value in the container by the quotient. Lists of values can be divided, 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 divided 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:
divide {chunk of} container by numExpr
Examples
divide score by totalCount
divide item 1 of balances by 12
Related:
Arithmetic Functions
Use these functions to manipulate numbers.
Abs
, As a positive number
, As a negative number
Functions
Behavior: Returns the absolute value of its numeric parameter. The absolute value is the magnitude of a number regardless of its sign — it is always positive or zero. As a positive number
returns the absolute value. As a negative number
returns the negative absolute value.
Syntax:
abs( numExpr )
the abs of numFactor
numFactor as a positive number
numFactor as a negative number
Examples
put abs(-11) --> 11
put the abs of -11 --> 11
Example
if height is a negative number then put abs(height) into height
Examples
put (3 - 16) as a positive number --> 13
put 500 as a negative number --> -500
ACos
Function
Behavior: Calculates the trigonometric arc cosine of a value and returns the result as an angle expressed in radians.
Syntax:
acos( numExpr )
{the} acos of numFactor
Examples
put acos(0) --> 1.570796 radians
put the acos of 0 --> 1.570796 radians
put acos(0.5) as degrees --> 60 degrees
Related:
ACosh
Function
Behavior: Calculates the inverse hyperbolic cosine of a value and returns the result as an angle expressed in radians.
Syntax:
acosh( numExpr )
{the} acosh of numFactor
Examples
put acosh(2.509178) rounded to the nearest degree --> 90 degrees
put the acosh of 1 --> 0 radians
Related:
Annuity
Function
Behavior: Calculates the present value of an ordinary annuity with payments of one unit, based on the specified interest rate per period and the number of periods.
Syntax:
annuity( interest, periods )
Example
put annuity(1%, 32) --> 27.269589
Related:
ASin
Function
Behavior: Calculates the trigonometric arc sine of a value and returns the result as an angle expressed in radians.
Syntax:
asin( numExpr )
{the} asin of numFactor
Examples
put asin(0) --> 0 radians
put asin of 1 --> 1.570796 radians
put asin(0.5) as degrees --> 30 degrees
Related:
ASinh
Function
Behavior: Calculates the inverse hyperbolic sine of a value and returns the result as an angle expressed in radians.
Syntax:
asinh( numExpr )
{the} asinh of numFactor
Examples
put asinh(0) --> 0 radians
put the asinh of 2.301299 rounded to the nearest degree --> 90 degrees
Related:
ATan
Function
Behavior: Returns the trigonometric arctangent of its parameter as an angle expressed in radians.
Syntax:
atan( numExpr )
the atan of numFactor
Examples
put atan(19) --> 1.518213 radians
put the atan of 5 --> 1.373401 radians
Related:
ATanh
Function
Behavior: Calculates the inverse hyperbolic tangent of a value and returns the result as an angle expressed in radians.
Syntax:
atanh( numExpr )
{the} atanh of numFactor
Examples
put atanh(0.917152) rounded to the nearest degree --> 90 degrees
put the atanh of 0 --> 0 radians
Related:
ATanPt
Function
Behavior: Calculates the trigonometric arc tangent of a point and returns the result as an angle expressed in radians.
Syntax:
atanPt( aPoint )
atanPt( xCoord , yCoord )
{the} atanPt of aPoint
The atanPt
function provides the same functionality as the atan2 function in other languages, but accepts either a point, or coordinates in x,y order as parameters, rather than y,x.
Examples
put atanPt(123,0) --> 0 radians
put the atanPt of [11,56] --> 1.376837 radians
Example
set myPoint to [50,50]
put atanPt(myPoint) as degrees --> 45 degrees
Related:
Average
Function
Behavior: Returns the average of its parameters.
Syntax:
average( numList )
{the} average of numList
The parameter numList may be a list of numbers, an expression which evaluates to a list of numbers separated by commas, or a combination of these, nested to any depth.
Examples
put average(8, 10, 12) --> 10
put the average of [3,7,15,84,6,42,11] --> 24
Example
This example is an incomplete portion of code meant to illustrate how the feature may be used
if the average of [x,y,z] is greater than z then
put "Z is below average!"
end if
Related:
Compound
Function
Behavior: Computes the principal plus accrued interest on an investment of 1 unit, based on the specified interest rate and the number of periods.
Syntax:
compound( interest, periods )
Example
put compound(7.25%, 6) --> 1.521892
Example
put 10 into initialinvestment
put initialInvestment * compound(6.7%, 12) into currentValue
put currentvalue --> 21.775746
Related:
Cos
Function
Behavior: Returns the trigonometric cosine of its parameter, which is an angle expressed in radians.
Syntax:
cos( numExpr )
{the} cos of numFactor
Examples
put cos(18) --> 0.660317
put the cos of 18 --> 0.660317
Related:
Cosh
Function
Behavior: Returns the hyperbolic cosine of its parameter, which is an angle expressed in radians.
Syntax:
cosh( numExpr )
{the} cosh of numFactor
Examples
put cosh(0) --> 1 (no units specified, so 0 radians assumed)
put cosh of 90 degrees --> 2.509178
Related:
Cube Root
, CbRt
Function
Behavior: Computes the cube root of its parameter.
Syntax:
cube root( number )
{the} cube root of number
cbrt( numExpr )
Examples
put cube root (27) --> 3
put the cube root of 8 --> 2
put cbrt(125) --> 5
Related:
Exp
Function
Behavior: Returns the natural exponential of its parameter (that is, the mathematical constant e raised to the power of the parameter).
Syntax:
exp( numExpr )
{the} exp of numFactor
Examples
put exp(2) --> 7.389056
put the exp of 1 --> 2.718282
Related:
Exp1
Function
Behavior: Returns one less than the natural exponential of its parameter (that is, the mathematical constant e raised to the power of the parameter, minus 1).
Syntax:
exp1( numExpr )
{the} exp1 of numFactor
Examples
put exp1(2) --> 6.389056
put the exp1 of 2 --> 6.389056
Related:
Exp2
Function
Behavior: Returns 2 raised to the power of its parameter.
Syntax:
exp2( numExpr )
{the} exp2 of numFactor
Examples
put exp2(8) --> 256
put exp2 of 10 --> 1024
Related:
Frac
Function
Behavior: Returns the fractional part of a number. Use the trunc
function to get the whole number part of a value. The trunc
and frac
functions are defined such that trunc(x) + frac(x)
is always equal to x
.
Syntax:
frac( numExpr )
{the} frac of numFactor
Examples
put frac(81.236) --> 0.236
put frac of 8.125 --> 0.125
Related:
Ln
Function
Behavior: Returns the natural logarithm of its parameter.
Syntax:
ln( numExpr )
{the} ln of numFactor
Examples
put ln(2) --> 0.693147
put the ln of 4 --> 1.386294
Related: