Arithmetic functions

```
┌─divide(toDecimal64(-12, 1), toDecimal32(2.1, 1))─┐
│                                             -5.7 │
└──────────────────────────────────────────────────┘
┌───a─┬───b─┬─divideDecimal(toDecimal64(-12, 1), toDecimal32(2.1, 1), 1)─┬─divideDecimal(toDecimal64(-12, 1), toDecimal32(2.1, 1), 5)─┐
│ -12 │ 2.1 │                                                       -5.7 │                                                   -5.71428 │
└─────┴─────┴────────────────────────────────────────────────────────────┴────────────────────────────────────────────────────────────┘
```

## divideOrNull[​](#divideOrNull "Direct link to divideOrNull")

Introduced in: v25.5.0

Same as `divide` but returns NULL when dividing by zero.

**Syntax**

```
divideOrNull(x, y)
```

**Arguments**

- `x` — Dividend - `y` — Divisor

**Returned value**

The quotient of x and y, or NULL.

**Examples**

**Dividing by zero**

```
SELECT divideOrNull(25, 0)
```

```
\N
```

## gcd[​](#gcd "Direct link to gcd")

Introduced in: v1.1.0

Returns the greatest common divisor of two values a and b.

An exception is thrown when dividing by zero or when dividing a minimal
negative number by minus one.

**Syntax**

```
gcd(x, y)
```

**Arguments**

- `x` — First integer - `y` — Second integer

**Returned value**

The greatest common divisor of `x` and `y`.

**Examples**

**Usage example**

```
SELECT gcd(12, 18)
```

```
6
```

## ifNotFinite[​](#ifNotFinite "Direct link to ifNotFinite")

Introduced in: v20.3.0

Checks whether a floating point value is finite.

You can get a similar result by using the [ternary operator](/docs/sql-reference/functions/conditional-functions#if): `isFinite(x) ? x : y`.

**Syntax**

```
ifNotFinite(x,y)
```

**Arguments**

- `x` — Value to check if infinite. [`Float*`](/docs/sql-reference/data-types/float)
- `y` — Fallback value. [`Float*`](/docs/sql-reference/data-types/float)

**Returned value**

- `x` if `x` is finite.
- `y` if `x` is not finite.

**Examples**

**Usage example**

```
SELECT 1/0 AS infimum, ifNotFinite(infimum,42)
```

```
inf  42
```

## intDiv[​](#intDiv "Direct link to intDiv")

Introduced in: v1.1.0

Performs an integer division of two values `x` by `y`. In other words it
computes the quotient rounded down to the next smallest integer.

The result has the same width as the dividend (the first parameter).

An exception is thrown when dividing by zero, when the quotient does not fit
in the range of the dividend, or when dividing a minimal negative number by minus one.

**Syntax**

```
intDiv(x, y)
```

**Arguments**

- `x` — Left hand operand. - `y` — Right hand operand.

**Returned value**

Result of integer division of `x` and `y`

**Examples**

**Integer division of two floats**

```
SELECT intDiv(toFloat64(1), 0.001) AS res, toTypeName(res)
```

```
┌──res─┬─toTypeName(intDiv(toFloat64(1), 0.001))─┐
│ 1000 │ Int64                                   │
└──────┴─────────────────────────────────────────┘
```

**Quotient does not fit in the range of the dividend**

```
SELECT
intDiv(1, 0.001) AS res,
toTypeName(res)
```

```
Received exception from server (version 23.2.1):
Code: 153. DB::Exception: Received from localhost:9000. DB::Exception:
Cannot perform integer division, because it will produce infinite or too
large number: While processing intDiv(1, 0.001) AS res, toTypeName(res).
(ILLEGAL_DIVISION)
```

## intDivOrNull[​](#intDivOrNull "Direct link to intDivOrNull")

Introduced in: v25.5.0

Same as `intDiv` but returns NULL when dividing by zero or when dividing a
minimal negative number by minus one.

**Syntax**

```
intDivOrNull(x, y)
```

**Arguments**

- `x` — Left hand operand. [`(U)Int*`](/docs/sql-reference/data-types/int-uint)
- `y` — Right hand operand. [`(U)Int*`](/docs/sql-reference/data-types/int-uint)

**Returned value**

Result of integer division of `x` and `y`, or NULL.

**Examples**

**Integer division by zero**

```
SELECT intDivOrNull(1, 0)
```

```
\N
```

**Dividing a minimal negative number by minus 1**

```
SELECT intDivOrNull(-9223372036854775808, -1)
```

```
\N
```

## intDivOrZero[​](#intDivOrZero "Direct link to intDivOrZero")

Introduced in: v1.1.0

Same as `intDiv` but returns zero when dividing by zero or when dividing a
minimal negative number by minus one.

**Syntax**

```
intDivOrZero(a, b)
```

**Arguments**

- `a` — Left hand operand. [`(U)Int*`](/docs/sql-reference/data-types/int-uint)
- `b` — Right hand operand. [`(U)Int*`](/docs/sql-reference/data-types/int-uint)

**Returned value**

Result of integer division of a and b, or zero.

**Examples**

**Integer division by zero**

```
SELECT intDivOrZero(1, 0)
```

```
0
```

**Dividing a minimal negative number by minus 1**

```
SELECT intDivOrZero(0.05, -1)
```

```
0
```

## isFinite[​](#isFinite "Direct link to isFinite")

Introduced in: v1.1.0

Returns `1` if the Float32 or Float64 argument not infinite and not a `NaN`,
otherwise this function returns `0`.

**Syntax**

```
isFinite(x)
```

**Arguments**

- `x` — Number to check for finiteness. [`Float*`](/docs/sql-reference/data-types/float)

**Returned value**

`1` if x is not infinite and not `NaN`, otherwise `0`.

**Examples**

**Test if a number is finite**

```
SELECT isFinite(inf)
```

```
0
```

## isInfinite[​](#isInfinite "Direct link to isInfinite")

Introduced in: v1.1.0

Returns `1` if the Float32 or Float64 argument is infinite, otherwise this function returns `0`.
Note that `0` is returned for a `NaN`.

**Syntax**

```
isInfinite(x)
```

**Arguments**

- `x` — Number to check for infiniteness. [`Float*`](/docs/sql-reference/data-types/float)

**Returned value**

`1` if x is infinite, otherwise `0` (including for `NaN`).

**Examples**

**Test if a number is infinite**

```
SELECT isInfinite(inf), isInfinite(NaN), isInfinite(10))
```

```
1 0 0
```

## isNaN[​](#isNaN "Direct link to isNaN")

Introduced in: v1.1.0

Returns `1` if the Float32 and Float64 argument is `NaN`, otherwise returns `0`.

**Syntax**

```
isNaN(x)
```

**Arguments**

- `x` — Argument to evaluate for if it is `NaN`. [`Float*`](/docs/sql-reference/data-types/float)

**Returned value**

`1` if `NaN`, otherwise `0`

**Examples**

**Usage example**

```
SELECT isNaN(NaN)
```

```
1
```

## lcm[​](#lcm "Direct link to lcm")

Introduced in: v1.1.0

Returns the least common multiple of two values `x` and `y`.

An exception is thrown when dividing by zero or when dividing a minimal negative number by minus one.

**Syntax**

```
lcm(x, y)
```

**Arguments**

- `x` — First integer. [`(U)Int*`](/docs/sql-reference/data-types/int-uint)
- `y` — Second integer. [`(U)Int*`](/docs/sql-reference/data-types/int-uint)

**Returned value**

Returns the least common multiple of `x` and `y`. [`(U)Int*`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT lcm(6, 8)
```

```
24
```

## max2[​](#max2 "Direct link to max2")

Introduced in: v21.11.0

Returns the bigger of two numeric values `x` and `y`.

**Syntax**

```
max2(x, y)
```

**Arguments**

- `x` — First value [`(U)Int8/16/32/64`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float) or [`Decimal`](/docs/sql-reference/data-types/decimal)
- `y` — Second value [`(U)Int8/16/32/64`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float) or [`Decimal`](/docs/sql-reference/data-types/decimal)

**Returned value**

Returns the bigger value of `x` and `y`. [`Float64`](/docs/sql-reference/data-types/float)

**Examples**

**Usage example**

```
SELECT max2(-1, 2)
```

```
2
```

## midpoint[​](#midpoint "Direct link to midpoint")

Introduced in: v25.11.0

Computes and returns the average value of the provided arguments.
Supports numerical and temporal types.

**Syntax**

```
midpoint(x1[, x2, ...])
```

**Arguments**

- `x1[, x2, ...]` — Accepts a single value or multiple values for averaging.

**Returned value**

Returns the average value of the provided arguments, promoted to the largest compatible type.

**Examples**

**Numeric types**