Mathematical Operators
SenseTalk provides a number of mathematical operators. They are outlined below.
+
, Plus
Operators
Behavior: Adds two numbers or lists of numbers.
Syntax:
operand1 + operand2
operand1 plus operand2
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
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
operand1 multiplied by operand2
Example:
put 2 * radius into diameter
Example:
put pi times diameter into circumference
Example:
put [1,2,3,4] * [2,2,1,3] --> [2,4,3,12]
Example:
put [1,2,3,4] * 4 --> [4,8,12,16]
Example:
put 7 multiplied by 3 --> 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
Example:
put pi / 2 into halfPi
Example:
put [1,2,3,4] / [2,1,1,2] --> [0.5,2,3,2]
Example:
put [2,4,5,8] / 2 --> [1,2,2.5,4]
^
, To The Power Of
, Squared
, Cubed
Operators
Behavior: Raises a number to a given power.
Syntax:
operand1 ^ operand2
operand1 to the power of operand2
operand1 squared
operand1 cubed
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 ]
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:
operand1 div operand2
Example:
put cookieCount div numberOfPeople into cookiesForEach