# 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:

addnumExprto{chunkof}container

**Example:**

`add amount to dollarsVariable`

**Example:**

`add 37 to item 2 of line 3 of scores`

**Example:**

`add speed * time to item 1 of distances`

**Example:**

`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:

subtractnumExprfrom{chunkof}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`

**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{chunkof}containerbynumExpr

**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

**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{chunkof}containerbynumExpr

**Example:**

`divide score by totalCount`

**Example:**

`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:

the abs ofnumFactor

abs(numExpr)

numFactoras a positive number

numFactoras a negative number

**Example:**

`put abs(-11) --> 11`

**Example:**

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

**Example:**

`put (3 - 16) as a positive number --> 13`

**Example:**

`put 500 as a negative number --> -500`

`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:**

`ATan`

Function

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

Syntax:

the atan ofnumFactor

atan(numExpr)

**Example:**

`put atan(19) --> 1.518213`

**Related:**

`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 ofnumList

average(numList)

**Example:**

`put average(8, 10, 12) --> 10`

**Example:**

`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 initialInvestment * compound(6.7%, 12) into currentValue`

**Related:**

`Cos`

Function

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

Syntax:

{the}cos ofnumFactor

cos(numExpr)

**Example:**

`put cos(18) --> 0.660317`

**Related:**

`Cube Root`

Function

**Behavior:** Computes the cube root of its parameter.

Syntax:

cube root(number)

{the}cube root ofnumber

**Example:**

`put cube root (27) --> 3`

**Example:**

`put the cube root of 8 --> 2`

**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:

{the}exp ofnumFactor

exp(numExpr)

**Example:**

`put exp(2) --> 7.389056`

**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:

{the}exp1 ofnumFactor

exp1(numExpr)

**Example:**

`put expl(2) --> 6.389056`

**Related:**

`Exp2`

function

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

Syntax:

{the}exp2 ofnumFactor

exp2(numExpr)

**Example:**

`put exp2(8) --> 256`

**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:

{the}frac ofnumFactor

frac(numExpr)

**Example:**

`put frac(81.236) --> .236`

**Related:**

`Ln`

Function

**Behavior:** Returns the natural logarithm of its parameter.

Syntax:

{the}ln ofnumFactor

ln(numExpr)

**Examples**

`put ln(2) --> 0.693147`

**Related:**

`Ln1`

Function

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

Syntax:

{the}ln1 ofnumFactor

ln1(numExpr)

**Example:**

`put ln1(2) --> 1.098612`

**Related:**

`Log2`

Function

**Behavior:** Returns the base 2 logarithm of its parameter.

Syntax:

{the}log2 ofnumFactor

log2(numExpr)

**Example:**

`put log2(256) --> 8`

**Related:**

`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|maximum|highest value|largest value]ofnumFactor

[max|maximum|highestValue|largestValue](numExpr)

**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 the highest value of [3,42,9,13,21] --> 42`

**Related:**

`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 ofnumList

median(numList)

**Example:**

`put median(1, 2, 8, 9, 12) --> 8`

**Example:**

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

**Related:**

`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|minimum|lowest value|smallest value]ofnumList

[min|minimum|lowestValue|smallestValue](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 smallest value of [9,13,21,56,72] --> 9`

**Related:**

`Payment`

Function

**Behavior:** Returns the payment amount of a loan, given a loan amount, interest rate, term, and frequency of payments.

Syntax:

{the} {daily|weekly|monthly|quarterly|yearly|annual}payment on{a} {loan of}principal{loan}at{a rate of}rate{interest}for{a term of}term{ {with} [ [payments|paid]frequency|frequencypayments] }

payment(loanProperties)

payment(principal,rate,term{,frequency})

If *loanProperties* is given as a property list, it should include:

**Principal**or**Amount**—*principal*amount of the loan (may be specified with a currency unit)**Rate**— interest*rate*of the loan (may be specified as percent per year, calendarQuarter, month, week, day — if per period is not specified, annual interest rate is assumed)**Term**— the*term*length of the loan (may be given as a duration of time or as a number of payments. If a number of payments is used (with no duration units), then frequency parameter is required)**Frequency**— the*frequency*of loan payments (optional if loan term is given as a duration) — may be one of these terms: monthly, months, calendarMonths, quarterly, calendarQuarters, annual, annually, yearly, years, calendarYears, decades, calendarDecades

If the *principal* is given in units of a particular currency, then the payment amount returned will have the same units. If *term* is specified with units of duration and *frequency* is omitted, then the term's units will be used as the frequency of payments.

**Example:**

`put payment(amount:$1000, rate: 6.5% per year, term:60 months) --> $19.566148`

put payment($10000,7.5%,60,"months") --> $200.379486

**Example:**

`put the monthly payment on a ten thousand dollar loan at 4.5% yearly for 10 years --> $103.638409`

put payment on a loan of $50000 at 7 percent interest for 60 months rounded to 2 places --> $990.06

**Related:**

`Random`

Function

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

Syntax:

{the}random ofnumFactor

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

**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

**Related:**

`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 ofnumFactor

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)

numExprrounded to{the}nearest{multiple of}nearestMultiple

**Example:**

`put roundToNearest(643,100) --> 600`

**Example:**

`put roundToNearest(643,25) --> 650`

**Example:**

`put 12.16 rounded to the nearest multiple of .25 --> 12.25`

**Related:**

`Sin`

Function

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

Syntax:

{the}sin ofnumFactor

sin(numExpr)

**Example:**

`put sin(18) --> -0.750987`

**Related:**

`Square Root`

, `SqRt`

function

**Behavior:** Returns the square root of its parameter.

Syntax:

{the} [square root|sqrt]ofnumFactorsqrt(numExpr)

**Example:**

`put sqrt(16) --> 4`

**Example:**

`put the square root of nine --> 3`

**Related:**

`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 ofnumList

sum(numList)

**Example:**

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

**Example:**

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

put "The sum exceeds 100"

end if

**Related:**

`Tan`

Function

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

Syntax:

{the}tan ofnumFactor

tan(numExpr)

**Example:**

`put tan(18) --> -1.137314`

**Related:**

`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 ofnumFactor

trunc(numExpr)

**Example:**

`put trunc(6.8) --> 6`

**Example:**

`put trunc(6.49) --> 6`

**Related:**