Arithmetic operators (+, -, *, ...)
Arithmetic operators create properties whose value is the result of an arithmetic operation. They primarily operate on values of number classes. In addition, the sum and difference also operate on date/time values (described below), and the sum also concatenates strings (see String operators). The platform currently supports the following arithmetic operators:
| Operator | Name | Description | Example | Result |
|---|---|---|---|---|
+ | Summation | Takes two input operands and returns their sum | 3 + 5 | 8 |
- | Difference | Accepts two input operands and returns their difference This operator also has a unary form, in which case the first operand is considered equal to 0 | 5 - 3 | 2 |
* | Multiplication | Accepts two input operands and returns their product | 3 * 5 | 15 |
/ | Ratio | Takes two input operands and returns their ratio | 15 / 3 | 5 |
All of these operators return NULL if one of the operands is NULL . Division by zero also returns NULL. It is also possible to use a special form of summation and difference operators with brackets, in which case NULL will be equivalent to 0. The reverse is also true for these type of operators: if the result of an operator in such form is 0, then NULL is returned (e. g., 5 (-) 5 = NULL):
| Operator | Name | Description | Example | Result |
|---|---|---|---|---|
(+) | Summation | Takes two input operands and returns their sum, treating NULL as 0 | 3 (+) 53 (+) NULL | 83 |
(-) | Difference | Takes two input operands and returns their difference, treating NULL as 0 | 5 (-) 35 (-) NULL5 (-) 5 | 25NULL |
The remainder of division, integer division, and exponentiation have no operators of their own — they are performed by properties of the system module Utils:
| Operation | Property | Description | Example | Result |
|---|---|---|---|---|
| Remainder of division | mod[…, …] | Takes two input operands and returns the remainder of dividing the first by the second; the sign of the result matches the sign of the dividend. With a zero divisor it does not return NULL but fails with a DBMS error | mod(-7, 2) | -1 |
| Integer division | divideInteger[…, …] | Casts both operands to INTEGER (non-integer values are rounded) and returns their ratio — integer division, truncating the fractional part | divideInteger(7, 2) | 3 |
| Exponentiation | power[…, …] | Takes two input operands and returns the first raised to the power of the second; the result is DOUBLE | power(2, 3) | 8 |
Determining the result class
The result class is determined as:
| Operator | Result |
|---|---|
+, - | Common ancestor ("Numbers" family) |
* | NUMERIC[p1.IntegerPart + p1.Precision + p2.IntegerPart + p2.Precision, p1.Precision + p2.Precision] |
/ | NUMERIC[p1.IntegerPart + p2.Precision + s, s] |
The NUMERIC[ , ] formulas for the product and the ratio apply only when at least one operand belongs to NUMERIC[ , ]; otherwise the result is the common ancestor ("Numbers" family) of the two operand classes, so the product or ratio of two integers is itself an integer — the ratio being integer (truncating) division. In the ratio formula s is the maximum NUMERIC scale (32 by default).
The sum and the difference also operate on date/time values, where a whole number is counted in base units — days for DATE, seconds for DATETIME, ZDATETIME, and TIME:
| Operands | Result |
|---|---|
date/time value + / - a whole number | the same date/time class |
DATE - DATE | INTEGER (number of days) |
DATETIME / ZDATETIME / TIME - a value of the same class | LONG (number of base units) |
If either operand of the + operator belongs to a string class, the result is a string class; its length, case-insensitivity, and other parameters follow the string-concatenation rules — see string operators.
Language
Description of arithmetic operators.
Examples
sum(a, b) = a + b;
transform(a, b, c) = -a * (b (+) c);
remainder(a, b) = mod(a, b); // remainder of division — a property of the Utils module