Operators

An operator executes a mathematical operation with two values. Examples are addition and multiplication. SpeQ uses the following operators for calculations.

Arithmetic Operators

The arithmetic operators are the most basic mathematical operators. They include the operations add, subtract, multiply, divide, raise to a power, square, and cube. To insert a square character (²), you can press <Ctrl+2>, and to insert a cube character (³) you can press <Ctrl+3>.
Operator Description Example
+
Plus, addition 2 + 3
       Ans = 5
-
Minus, subtraction 7.5 - 3
       Ans = 4.5
*
Multiplication 8 * 2.5
       Ans = 20
/
Division 5 / 4
       Ans = 1.25
%
Mod
Modulus,
the remainder of an integer division
7 % 3
       Ans = 1
^
Power 2 ^ 3
        Ans = 8
²
^2
Square
        Ans = 16
³
^3
Cube
        Ans = 8
Note that the square and cube characters do only work on computers using a Western character set, they don't work in languages like Chinese or Russian. You can turn off usage of these characters, see Settings Window.

Back to top

Bitwise Operators

Bitwise operators takes two bit patterns of equal length, and produces another one of the same length by matching up corresponding bits (the first of each; the second of each; and so on) and performing the logical operation And, Or or Xor on each pair of corresponding bits.
The operations bitshift left (a << b) and bitshift right (a >> b) do shift the (binary) bits of the number a over a number of positions given by b. The value of b must be a positive integer, and a must be an integer.
Operator
Description
Example
&
Bitwise And 11 & 6
       Ans = 2
|
Bitwise Or 1 | 2 | 4
       Ans = 7
||
Bitwise Exclusive Or 1 || 2
       Ans = 3
<<
Bitshift left
a << b = a * (2^b)

16 << 2
       Ans = 64
>>
Bitshift right
a >> b = a / (2^b)
16 >> 2
       Ans = 4
Back to top

Conditional Operators

Conditional operators compare two values. The result will be 1 (True) if the statement is true, or 0 (False) if untrue.
Operator
Description
Example
=
Is equal to (5-4 = 1)
        Ans = 1
<>
Is not equal to (5-4 <> 1)
        Ans = 0
<
Is smaller than (2 < 3)
        Ans = 1
>
Is bigger than (2 > 3)
        Ans = 0
<=
Is smaller than or equal to (2 <= 3-1)
        Ans = 1
>=
Is bigger than or equal to (2 >= 3)
        Ans = 0
It's advisable to enclose a condition in parentheses. This makes your expressions more readable and prevents mistakes when defining variables. For example, entering 2=3 will result in the error message Error: Illegal variablename "2", because you're trying to define a variable with the name "2".

Back to top

Logical Operators

Logical operators will return 1 (True) if the statement is true, or 0 if (False).
Operator
Description
Example
And
And
Returns 1 if both statements are true.
(4-5 = 1) And (2<>3)
       Ans = 1
Or
Or
Returns 1 if one or both statements are true.
(4-5 = 2) Or (2<>3)
       Ans = 1
Xor
Exclusive Or
Returns 1 only if one of the statements is true.
(4-5 = 2) Xor (2<>3)
       Ans = 1


Back to top

Precedence

Precedence of math operators from highest to lowest:
( )
! ² ³
^
/, *, %
+, -
=, <>, <, >, <=, >=
And, Or, Xor, &, |, ||, >>, <<
Back to top

Examples

'Examples of using operators
2 * 3
       Ans = 6

3 / 4
       Ans = 0.75

2.5 + 3
       Ans = 5.5

2.5 - 3
       Ans = -0.5

2 * 3 + 4
       Ans = 10

3 ^ 2
       Ans = 9

1.5 * 2 ^ 2
       Ans = 6

2.13 + 3.99 + 1.25 - 0.50 + 2.00
       Ans = 8.87

'Examples of using logical and conditional operators

'Create a few variables to experiment with
a = 3.5;
b = 8.0;
c = 5.7;

'Test conditions of the variables
(a < b)
       Ans = 1
(a = b) Ans = 0
(b >= a) And (b >= c) Ans = 1
(c >= b) And (c >= a) Ans = 0
(c >= b) Or (c >= a) Ans = 1
Back to top

See Also

Constants, Expressions, Functions Overview, Precedence, System functions