Formulas 3. Operators

In formulas you can use the numerical and logical operators that are described on this page. The order of evaluation of the operators is the order that is most usual in programming languages. To force a different order, you use parentheses.

The operators with the highest precedence are negation (-) and exponentation (^):

--6 → 6
2^6 → 64

Sequences of negation and exponentiation are evaluated from right to left:

2^-6 → 0.015625
-(1+1)^6 → -64
4^3^2 → 4^9 → 262144

Note that changing the spacing does not change the meaning:

4^3 ^ 2 → 262144

To change the order of evaluation, you have to use parentheses:

(4 ^ 3) ^ 2 → 4096

The following construction is not allowed because of an ambiguity between a negative number and negation of a positive number:

-2^6 → ?

Instead, you use any of the following:

(-2)^6 → 64
-(2^6) → -64
-(2)^6 → -64

The operators with the next highest precedence are multiplication (*) and division (/). They are evaluated from left to right:

1/4*5 → 1.25 (from left to right)
1 / 4*5 → 1.25 (spacing does not help)
1 / (4*5) → 0.05 (use parentheses to change the order)
3 * 2 ^ 4 → 48 (exponentiation before multiplication)
3*2 ^ 4 → 48 (this spacing does not matter and is misleading)
(3 * 2) ^ 4 → 1296 (use parentheses to change the order)

Integer division operators (div and mod) have the same precedence as * and /, and are likewise evaluated from left to right:

54 div 5 → 10 (division rounded down)
54 mod 5 → 4 (the remainder)
54.3 div 5.1 → 10 (works for real numbers as well)
54.3 mod 5.1 → 3.3 (the remainder)
-54 div 5 → -11 (division rounded down; negation before division)
-54 mod 5 → 1 (the remainder)
-(54 div 5) → -10 (use parentheses to change the order)
-(54 mod 5) → -4
3 * 18 div 5 → 10 (from left to right)
3 * (18 div 5) → 9
3 * 18 mod 5 → 4
3 * (18 mod 5) → 9
54 div 5 * 3 → 30 (from left to right)
54 div (5 * 3) → 3
54 mod 5 * 3 → 12
54 mod (5 * 3) → 9

The operators with the next highest precedence are addition (+) and subtraction (-), evaluated from left to right:

3 - 8 + 7 → 2 (from left to right)
3 - (8 + 7) → -12 (use parentheses to change the order)
3 + 8 * 7 → 59 (multiplication before addition)
(3 + 8) * 7 → 77 (use parentheses to change the order)
3 + - (2 ^ 4) → -13 (exponentiation, negation, addition)
3 + 5 / 2 + 3 → 8.5
(3 + 5) / (2 + 3) → 1.6

The operators with the next highest precedence are the comparison operators (=, <>, <, >, <=, >=). These operators always yield 0 (false) or 1 (true):

5 + 6 = 10 → 0 (equal)
5 + 6 = 11 → 1
5 + 6 <> 10 → 1 (unequal)
5 + 6 <> 11 → 0
5 + 6 < 10 → 0 (less than)
5 + 6 < 11 → 0
5 + 6 > 10 → 1 (greater than)
5 + 6 > 11 → 0
5 + 6 <= 10 → 0 (less than or equal)
5 + 6 <= 11 → 1
5 + 6 >= 10 → 1 (greater or equal)
5 + 6 >= 11 → 1

The comparison operators are mainly used in if, while, and until conditions.

The operators of lowest precedence are the logical operators (not, and, and or), of which not has the highest precedence and or the lowest:

not 5 + 6 = 10 → 1
x > 5 and x < 10 (is x between 5 and 10?)
not x <= 5 and not x >= 10 (means the same as the previous line)
not (x <= 5 or x >= 10) (means the same as the previous line, unless x is undefined)

String comparison

a\$ = b\$
gives the value true (= 1) if the strings are equal, and false (= 0) otherwise.
a\$ <> b\$
gives the value true if the strings are unequal, and false otherwise.
a\$ < b\$
gives true if the string a\$ precedes the string b\$ in Unicode sorting order. Thus, "ha" < "hal" and "ha" < "ja" are true, but "ha" < "JA" is false, because all capitals precede all lower-case characters in the Unicode sorting order.
a\$ > b\$
true if a\$ comes after b\$ in Unicode sorting order.
a\$ <= b\$
gives the value true if the string a\$ precedes the string b\$ in Unicode sorting order, or if the strings are equal.
a\$ >= b\$
true if a\$ comes after b\$ or the two are equal.

String concatenation and truncation

a\$ + b\$
concatenates the two strings. After
text\$ = "hallo" + "dag"
The variable text\$ contains the string "hallodag".
a\$ - b\$
subtracts the second string from the end of the first. After
soundFileName\$ = "hallo.aifc"
textgridFileName\$ = soundFileName\$ - ".aifc" + ".TextGrid"
the variable textgridFileName\$ contains the string "hallo.TextGrid". If the first string a\$ does not end in the string b\$, the result of the subtraction is the string a\$.