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.

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

Example:

Example:

add 37 to item 2 of line 3 of scores

Example:

add speed * time to item 1 of distances

Example:

Related: the subtract command

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

Example:

subtract checkAmt from accountBalance

Example:

subtract 1 from property CountDown of gameController

Example:

subtract rate * pmt from line 4 of amortization

Example:

subtract [1,2] from boxDimensions

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

Example:

multiply score by weightingFactor

Example:

multiply accountBalance by 1 + interestRate

Example:

multiply item 3 of line x of table by 2

Related: Divide Command

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

Example:

divide score by totalCount

Example:

divide item 1 of balances by 12

Related: Multiply Command

Arithmetic Functions

Use these functions to manipulate numbers.

In this section:

abs function

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.

Syntax:

the abs of numFactor

abs(numExpr)

Example:

put abs(-11) -- shows 11

Example:

if height is a negative number then put abs(height) into height

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)

Related: the compound function

ATan Function

Behavior: Returns the trigonometric arctangent of its parameter as an angle expressed in radians.

Syntax:

the atan of numFactor

atan(numExpr)

Example:

put atan(19) -- shows 1.518213

Average Function

Behavior: Returns the average of its parameters. numList may either 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.

Syntax:

{the} average of numList

average(numList)

Example:

put average(8, 10, 12) -- shows 10

Example:

if the average of [x,y,z] is greater than z then

put "Z is below average!"

end if

Related: the median function

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) -- shows 1.521892

Example:

put initialInvestment * compound(6.7%, 12) into currentValue

Related: the annuity function

Cos Function

Behavior: Returns the trigonometric cosine of its parameter, which is an angle expressed in radians.

Syntax:

{the} cos of numFactor

cos(numExpr)

Example:

put cos(18) -- shows 0.660317

Cube Root Function

Behavior: Computes the cube root of its parameter.

Syntax:

put cube root(number)

put the cube root of number

Example:

put cube root (27)--3

Example:

put the cube root of 8--2

Exp Function

Behavior: Returns the natural exponential of its parameter (that is, the mathematical constant e raised to the power of the parameter).

Syntax:

{the} exp of numFactor

exp(numExpr)

Example:

put exp(2) -- 7.389056

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:

{the} exp1 of numFactor

exp1(numExpr)

Example:

put expl(2) -- 6.389056

exp2 function

Behavior: Returns 2 raised to the power of its parameter.

Syntax:

{the} exp2 of numFactor

exp2(numExpr)

Example:

put exp2(8) -- 256

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:

{the} frac of numFactor

frac(numExpr)

Example:

put frac(81.236) -- .236

Related: the trunc function

Ln Function

Behavior: Returns the natural logarithm of its parameter.

Syntax:

{the} ln of numFactor

ln(numExpr)

Examples

put ln(2) -- 0.693147

Ln1 Function

Behavior: Returns the natural logarithm of 1 more than its parameter.

Syntax:

{the} ln1 of numFactor

ln1(numExpr)

Example:

put ln1(2) -- 1.098612

Log2 Function

Behavior: Returns the base 2 logarithm of its parameter.

Syntax:

{the} log2 of numFactor

log2(numExpr)

Example:

put log2(256) -- 8

Maximum, Max, HighestValue, LargestValue Functions

Behavior: Returns the highest number from a list. The maximum function may be abbreviated as max. 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.

Syntax:

{the} max{imum} of numList

max{imum}(numList)

Example:

put max(4, 6, 5, 7, 3) -- 7

Example:

if the maximum of [x,y,z] is z then

put "Z is the greatest!"

end if

Example:

put largestValue of [2,23,45,76] --76

Example:

put highestValue of [3,9,13,21,42] --42

Related: the minimum function

Median Function

Behavior: Returns the median (middle value) of its parameters, or the average of the two middle values. numList may either 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. If numList contains an odd number of numbers, the median is the middle value of the sorted list of numbers, otherwise it is the average of the two middle values.

Syntax:

{the} median of numList

median(numList)

Example:

put median(1, 8, 9, 12) -- 8

Example:

put the median of "2,7,8,10" -- 7.5

Related: the average function

Minimum, Min, LowestValue, SmallestValue Functions

Behavior: Returns the lowest number from a list. The minimum function may be abbreviated as min. 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.

Syntax:

{the} min{imum} of numList

min{imum}(numList)

Example:

put min(4,6,5,7,3) -- 3

Example:

if the min of [x,y,z] is z then put "Z is the smallest!"

Example:

put lowestValue of [3,9,13,21,42] --3

Example:

put smallestValue of [9,13,21,56,72] --9

Related: the maximum function

Random Function

Behavior: Returns a randomly generated integer between 1 and the value of its parameter, or between two values.

Syntax:

{the} random of numFactor

random(numExpr {, secondExpr} )

Example:

put random(12) -- returns any number from 1 to 12

Example:

put random(20,30) -- gets a number from 20 to 30, inclusive

Example:

put (random(100) / 100) into randomPercentage

Related: Reset Random Command

Reset Random Command

Behavior: Resets the random number generator sequence used by the random function and whenever SenseTalk selects things at random. By setting a specific seedExpr value for the random number generator, you can obtain a repeatable sequence of "random" events. This can be very useful for testing purposes. Use the reset random command without a seed value to get an unpredictable sequence.

Syntax:

reset random {{with | from} {seed} seedExpr}

Example:

reset random with seed 27

Example:

reset random

Example:

reset random 2

repeat 10 times

put 1..10 into NumberRange

put any item of NumberRange into ReplaceitemNumber

log ReplaceitemNumber // Using reset random [#] as shown in line 1 results in the same list every time you run the loop.
[#] can be any seed number.

// Commenting out "reset random 2" in line 1 and running the repeat loop multiple times results in a different list every time you run the loop.

end repeat

Round Function

Behavior: Returns the value of its parameter rounded to the nearest whole number. An optional second parameter may be supplied to specify the number of decimal places to round to. A negative number of places will round to the left of the decimal point.

Syntax:

{the} round of numFactor

round(numExpr, decimalPlaces)

Example:

put round(6.5) -- 7

Example:

put round(6.49) -- 6

Example:

put round(6.49 , 1) -- 6.5

Example:

put round(2389 , -2) -- 2400

Related:

RoundToNearest function

Behavior: Returns the value of its first parameter rounded to the nearest whole multiple of its second parameter.

Syntax:

roundToNearest(numExpr, nearestMultiple)

Example:

put roundToNearest(643,100) -- 600

Example:

put roundToNearest(643,25) -- 650

Related:

Sin Function

Behavior: Returns the trigonometric sine of its parameter, which is an angle expressed in radians.

Syntax:

{the} sin of numFactor

sin(numExpr)

Example:

put sin(18) -- -0.750987

Square Root , SqRt function

Behavior: Returns the square root of its parameter.

Syntax:

{the} square root of numFactor

{the} sqrt of numFactor)

sqrt(numExpr)

Example:

put sqrt(16) -- 4

Example:

put the square root of nine -- 3

Sum Function

Behavior: Returns the sum of its parameters. 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.

Syntax:

{the} sum of numList

sum(numList)

Example:

put sum("8,1", [10,11], 12) -- shows 42

Example:

if the sum of [x,y,z] is more than 100 then

put "The sum exceeds 100"

end if

Tan Function

Behavior: Returns the trigonometric tangent of its parameter, which is an angle expressed in radians.

Syntax:

{the} tan of numFactor

tan(numExpr)

Example:

put tan(18) -- -1.137314

Trunc Function

Behavior: Truncates a number, returning the integer part of its parameter, and discarding any fractional part. Use the frac function to get the fractional part of a value. The trunc and frac functions are defined such that trunc(x) + frac(x) is always equal to x.

Syntax:

{the} trunc of numFactor

trunc(numExpr)

Example:

put trunc(6.8) -- 6

Example:

put trunc(6.49) -- 6

Related: the frac function

This topic was last updated on August 19, 2021, at 03:30:51 PM.