2020-04-03 13:23:32 +00:00
---
2022-04-09 13:29:05 +00:00
sidebar_position: 34
sidebar_label: Arithmetic
2020-04-03 13:23:32 +00:00
---
2022-06-02 10:55:18 +00:00
# Arithmetic Functions
2017-12-28 15:13:23 +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.
Example:
2020-03-20 10:10:48 +00:00
``` sql
2017-12-28 15:13:23 +00:00
SELECT toTypeName(0), toTypeName(0 + 0), toTypeName(0 + 0 + 0), toTypeName(0 + 0 + 0 + 0)
```
2020-03-20 10:10:48 +00:00
``` text
2017-12-28 15:13:23 +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 │
└───────────────┴────────────────────────┴─────────────────────────────────┴──────────────────────────────────────────┘
```
Arithmetic functions work for any pair of types from UInt8, UInt16, UInt32, UInt64, Int8, Int16, Int32, Int64, Float32, or Float64.
Overflow is produced the same way as in C++.
2022-06-02 10:55:18 +00:00
## plus(a, b), a + b operator
2017-12-28 15:13:23 +00:00
Calculates the sum of the numbers.
You can also add integer numbers with a date or date and time. In the case of a date, adding an integer means adding the corresponding number of days. For a date with time, it means adding the corresponding number of seconds.
2022-06-02 10:55:18 +00:00
## minus(a, b), a - b operator
2017-12-28 15:13:23 +00:00
Calculates the difference. The result is always signed.
2020-03-20 10:10:48 +00:00
You can also calculate integer numbers from a date or date with time. The idea is the same – see above for ‘ plus’ .
2017-12-28 15:13:23 +00:00
2022-06-02 10:55:18 +00:00
## multiply(a, b), a \* b operator
2017-12-28 15:13:23 +00:00
Calculates the product of the numbers.
2022-06-02 10:55:18 +00:00
## divide(a, b), a / b operator
2017-12-28 15:13:23 +00:00
Calculates the quotient of the numbers. The result type is always a floating-point type.
2020-03-20 10:10:48 +00:00
It is not integer division. For integer division, use the ‘ intDiv’ function.
When dividing by zero you get ‘ inf’ , ‘ -inf’ , or ‘ nan’ .
2017-12-28 15:13:23 +00:00
2022-06-02 10:55:18 +00:00
## intDiv(a, b)
2017-12-28 15:13:23 +00:00
Calculates the quotient of the numbers. Divides into integers, rounding down (by the absolute value).
An exception is thrown when dividing by zero or when dividing a minimal negative number by minus one.
2022-06-02 10:55:18 +00:00
## intDivOrZero(a, b)
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +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-12-28 15:13:23 +00:00
2022-06-02 10:55:18 +00:00
## modulo(a, b), a % b operator
2017-12-28 15:13:23 +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.
2022-06-02 10:55:18 +00:00
## moduloOrZero(a, b)
2020-02-25 10:15:24 +00:00
2020-05-19 06:02:30 +00:00
Differs from [modulo ](#modulo ) in that it returns zero when the divisor is zero.
2020-02-25 10:15:24 +00:00
2022-06-02 10:55:18 +00:00
## negate(a), -a operator
2017-12-28 15:13:23 +00:00
Calculates a number with the reverse sign. The result is always signed.
2022-06-02 10:55:18 +00:00
## abs(a)
2017-12-28 15:13:23 +00:00
2021-05-27 19:44:11 +00:00
Calculates the absolute value of the number (a). That is, if a \< 0, it returns -a. For unsigned types it does not do anything. For signed integer types, it returns an unsigned number.
2017-12-28 15:13:23 +00:00
2022-06-02 10:55:18 +00:00
## gcd(a, b)
2017-12-28 15:13:23 +00:00
Returns the greatest common divisor of the numbers.
An exception is thrown when dividing by zero or when dividing a minimal negative number by minus one.
2022-06-02 10:55:18 +00:00
## lcm(a, b)
2017-12-28 15:13:23 +00:00
Returns the least common multiple of the numbers.
An exception is thrown when dividing by zero or when dividing a minimal negative number by minus one.
2022-06-02 10:55:18 +00:00
## max2
2021-10-19 22:42:05 +00:00
Compares two values and returns the maximum. The returned value is converted to [Float64 ](../../sql-reference/data-types/float.md ).
**Syntax**
```sql
max2(value1, value2)
```
**Arguments**
- `value1` — First value. [Int/UInt ](../../sql-reference/data-types/int-uint.md ) or [Float ](../../sql-reference/data-types/float.md ).
- `value2` — Second value. [Int/UInt ](../../sql-reference/data-types/int-uint.md ) or [Float ](../../sql-reference/data-types/float.md ).
**Returned value**
2021-10-19 23:00:54 +00:00
- The maximum of two values.
2021-10-19 22:42:05 +00:00
Type: [Float ](../../sql-reference/data-types/float.md ).
**Example**
Query:
```sql
SELECT max2(-1, 2);
```
Result:
```text
┌─max2(-1, 2)─┐
│ 2 │
└─────────────┘
```
2022-06-02 10:55:18 +00:00
## min2
2021-10-19 22:42:05 +00:00
Compares two values and returns the minimum. The returned value is converted to [Float64 ](../../sql-reference/data-types/float.md ).
**Syntax**
```sql
max2(value1, value2)
```
**Arguments**
- `value1` — First value. [Int/UInt ](../../sql-reference/data-types/int-uint.md ) or [Float ](../../sql-reference/data-types/float.md ).
- `value2` — Second value. [Int/UInt ](../../sql-reference/data-types/int-uint.md ) or [Float ](../../sql-reference/data-types/float.md ).
**Returned value**
2021-10-19 23:00:54 +00:00
- The minimum of two values.
2021-10-19 22:42:05 +00:00
Type: [Float ](../../sql-reference/data-types/float.md ).
**Example**
Query:
```sql
2021-10-22 14:56:10 +00:00
SELECT min2(-1, 2);
2021-10-19 22:42:05 +00:00
```
Result:
```text
2021-10-22 14:56:10 +00:00
┌─min2(-1, 2)─┐
│ -1 │
2021-10-19 22:42:05 +00:00
└─────────────┘
```