Topic: Expressions

Online Help


Operators


The following operators are supported by the Calcpad language:

  • Arithmetic:
    • "!" - factorial;
    • "^" - exponentiation;
    • "/" - floating point division;
    • "\" - integer division;
    • "÷" - division bar;
    • "" - modulo (%%, reminder);
    • "*" - multiplication;
    • "-" - subtraction;
    • "+" - addition;
  • Relational (comparison):
    • "" - equal to;
    • "" - unequal to;
    • "<" - less then;
    • ">" - greater than;
    • "" - less or equal;
    • "" - greater or equal;
  • Logical:
    • "" - logical "AND";
    • "" - logical "OR";
    • "" - logical “XOR”;
  • "=" - assignment.

Operator precedence and associativity

The above operators are listed in the order of their precedence. This is the order they will be evaluated in an expression. When you have different types of operators in a single expression, exponentiation will be evaluated first, then division and multiplication, subtraction and addition and comparison will be the last. All relational operators are of equal precedence. If you need to change the order of evaluation, you can use brackets. For example, "5 + 2∙3" makes "11". If the addition have to be first, write "(5 + 2)∙3". You will get "7∙3 = 21". Operators with equal precedence are evaluated from left to right. This is called operator associativity. For example, "3 - 2 + 1" makes "(3 - 2) + 1 = 2" and not "3 - (2 + 1) = 0". Another good example is "2∙3 / 2∙3", which makes "9" and not "1".

All operators in Calcpad are left-associative (calculations are performed from left to right). The only exception is exponentiation, which is right-associative. It is performed from right to left, which means that x^a^b will be evaluated as xab. However, many hand calculators and spreadsheet software like Excel use left associativity for exponentiation. In this case x^a^b will be evaluated as xa·b. If you need to have xab, you will have to add brackets: x^(a^b).

Relational and boolean expressions

Relational operators can return only two values: "1" for "true" and "0" for "false". You can use them in expressions along with arithmetic operators. For example, you can get the greater of two numbers a and b by the expression: "a*(a ≥ b) + b*(a < b)". But you need to be careful. If you use "" instead of "<", for the case of a equal to b, you will get a + b, which may be not exactly what you want. For that special purpose, it is better to use the built-in function max(ab), logical operators or conditional execution (look further in this manual). Arithmetic operators are of higher precedence than relational, and both are evaluated before logical ones.

Logical expressions

Calcpad operates only with numerical data, and does not have special types for boolean data. Similar to relational ones, logical operators also uses "1" for "true" and "0" for "false". Any input value, different than 0 is also assumed to be "true". You can build logical expressions by using logical operators and/or logical functions (see further in this manual). They work as follows:

  • "" (and) returns 1 if both operands are 1 and 0 otherwise;
  • "" (or) returns 1 if any of the operands is 1 and 0 if both are 0;
  • "" (xor) returns 1 if just one of the operands is 1 and 0 otherwise.

The results from the above operators are presented in the following tables:

And

x y x∧y
0 0 0
0 1 0
1 0 0
1 1 1

Or

x y x∨y
0 0 0
0 1 1
1 0 1
1 1 1

Xor

x y x⊕y
0 0 0
0 1 1
1 0 1
1 1 0

Complex arithmetic

All operators support complex numbers except for factorial "!", integer division "\", reminder "" and comparison: "<", "", ">", "". The evaluation of a complex expression is a little bit more difficult than real. The rules for the basic complex operations are given bellow:

  • Addition: (a + bi) + (c + di) = (a + c) + (b + d)i;
  • Subtraction: (a + bi) − (c + di) = (a − c) + (b − d)i;
  • Multiplication: (a + bi)·(c + di) = (ac − bd) + (bc + ad)i;
  • Division: (a + bi)/(c + di) = (ac + bd)/(c2 + d2) + (bc − ad)/(c2 + d2)i;