2017-04-26 19:16:38 +00:00
Arithmetic functions
2017-06-13 04:15:47 +00:00
====================
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
For all arithmetic functions, the result type is calculated as the smallest number type that the result fits in, if there is such a type. The minimum is taken simultaneously based on the number of bits, whether it is signed, and whether it floats. If there are not enough bits, the highest bit type is taken.
2017-04-03 19:49:50 +00:00
2017-06-13 04:15:47 +00:00
Example:
2017-04-03 19:49:50 +00:00
.. code-block :: sql
2017-06-13 04:15:47 +00:00
SELECT toTypeName(0), toTypeName(0 + 0), toTypeName(0 + 0 + 0), toTypeName(0 + 0 + 0 + 0)
.. code-block :: text
2017-04-03 19:49:50 +00:00
┌─toTypeName(0)─┬─toTypeName(plus(0, 0))─┬─toTypeName(plus(plus(0, 0), 0))─┬─toTypeName(plus(plus(plus(0, 0), 0), 0))─┐
│ UInt8 │ UInt16 │ UInt32 │ UInt64 │
└───────────────┴────────────────────────┴─────────────────────────────────┴──────────────────────────────────────────┘
2017-04-26 19:16:38 +00:00
Arithmetic functions work for any pair of types from UInt8, UInt16, UInt32, UInt64, Int8, Int16, Int32, Int64, Float32, or Float64.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
Overflow is produced the same way as in C++.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
plus(a, b), a + b operator
2017-04-03 19:49:50 +00:00
--------------------------
2017-04-26 19:16:38 +00:00
Calculates the sum of the numbers.
You can also add whole numbers with a date or date and time. In the case of a date, adding a whole number means adding the corresponding number of days. For a date with time, it means adding the corresponding number of seconds.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
minus(a, b), a - b operator
2017-04-03 19:49:50 +00:00
---------------------------
2017-04-26 19:16:38 +00:00
Calculates the difference. The result is always signed.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
You can also calculate whole numbers from a date or date with time. The idea is the same - see above for 'plus'.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
multiply(a, b), a * b operator
2017-04-03 19:49:50 +00:00
------------------------------
2017-04-26 19:16:38 +00:00
Calculates the product of the numbers.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
divide(a, b), a / b operator
2017-06-13 04:15:47 +00:00
----------------------------
2017-04-26 19:16:38 +00:00
Calculates the quotient of the numbers. The result type is always a floating-point type.
It is not integer division. For integer division, use the 'intDiv' function.
When dividing by zero you get 'inf', '-inf', or 'nan'.
2017-04-03 19:49:50 +00:00
intDiv(a, b)
------------
2017-04-26 19:16:38 +00:00
Calculates the quotient of the numbers. Divides into integers, rounding down (by the absolute value).
When dividing by zero or when dividing a minimal negative number by minus one, an exception is thrown.
2017-04-03 19:49:50 +00:00
intDivOrZero(a, b)
------------------
2017-04-26 19:16:38 +00:00
Differs from 'intDiv' in that it returns zero when dividing by zero or when dividing a minimal negative number by minus one.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
modulo(a, b), a % b operator
2017-04-03 19:49:50 +00:00
----------------------------
2017-04-26 19:16:38 +00:00
Calculates the remainder after division.
If arguments are floating-point numbers, they are pre-converted to integers by dropping the decimal portion. The remainder is taken in the same sense as in C++. Truncated division is used for negative numbers.
An exception is thrown when dividing by zero or when dividing a minimal negative number by minus one.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
negate(a), -a operator
2017-04-03 19:49:50 +00:00
----------------------
2017-04-26 19:16:38 +00:00
Calculates a number with the reverse sign. The result is always signed.
2017-04-03 19:49:50 +00:00
abs(a)
------
2017-04-26 19:16:38 +00:00
Calculates the absolute value of the number 'a'. That is, if a< 0, it returns -a.
For unsigned types, it doesn't do anything. For signed integer types, it returns an unsigned number.