Topic: Expressions
Online Help
Operators
The following operators are supported by the Calcpad language:
- Arithmetic:
- "!" - factorial;
- "^" - exponentiation;
- "/" - floating point division;
- "\" - integer division;
- "÷" - division bar;
- "%" - division remainder;
- "*" - multiplication;
- "-" - subtraction;
- "+" - addition;
- Relational (comparison):
- "≡" - equal to;
- "≠" - unequal to;
- "<" - less then;
- ">" - greater than;
- "≤" - less or equal;
- "≥" - greater or equal;
- "=" - 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(a; b) or conditional execution (look further in this manual). You can also simulate boolean algebra with arithmetic expressions. In this case, you can use "*" instead of logical "AND" and "+" for logical "OR". For example, "(2 < 3) + (2 < 1) = 1 + 0 = 1" (true) and (2 < 3)∙(2 < 1) = 1∙0 = 0 (false). Again, you must be careful. This is not true boolean algebra and some expressions may evaluate to other results than 0 and 1. It depends on you to properly compose the expression and interpret the results. Also, make sure to put brackets when and where they are required. Arithmetic operators are of higher precedence than relational.
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
;