Arithmetic operators
seekdb supports commonly used arithmetic operators.
Overview of arithmetic operators
The following table describes the arithmetic operators supported in the current version of seekdb.
| Operator | Operand Type | Description |
|---|---|---|
+ | Unary/Binary | Unary indicates a positive number, and binary indicates addition. |
- | Unary/Binary | Unary indicates a negative number, and binary indicates subtraction. |
* | Binary | Multiplication. |
/ | Binary | Division. |
DIV | Binary | Integer division, which returns the quotient. |
MOD or % | Binary | Integer division, which returns the remainder. N % M or N MOD M returns the remainder after N is divided by M. |
Syntax
Rules for arithmetic operations
The following rules apply to integer division:
-
The quotient is rounded to the nearest integer toward zero.
-
The sign of the remainder is the same as the sign of the dividend.
Here is an example:
SELECT (-7) DIV (3.6), (-7) MOD (3.6);
+----------------+----------------+
| (-7) DIV (3.6) | (-7) MOD (3.6) |
+----------------+----------------+
| -1 | -3.4 |
+----------------+----------------+
1 row in set (0.001 sec)
SELECT (-7) DIV (-3.4), (-7) % (-3.4);
+-----------------+---------------+
| (-7) DIV (-3.4) | (-7) % (-3.4) |
+-----------------+---------------+
| 2 | -0.2 |
+-----------------+---------------+
1 row in set (0.001 sec)
Results of arithmetic operations
The result of an arithmetic operation is determined based on the following rules:
-
For the operators
-,+, and*, if both operands are integers, the result is calculated withBIGINT(64-bit) precision. -
If both operands are integers and one of them is an unsigned integer, the result is an unsigned integer. However, if the
NO_UNSIGNED_SUBTRACTIONSQL mode is enabled, the result of a subtraction operation is signed even if one of the operands is an unsigned integer. -
If the operands of
+,-,/,*, or%are real numbers or string values, the result has the precision of the operand with the highest precision. -
When you use the
/operator to divide two exact-value operands, the number of decimal places in the result is the number of decimal places in the first operand plus the value of thediv_precision_incrementsystem variable (default value is 4). For example, the expression 5.15/0.013 has six decimal places (396.153846).