Mathematical Operators
SenseTalk provides a number of mathematical operators. They are outlined below.
On this page:
- +, Plus Operators
- -, Minus Operators
- *, Times, Multiplied By Operators
- /, Divided By Operators
- ^ To The Power Of, Squared, Cubed Operators
- %, Percent Operators
- Div Operator
- Rem Operator
- Modulo, Mod Operators
- Is a Multiple Of, Is Divisible By Operators
- Rounded Top, Rounded to Nearest Operators
- But At Least, But No Less Than, But At Most, But No More Than Operators
- BitNot, BitAnd, BitOr, BitXOr, BitShift Operators
- Bit Expression, Bit Function
+ , Plus Operators
Behavior: Adds two numbers or lists of numbers.
Syntax:
operand1 + operand2
operand1 plus operand2
Syntax definitions for language elements follow these formatting guidelines:
- boldface: Indicates words and characters that must be typed exactly
- italic: Indicates expressions or other variable elements
- {} (curly braces): Indicate optional elements.
- [] (square brackets) separated by | (vertical pipes): Indicate alternative options where one or the other can be used, but not both.
Example syntax:
In this example, "open file" is required and must be typed exactly. "fileName" is a variable element; it is the path to and name of the file being opened. The following expression is optional and indicates why the file is being opened. If this expression is added, "for" is required and must be typed exactly. One of the following must be included, but only one, and they also must be typed exactly: "reading", "writing", "readwrite", "appending", or "updating".
Example:
put 12 + 97 into someSum
Example:
put a squared plus b squared into sumOfSquares
Example:
put [12,8] + [4,7] into vectorSum
- , Minus Operators
Behavior: Subtracts one number or list of numbers from another. Subtracting one date/time from another will give the difference as a time interval, measured in seconds.
Syntax:
operand1 - operand2
operand1 minus operand2
Syntax definitions for language elements follow these formatting guidelines:
- boldface: Indicates words and characters that must be typed exactly
- italic: Indicates expressions or other variable elements
- {} (curly braces): Indicate optional elements.
- [] (square brackets) separated by | (vertical pipes): Indicate alternative options where one or the other can be used, but not both.
Example syntax:
In this example, "open file" is required and must be typed exactly. "fileName" is a variable element; it is the path to and name of the file being opened. The following expression is optional and indicates why the file is being opened. If this expression is added, "for" is required and must be typed exactly. One of the following must be included, but only one, and they also must be typed exactly: "reading", "writing", "readwrite", "appending", or "updating".
Example:
put c^2 - sumOfSquares into difference
Example:
put (1,3,5,6) - (1,1,0,2) into diffList
* , Times, Multiplied By Operators
Behavior: Multiplies two numbers or lists, or multiplies a list by a scalar. When used with two lists of equal length, the result will be a series of products of the corresponding elements of the two lists. When one operand is a list and the other is a single (scalar) value, the result is a list of values obtained by multiplying each original list element by the scalar value.
Syntax:
operand1 * operand2
operand1 times operand2
operand1multiplied byoperand2
Syntax definitions for language elements follow these formatting guidelines:
- boldface: Indicates words and characters that must be typed exactly
- italic: Indicates expressions or other variable elements
- {} (curly braces): Indicate optional elements.
- [] (square brackets) separated by | (vertical pipes): Indicate alternative options where one or the other can be used, but not both.
Example syntax:
In this example, "open file" is required and must be typed exactly. "fileName" is a variable element; it is the path to and name of the file being opened. The following expression is optional and indicates why the file is being opened. If this expression is added, "for" is required and must be typed exactly. One of the following must be included, but only one, and they also must be typed exactly: "reading", "writing", "readwrite", "appending", or "updating".
Example:
put 2 * radius into diameter
Example:
put pi times diameter into circumference
Example:
put [1,2,3,4] * [2,2,1,3]-- result is [2,4,3,12]
Example:
put [1,2,3,4] * 4 -- result is [4,8,12,16]
Example:
put 7 multiplied by 3 -- result is 21
/ , Divided By Operators
Behavior: Divides one number or list by another, or divides a list by a scalar. The result is a quotient that may not be a whole number. Compare this to the div operator, which yields a whole number. When used with two lists of equal length, the result will be a series of quotients of the corresponding elements of the two lists. When the first operand is a list and the second is a single (scalar) value, the result is a list of values obtained by dividing each list element by the scalar value. If the second operand is zero, this operator will return the value infinity, displayed as Inf. Using an infinite value in other calculations will generally give expected results.
Syntax:
operand1 / operand2
operand1 divided by operand2
Syntax definitions for language elements follow these formatting guidelines:
- boldface: Indicates words and characters that must be typed exactly
- italic: Indicates expressions or other variable elements
- {} (curly braces): Indicate optional elements.
- [] (square brackets) separated by | (vertical pipes): Indicate alternative options where one or the other can be used, but not both.
Example syntax:
In this example, "open file" is required and must be typed exactly. "fileName" is a variable element; it is the path to and name of the file being opened. The following expression is optional and indicates why the file is being opened. If this expression is added, "for" is required and must be typed exactly. One of the following must be included, but only one, and they also must be typed exactly: "reading", "writing", "readwrite", "appending", or "updating".
Example:
put pi / 2 into halfPi
Example:
put [1,2,3,4] / [2,1,1,2] -- result is [0.5,2,3,2]
Example:
put [2,4,5,8] / 2 -- Result is [1,2,2.5,4]
^ , To The Power Of , Squared , Cubed Operators
Behavior: Raises a number to a given power.
Syntax:
operand1 to the power of operand2
operand1 squared
operand1 cubed
Syntax definitions for language elements follow these formatting guidelines:
- boldface: Indicates words and characters that must be typed exactly
- italic: Indicates expressions or other variable elements
- {} (curly braces): Indicate optional elements.
- [] (square brackets) separated by | (vertical pipes): Indicate alternative options where one or the other can be used, but not both.
Example syntax:
In this example, "open file" is required and must be typed exactly. "fileName" is a variable element; it is the path to and name of the file being opened. The following expression is optional and indicates why the file is being opened. If this expression is added, "for" is required and must be typed exactly. One of the following must be included, but only one, and they also must be typed exactly: "reading", "writing", "readwrite", "appending", or "updating".
Example:
put a squared + b squared into sumOfSquares
Example:
put 6 * x^4 - 2 * y^3 into z
% , Percent Operator
Behavior: Treats a number as a percentage, or computes add-on or discount percentages. In its simple form, % following a value divides that value by 100 (so 6% is the same as .06). However, if % is used following a + or - operator, the corresponding percent of the value to the left of that operator will be increased or decreased by the specified percent.
Syntax:
factor %
factor percent
value [ + | - | plus | minus ] factor [ % | percent ]
Syntax definitions for language elements follow these formatting guidelines:
- boldface: Indicates words and characters that must be typed exactly
- italic: Indicates expressions or other variable elements
- {} (curly braces): Indicate optional elements.
- [] (square brackets) separated by | (vertical pipes): Indicate alternative options where one or the other can be used, but not both.
Example syntax:
In this example, "open file" is required and must be typed exactly. "fileName" is a variable element; it is the path to and name of the file being opened. The following expression is optional and indicates why the file is being opened. If this expression is added, "for" is required and must be typed exactly. One of the following must be included, but only one, and they also must be typed exactly: "reading", "writing", "readwrite", "appending", or "updating".
Example:
put 4% -- .04
Example:
put 50 * 4% -- 2
Example:
put 50 + 4% -- 52
Example:
put 50 - 4% -- 48
Example:
put sales plus ten percent into projectedSales
Div Operator
Behavior: Divides one number by another, giving the result as an integer. The companion rem operator can be used to find the remainder of such an operation. Division by zero will yield the result Inf.
Syntax:
Syntax definitions for language elements follow these formatting guidelines:
- boldface: Indicates words and characters that must be typed exactly
- italic: Indicates expressions or other variable elements
- {} (curly braces): Indicate optional elements.
- [] (square brackets) separated by | (vertical pipes): Indicate alternative options where one or the other can be used, but not both.
Example syntax:
In this example, "open file" is required and must be typed exactly. "fileName" is a variable element; it is the path to and name of the file being opened. The following expression is optional and indicates why the file is being opened. If this expression is added, "for" is required and must be typed exactly. One of the following must be included, but only one, and they also must be typed exactly: "reading", "writing", "readwrite", "appending", or "updating".
Example:
put cookieCount div numberOfPeople into cookiesForEach
Rem Operator
Behavior: Calculates the integer remainder of a division. This is the complement of the div operator. The result of the rem operator will always have the same sign as its first operand.
Syntax:
Syntax definitions for language elements follow these formatting guidelines:
- boldface: Indicates words and characters that must be typed exactly
- italic: Indicates expressions or other variable elements
- {} (curly braces): Indicate optional elements.
- [] (square brackets) separated by | (vertical pipes): Indicate alternative options where one or the other can be used, but not both.
Example syntax:
In this example, "open file" is required and must be typed exactly. "fileName" is a variable element; it is the path to and name of the file being opened. The following expression is optional and indicates why the file is being opened. If this expression is added, "for" is required and must be typed exactly. One of the following must be included, but only one, and they also must be typed exactly: "reading", "writing", "readwrite", "appending", or "updating".
Example:
put cookieCount rem numberOfPeople into extrasForMe
Modulo, Mod Operators
Behavior: Performs the mathematical modulo operation. Obtains the amount by which one number exceeds the next-lower even multiple of another. The modulo operator is different from the rem operator, which gives the remainder of an integer division. When both operands are positive integers, rem and modulo will yield the same results. Negative numbers and non-integer values are treated much differently by the two operators, however.
Syntax:
Syntax definitions for language elements follow these formatting guidelines:
- boldface: Indicates words and characters that must be typed exactly
- italic: Indicates expressions or other variable elements
- {} (curly braces): Indicate optional elements.
- [] (square brackets) separated by | (vertical pipes): Indicate alternative options where one or the other can be used, but not both.
Example syntax:
In this example, "open file" is required and must be typed exactly. "fileName" is a variable element; it is the path to and name of the file being opened. The following expression is optional and indicates why the file is being opened. If this expression is added, "for" is required and must be typed exactly. One of the following must be included, but only one, and they also must be typed exactly: "reading", "writing", "readwrite", "appending", or "updating".
Example:
put someValue mod modulusValue into extrasForMe
Is a Multiple Of, Is Divisible By Operators
Behavior: These operators check whether one number is an exact multiple of another. That is, if the result of dividing one by the other would result in a whole number with no remainder.
Syntax:
value is {not} {a | an} {exact | even} multiple of divisor
value is {not} {exactly | evenly} divisible by divisor
Syntax definitions for language elements follow these formatting guidelines:
- boldface: Indicates words and characters that must be typed exactly
- italic: Indicates expressions or other variable elements
- {} (curly braces): Indicate optional elements.
- [] (square brackets) separated by | (vertical pipes): Indicate alternative options where one or the other can be used, but not both.
Example syntax:
In this example, "open file" is required and must be typed exactly. "fileName" is a variable element; it is the path to and name of the file being opened. The following expression is optional and indicates why the file is being opened. If this expression is added, "for" is required and must be typed exactly. One of the following must be included, but only one, and they also must be typed exactly: "reading", "writing", "readwrite", "appending", or "updating".
Example:
put 2895 is a multiple of 5-- true
Example:
put 169 is divisible by 13 -- true
Example:
put 98.6 is an exact multiple of 3.14-- false
Example:
if cookieCount is evenly divisible by numberOfPeople then put "Hooray!"
Rounded To, Rounded to Nearest Operators
Behavior: Rounds a value to a number of decimal places, or to the nearest multiple of another value. Also rounds to a unit name or to a variable whose value is a unit name. These operators provide an alternate syntax for calling the round() and roundToNearest() functions (see Arithmetic Calculations).
Syntax:
value rounded {to} places {{decimal} places}
value rounded to {the} nearest {multiple of} nearestMultiple
value rounded to {the} nearest unit
Syntax definitions for language elements follow these formatting guidelines:
- boldface: Indicates words and characters that must be typed exactly
- italic: Indicates expressions or other variable elements
- {} (curly braces): Indicate optional elements.
- [] (square brackets) separated by | (vertical pipes): Indicate alternative options where one or the other can be used, but not both.
Example syntax:
In this example, "open file" is required and must be typed exactly. "fileName" is a variable element; it is the path to and name of the file being opened. The following expression is optional and indicates why the file is being opened. If this expression is added, "for" is required and must be typed exactly. One of the following must be included, but only one, and they also must be typed exactly: "reading", "writing", "readwrite", "appending", or "updating".
Example:
put 123.4567 rounded to 2 places-- 123.46
Example:
put 123.4567 rounded -1 decimal places-- 120
Example:
put 98.6 rounded to the nearest multiple of 3.14-- 97.34
Example:
put total rounded to nearest .25 into amountDue
Example:
put 12.345 meters rounded to the nearest foot — 41 feet
But at Least, But No Less Than, But At Most, But No More Than Operators
Behavior: Limits a number to a minimum or maximum value.
Syntax:
operand but at least value
operand but no less than value
operand but at most value
operand but no more than value
Syntax definitions for language elements follow these formatting guidelines:
- boldface: Indicates words and characters that must be typed exactly
- italic: Indicates expressions or other variable elements
- {} (curly braces): Indicate optional elements.
- [] (square brackets) separated by | (vertical pipes): Indicate alternative options where one or the other can be used, but not both.
Example syntax:
In this example, "open file" is required and must be typed exactly. "fileName" is a variable element; it is the path to and name of the file being opened. The following expression is optional and indicates why the file is being opened. If this expression is added, "for" is required and must be typed exactly. One of the following must be included, but only one, and they also must be typed exactly: "reading", "writing", "readwrite", "appending", or "updating".
Example:
set highScore to largest value in scores but at most 100
Example:
set lowScore to rawScore - 9 but no less than zero
Example:
set volume to myVolume but no more than 11
Example:
set roomCapacity to sizeLimit but at least 123
BitNot, BitAnd, BitOr, BitXOr, BitShift Operators
Behavior: Performs operations on the individual bits of a number. Operands are treated as integers composed of 64 bits or fewer, as set by the bitLength global property.
- BitNot: Reverses each of the bits of a number, with 0's becoming 1's and 1's becoming 0's.
- BitAnd: Compares each bit of operand1 to the corresponding bit of operand2 to generate a bit in the result. When both bits are 1's the result is 1, otherwise the result is 0.
- BitOr: Compares each bit of operand1 to the corresponding bit of operand2 to generate a bit in the result.
- When both bits are 0's the result is 0.
- When either or both bits are 1's the result is 1.
- BitXOr: Compares each bit of operand1 to the corresponding bit of operand2 to generate a bit in the result.
- When both bits are 0's or both bits are 1's, the result is 0.
- When exactly one of the bits is 1, the result is 1.
- BitShift operator: Shifts all the bits in a number by the specified number of bits to the right or left. If right or left is not specified:
- A positive number shifts bits to the right.
- A negative number shifts bits to the left.
Syntax:
bitNot {of} operand
operand1 bitAnd operand2
operand1 bitOr operand2
operand1 bitXOr operand2
operand bitShift {left | right} shiftAmount
Syntax definitions for language elements follow these formatting guidelines:
- boldface: Indicates words and characters that must be typed exactly
- italic: Indicates expressions or other variable elements
- {} (curly braces): Indicate optional elements.
- [] (square brackets) separated by | (vertical pipes): Indicate alternative options where one or the other can be used, but not both.
Example syntax:
In this example, "open file" is required and must be typed exactly. "fileName" is a variable element; it is the path to and name of the file being opened. The following expression is optional and indicates why the file is being opened. If this expression is added, "for" is required and must be typed exactly. One of the following must be included, but only one, and they also must be typed exactly: "reading", "writing", "readwrite", "appending", or "updating".
Example:
set the numberFormat to "binary8" // Display numbers in binary as eight digits
put bitNot 0b00011011 —> 0b11100100
put 0b00010101 bitAnd 0b00111100 —> 0b00010100
put 0b00010101 bitOr 0b00111100 —> 0b00111101
put 0b00010101 bitXOr 0b00111100 —> 0b00101001
put 0b00010101 bitShift left 2 —> 0b01010100
Bit Expression, Bit Function
Behavior: Gives access to or sets an individual bit within a number. The source value is treated as an integer composed of 64 bits or fewer, as set by the bitLength global property.
A bit expression is a container, which allows you to:
- Get the value of an individual bit within a number, either 0 or 1, or True or False.
- Set the value of an individual bit within a number to either 0 or 1, On or Off, Yes or No, True or False, when the sourceValue is a variable or other container.
You can also use a bit expression as a boolean, for example, as the conditional in an if statement.
Syntax: Use either bit expression, or bit function syntax (with parentheses).
bit bitNumber of sourceValue
bit(bitNumber) of sourceValue
sourceValue.bit(bitNumber)
sourceValue's bit(bitNumber)
Syntax definitions for language elements follow these formatting guidelines:
- boldface: Indicates words and characters that must be typed exactly
- italic: Indicates expressions or other variable elements
- {} (curly braces): Indicate optional elements.
- [] (square brackets) separated by | (vertical pipes): Indicate alternative options where one or the other can be used, but not both.
Example syntax:
In this example, "open file" is required and must be typed exactly. "fileName" is a variable element; it is the path to and name of the file being opened. The following expression is optional and indicates why the file is being opened. If this expression is added, "for" is required and must be typed exactly. One of the following must be included, but only one, and they also must be typed exactly: "reading", "writing", "readwrite", "appending", or "updating".
Examples:
put bit 3 of 0b100110 —> 1
if bit(1) of 543 then put "It's an odd number!" —> It's an odd number!
set flags to 0b100000
put flags.bit(2) —> 0
set flags.bit(2) to yes
turn on bit 3 of flags
put flags —> 0b100110