2 Operators
The following operators are supported within math expressions:
Operator | Description |
---|---|
* | Multiplication. |
/ | Division. |
% | Modulus: Divides two values and returns the remainder. |
+ | Addition. |
- | Subtraction. |
^ | Either exponentiation (the default) or bitwise XOR. For exponentiation, the number in front of ^ is the base, the number after it is the power to raise it to. |
** | Exponentiation. (This is an alias for ^) |
< | Less than. |
> | Greater than. |
>= | Greater than or equal to. |
<= | Less than or equal to. |
= | Equals. |
== | Equals. (This is an alias for =) |
<> | Not equal to. |
!= | Not equal to. (This is an alias for <>) |
& | Either logical (the default) or bitwise conjunction (AND). |
&& | Logical conjunction (AND). |
| | Either Logical (the default) or bitwise alternative (OR). |
|| | Logical alternative (OR). |
( ) | Groups sub-expressions, overriding the order of operations. |
~ | Bitwise NOT. |
<< |
Bitwise left shift. |
>> |
Bitwise right shift. |
<<< |
Bitwise left rotate. (Only available if compiled as C++20.) |
>>> |
Bitwise right rotate. (Only available if compiled as C++20.) |
Define TE_BITWISE_OPERATORS
to enable bitwise behavior for &
, |
, and ^
.
For operators, the order of precedence is:
Operator | Description |
---|---|
( ) | Instructions in parentheses are executed first. (If TE_BRACKETS_AS_PARENS is defined, then [] are treated the same way.) |
+, -, ~ | Positive or negative sign for a value, and bitwise NOT. |
^ | Exponentiation. |
*, /, and % | Multiplication, division, and modulus. |
+ and - | Addition and subtraction. |
<< , >> , <<< , >>> |
Bitwise left and right shift and rotation. |
<, >, >=, <= | Relational comparisons. |
= and != | Equality comparisons. |
&& | Logical conjunction (AND). |
|| | Logical alternative (OR). |
& | Bitwise AND (if TE_BITWISE_OPERATORS is defined). |
^ | Bitwise XOR (if TE_BITWISE_OPERATORS is defined). |
| | Bitwise OR (if TE_BITWISE_OPERATORS is defined). |
Defining TE_FLOAT
will disable all bitwise functions and operators.
For example, the following:
5+5+5/2
Will yield 12.5
. 5/2
is executed first, then added to the other fives. However, by using parentheses:
(5+5+5)/2
You can override it so that the additions happen first (resulting in 15
), followed by the division (finally yielding 7.5
). Likewise, (2+5)^2
will yield 49
(7
squared), while 2+5^2
will yield 27
(5
squared, plus 2
).
Compatibility Note
The %
character acts as a modulus operator in TinyExpr++, which is different from most spreadsheet programs. In programs such as LibreOffice Calc and Excel, %
is used to convert a number to a percentage. For example, =20%
would yield 0.20
in Excel. In TinyExpr++, however, 20%
will result in a syntax error as it is expecting a binary (modulus) operation.