For all arithmetic functions, the result Type is the Min Type suitable for the result (if such a Type exists). The Min Type is determined according to the number of Digits of the Numerical value, whether it is signed, and whether it is a floating-point Type. If there are not enough Digits, then the highest Digits Type is adopted.
For example:
SELECT toTypeName(0), toTypeName(0 + 0), toTypeName(0 + 0 + 0), toTypeName(0 + 0 + 0 + 0)
┌─toTypeName(0)─┬─toTypeName(plus(0, 0))─┬─toTypeName(plus(plus(0, 0), 0))─┬─toTypeName(plus(plus(plus(0, 0), 0), 0))─┐ │ UInt8 │ UInt16 │ UInt32 │ UInt64 │ └───────────────┴────────────────────────┴─────────────────────────────────┴──────────────────────────────────────────┘
Arithmetic functions apply to any Type in UInt8, UInt16, UInt32, UInt64, Int8, Int16, Int32, Int64, Float32, or Float64.
The method of generating overflow is the same as in C++.
Calculate the Sum of Numerical values. You can also add Date or DateTime and Integer. In the case of Date, adding an Integer means adding the corresponding number of days. For DateTime, this means adding the corresponding number of seconds.
Syntax
select plus(a, b) -- a + b operator
Param
a – Number.b – Number.Return value
Example
select plus(1,2);
Result
┌─plus(1, 2)─┐ │ 3 │ └────────────┘
Calculate the Poor between Numerical values. The result is always signed. You can also subtract Integer from Date or DateTime. See 'plus' above.
Syntax
minus(a, b) -- a-b operator
Param
a – Number.b – Number.Return value
Example
SELECT minus(10, 3);
Result
┌─minus(10, 3)─┐ │ 7 │ └──────────────┘
Calculate the product of Numerical values.
Syntax
multiply(a, b) -- a * b operator
Param
a – Number.b – Number.Return value
Example
SELECT multiply(3,12);
Result
┌─multiply(3, 12)─┐ │ 36 │ └─────────────────┘
Calculate the quotient of Numerical values. The result Type is always a floating-point Type. This is not integer division. For integer division, use the 'intDiv' function. When dividing by Zero, you get 'inf', '-inf', or 'nan'.
Syntax
divide(a, b) -- a / b operator
Param
a – Number.b – Number.Return value
Example
SELECT divide(50, 2);
Result
┌─divide(50, 2)─┐ │ 2.5e+01 │ └───────────────┘
Calculate the quotient of Numerical values, rounding down (by Absolute value). An Abnormal is thrown when dividing by Zero or dividing the minimum negative Number by -1.
Syntax
intDiv(a, b)
Param
a – Number.b – Number.Return value
Example
SELECT intDiv(10, 2);
Result
┌─intDiv(10, 2)─┐ │ 5 │ └───────────────┘
The difference from 'intDiv' is that it returns Zero when dividing by Zero or dividing the minimum negative Number by -1.
Syntax
intDivOrZero(a, b)
Param
a – Number.b – Number.Return value
Example
SELECT intDivOrZero(10, -2);
Result
┌─intDivOrZero(10, -2)─┐ │ -5 │ └──────────────────────┘
Calculate the remainder after division. If both inputs are Integer, the result Type is Integer. If one of the inputs is a Floating-point number, the result is a Floating-point number. The rest is the same as in C++. Truncating division is used for negative numbers. An exception is thrown when dividing by zero or dividing the minimum negative number by -1.
Syntax
modulo(a, b) --a % b operator
Parameters
a – Number.b – Number.Return value
Example
SELECT modulo(10, 3);
Result
┌─modulo(10, 3)─┐ │ 1 │ └───────────────┘
The difference from modulo is that when divided by zero, the result returns 0.
Syntax
moduloOrZero(a, b) --a % b operator
Parameters
a – Number.b – Number.Return value
Example
SELECT moduloOrZero(10, 0);
Result
┌─moduloOrZero(10, 0)─┐ │ 0 │ └───────────────┘
Negates the value by changing its sign bit; the result is always signed.
Syntax
negate(a) -- -a operator
Parameters
x – Number.Return value
Example
SELECT negate(20);
Result
┌─negate(20)─┐ │ -20 │ └────────────┘
Calculates the absolute value of the numerical value (a). That is, if a < 0, it returns -a. For unsigned types, it does nothing. For signed integer types, it returns an unsigned number.
Syntax
abs(x)
Parameters
x – Number.Return value
Example
SELECT abs(-2);
Result
┌─abs(-2)─┐ │ 2 │ └─────────┘
Returns the greatest common divisor of the numerical value. An exception is thrown when dividing by zero or dividing the minimum negative number by -1.
Syntax
gcd(a, b)
Parameters
a – Number.b – Number.Return value
Example
SELECT gcd(27,18);
Result
┌─gcd(27, 18)─┐ │ 9 │ └─────────────┘
Returns the least common multiple of the numerical value. An exception is thrown when dividing by zero or dividing the minimum negative number by -1.
Syntax
lcm(a, b)
Parameters
a – Number.b – Number.Return value
Example
SELECT lcm(27,18);
Result
┌─lcm(27, 18)─┐ │ 54 │ └─────────────┘
Compares two values and returns the maximum value. The return value is converted to Float64.
Syntax
max2(value1, value2)
Parameters
value1 — The first value, Type: Int/UInt or Float.value2 — The second value, Type: Int/UInt or Float.Return value
Type: Float.
Example
Query statement:
SELECT max2(-1, 2);
Result:
┌─max2(-1, 2)─┐ │ 2 │ └─────────────┘
Compares two values and returns the minimum value. The return value is converted to Float64.
Syntax
min2(value1, value2)
Parameters
value1 — The first value, Type: Int/UInt or Float.value2 — The second value, Type: Int/UInt or Float.Return value
Type: Float.
Example
Query statement:
SELECT min2(-1, 2);
Result:
┌─min2(-1, 2)─┐ │ -1 │ └─────────────┘