> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-8c05c8a2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> 산술 함수 문서

# 산술 함수

<div id="overview">
  ## 개요
</div>

산술 함수는 `UInt8`, `UInt16`, `UInt32`, `UInt64`, `Int8`, `Int16`, `Int32`, `Int64`, `Float32` 또는 `Float64` 타입의 임의의 두 피연산자에 대해 동작합니다.

연산을 수행하기 전에 두 피연산자는 결과 타입으로 변환됩니다. 결과 타입은 다음과 같이 결정됩니다(아래 함수 문서에서 별도로
지정하지 않는 한).

* 두 피연산자가 모두 32비트 이하이면 결과 타입의 크기는 두 피연산자 중 더 큰 타입보다 한 단계 큰 타입의 크기가 됩니다
  (정수 크기 승격). 예를 들어 `UInt8 + UInt16 = UInt32` 또는 `Float32 * Float32 = Float64`입니다.
* 피연산자 중 하나가 64비트 이상이면 결과 타입의 크기는 두 피연산자 중 더 큰 타입과 동일한 크기가 됩니다. 예를
  들어 `UInt32 + UInt128 = UInt128` 또는 `Float32 * Float64 = Float64`입니다.
* 피연산자 중 하나가 부호 있는 타입이면 결과 타입도 부호 있는 타입이 되고, 그렇지 않으면 부호 없는 타입이 됩니다. 예를 들어 `UInt32 * Int32 = Int64` 또는 `UInt32 * UInt32 = UInt64`입니다.

이 규칙은 결과 타입이 가능한 모든 결과를 표현할 수 있는 가장 작은 타입이 되도록 합니다. 이로 인해 값 범위 경계 부근에서는
오버플로우 위험이 생길 수 있지만, 최대 64비트 네이티브 정수 폭을 사용해 계산을 빠르게 수행할 수 있습니다. 또한 이러한 동작은
64비트 정수(BIGINT)를 가장 큰 정수 타입으로 제공하는 다른 많은 데이터베이스와의 호환성도 보장합니다.

예시:

```sql theme={null}
SELECT toTypeName(0), toTypeName(0 + 0), toTypeName(0 + 0 + 0), toTypeName(0 + 0 + 0 + 0)
```

```text theme={null}
┌─toTypeName(0)─┬─toTypeName(plus(0, 0))─┬─toTypeName(plus(plus(0, 0), 0))─┬─toTypeName(plus(plus(plus(0, 0), 0), 0))─┐
│ UInt8         │ UInt16                 │ UInt32                          │ UInt64                                   │
└───────────────┴────────────────────────┴─────────────────────────────────┴──────────────────────────────────────────┘
```

오버플로우는 C++와 같은 방식으로 발생합니다.

{/*AUTOGENERATED_START*/}

<div id="abs">
  ## abs
</div>

도입 버전: v1.1.0

`x`의 절대값을 계산합니다. `x`가 부호 없는 유형이면 값에 변화가 없습니다. `x`가 부호 있는 유형이면 부호 없는 숫자를 반환합니다.

**구문**

```sql theme={null}
abs(x)
```

**인수**

* `x` — 절댓값을 구할 대상 값

**반환 값**

`x`의 절댓값

**예시**

**사용 예시**

```sql title=Query theme={null}
SELECT abs(-0.5)
```

```response title=Response theme={null}
0.5
```

<div id="avg2">
  ## avg2
</div>

도입 버전: v25.11.0

전달된 인수의 평균값을 계산해 반환합니다.
숫자 및 시간 관련 타입을 지원합니다.

**구문**

```sql theme={null}
avg2(x1, x2])
```

**인수**

* `x1, x2]` — 평균 계산에 사용할 두 값을 받습니다.

**반환 값**

제공된 인수의 평균값을 반환합니다. 결과는 호환 가능한 가장 큰 유형으로 승격됩니다.

**예시**

**숫자 타입**

```sql title=Query theme={null}
SELECT avg2(toUInt8(3), 1.0) AS result, toTypeName(result) AS type;
-- UInt8이 비교를 위해 64비트로 승격되어야 하므로 반환되는 유형은 Float64입니다.
```

```response title=Response theme={null}
┌─result─┬─type────┐
│      2 │ Float64 │
└────────┴─────────┘
```

**Decimal 타입**

```sql title=Query theme={null}
SELECT avg2(toDecimal32(1, 2), 2) AS result, toTypeName(result) AS type;
```

```response title=Response theme={null}
┌─result─┬─type──────────┐
│    1.5 │ Decimal(9, 2) │
└────────┴───────────────┘
```

**날짜 타입**

```sql title=Query theme={null}
SELECT avg2(toDate('2025-01-01'), toDate('2025-01-05')) AS result, toTypeName(result) AS type;
```

```response title=Response theme={null}
┌─────result─┬─type─┐
│ 2025-01-03 │ Date │
└────────────┴──────┘
```

**DateTime 타입**

```sql title=Query theme={null}
SELECT avg2(toDateTime('2025-01-01 00:00:00'), toDateTime('2025-01-03 12:00:00')) AS result, toTypeName(result) AS type;
```

```response title=Response theme={null}
┌──────────────result─┬─type─────┐
│ 2025-01-02 06:00:00 │ DateTime │
└─────────────────────┴──────────┘
```

**Time64 타입**

```sql title=Query theme={null}
SELECT avg2(toTime64('12:00:00', 0), toTime64('14:00:00', 0)) AS result, toTypeName(result) AS type;
```

```response title=Response theme={null}
┌───result─┬─type──────┐
│ 13:00:00 │ Time64(0) │
└──────────┴───────────┘
```

<div id="byteSwap">
  ## byteSwap
</div>

도입 버전: v23.10.0

정수의 바이트 순서를 뒤집습니다. 즉, [엔디언 방식](https://en.wikipedia.org/wiki/Endianness)을 변경합니다.

아래 예시는 다음과 같은 방식으로 계산할 수 있습니다:

1. 10진수 정수를 big-endian 기준의 16진수 포맷으로 변환합니다. 즉, 3351772109 -> C7 C7 FB CD (4바이트)
2. 바이트 순서를 뒤집습니다. 즉, C7 C7 FB CD -> CD FB C7 C7
3. 결과를 big-endian으로 가정하고 다시 정수로 변환합니다. 즉, CD FB C7 C7 -> 3455829959
   이 함수의 한 가지 사용 예시는 IPv4의 바이트 순서를 뒤집는 것입니다:

```result theme={null}
┌─toIPv4(byteSwap(toUInt32(toIPv4('205.251.199.199'))))─┐
│ 199.199.251.205                                       │
└───────────────────────────────────────────────────────┘
```

**구문**

```sql theme={null}
byteSwap(x)
```

**인수**

* `x` — 정수형 값입니다. [`(U)Int*`](/ko/reference/data-types/int-uint)

**반환 값**

바이트 순서가 뒤바뀐 `x`를 반환합니다. [`(U)Int*`](/ko/reference/data-types/int-uint)

**예시**

**사용 예시**

```sql title=Query theme={null}
SELECT byteSwap(3351772109)
```

```response title=Response theme={null}
3455829959
```

**8비트**

```sql title=Query theme={null}
SELECT byteSwap(54)
```

```response title=Response theme={null}
54
```

**16비트**

```sql title=Query theme={null}
SELECT byteSwap(4135)
```

```response title=Response theme={null}
10000
```

**32비트**

```sql title=Query theme={null}
SELECT byteSwap(3351772109)
```

```response title=Response theme={null}
3455829959
```

**64비트**

```sql title=Query theme={null}
SELECT byteSwap(123294967295)
```

```response title=Response theme={null}
18439412204227788800
```

<div id="divide">
  ## divide
</div>

도입 버전: v1.1.0

두 값 `a`와 `b`의 몫을 계산합니다. 결과 타입은 항상 [Float64](/ko/reference/data-types/float)입니다.
정수 나눗셈에는 `intDiv` 함수를 사용합니다.

<Note>
  `0`으로 나누면 `inf`, `-inf` 또는 `nan`이 반환됩니다.
</Note>

**구문**

```sql theme={null}
divide(x, y)
```

**인수**

* `x` — 피제수 - `y` — 제수

**반환 값**

x와 y를 나눈 몫

**예시**

**두 수 나누기**

```sql title=Query theme={null}
SELECT divide(25,5) AS quotient, toTypeName(quotient)
```

```response title=Response theme={null}
5 Float64
```

**0으로 나누기**

```sql title=Query theme={null}
SELECT divide(25,0)
```

```response title=Response theme={null}
inf
```

<div id="divideDecimal">
  ## divideDecimal
</div>

도입 버전: v22.12.0

두 개의 Decimal 값에 대해 나눗셈을 수행합니다. 결과 값은 [Decimal256](/ko/reference/data-types/decimal) 타입입니다.
결과 스케일은 `result_scale` 인수(const Integer, 범위 `[0, 76]`)로 명시적으로 지정할 수 있습니다. 지정하지 않으면 결과 스케일은 주어진 인수 중 최댓값이 됩니다.

<Note>
  이 함수는 일반적인 `divide`보다 훨씬 느리게 동작합니다.
  정밀도를 제어할 필요가 없거나 빠른 계산이 필요하다면 [divide](#divide) 사용을 고려하십시오.
</Note>

**구문**

```sql theme={null}
divideDecimal(x, y[, result_scale])
```

**인수**

* `x` — 첫 번째 값: [Decimal](/ko/reference/data-types/decimal). - `y` — 두 번째 값: [Decimal](/ko/reference/data-types/decimal). - `result_scale` — 결과의 소수 자릿수입니다. 유형은 [Int/UInt](/ko/reference/data-types/int-uint)입니다.

**반환 값**

지정된 소수 자릿수가 적용된 나눗셈 결과입니다. [`Decimal256`](/ko/reference/data-types/decimal)

**예시**

**예시 1**

```sql title=Query theme={null}
divideDecimal(toDecimal256(-12, 0), toDecimal32(2.1, 1), 10)
```

```response title=Response theme={null}
┌─divideDecimal(toDecimal256(-12, 0), toDecimal32(2.1, 1), 10)─┐
│                                                -5.7142857142 │
└──────────────────────────────────────────────────────────────┘
```

**예시 2**

```sql title=Query theme={null}
SELECT toDecimal64(-12, 1) / toDecimal32(2.1, 1);
SELECT toDecimal64(-12, 1) as a, toDecimal32(2.1, 1) as b, divideDecimal(a, b, 1), divideDecimal(a, b, 5);
```

```response title=Response theme={null}
┌─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 │
└─────┴─────┴────────────────────────────────────────────────────────────┴────────────────────────────────────────────────────────────┘
```

<div id="divideOrNull">
  ## divideOrNull
</div>

도입 버전: v25.5.0

`divide`와 동일하지만 0으로 나누면 NULL을 반환합니다.

**구문**

```sql theme={null}
divideOrNull(x, y)
```

**인수**

* `x` — 피제수 - `y` — 제수

**반환 값**

x를 y로 나눈 몫 또는 NULL입니다.

**예시**

**0으로 나누기**

```sql title=Query theme={null}
SELECT divideOrNull(25, 0)
```

```response title=Response theme={null}
\N
```

<div id="gcd">
  ## gcd
</div>

도입 버전: v1.1.0

두 값 a와 b의 최대공약수를 반환합니다.

0으로 나누거나 가장 작은 음수를 -1로 나누면 예외가 발생합니다.

**구문**

```sql theme={null}
gcd(x, y)
```

**인수**

* `x` — 첫 번째 정수 - `y` — 두 번째 정수

**반환 값**

`x`와 `y`의 최대공약수입니다.

**예시**

**사용 예시**

```sql title=Query theme={null}
SELECT gcd(12, 18)
```

```response title=Response theme={null}
6
```

<div id="ifNotFinite">
  ## ifNotFinite
</div>

도입 버전: v20.3.0

부동소수점 값이 유한한지 검사합니다.

[삼항 연산자](/ko/reference/functions/regular-functions/conditional-functions#if)를 사용하면 유사한 결과를 얻을 수 있습니다: `isFinite(x) ? x : y`.

**구문**

```sql theme={null}
ifNotFinite(x,y)
```

**인수**

* `x` — 무한인지 확인할 값. [`Float*`](/ko/reference/data-types/float)
* `y` — 폴백 값. [`Float*`](/ko/reference/data-types/float)

**반환 값**

* `x`가 유한하면 `x`
* `x`가 유한하지 않으면 `y`

**예시**

**사용 예시**

```sql title=Query theme={null}
SELECT 1/0 AS infimum, ifNotFinite(infimum,42)
```

```response title=Response theme={null}
inf  42
```

<div id="intDiv">
  ## intDiv
</div>

도입 버전: v1.1.0

두 값 `x`를 `y`로 나누는 정수 나눗셈을 수행합니다. 즉,
몫을 내림하여 가장 가까운 작은 정수로 계산합니다.

결과는 피제수(첫 번째 매개변수)와 동일한 비트를 가집니다.

0으로 나누는 경우, 몫이 피제수의 표현 범위에 맞지 않는 경우,
또는 가장 작은 음수를 -1로 나누는 경우 예외가 발생합니다.

**구문**

```sql theme={null}
intDiv(x, y)
```

**인수**

* `x` — 왼쪽 피연산자. - `y` — 오른쪽 피연산자.

**반환 값**

`x`와 `y`를 정수 나눗셈한 결과

**예시**

**두 개의 float에 대한 정수 나눗셈**

```sql title=Query theme={null}
SELECT intDiv(toFloat64(1), 0.001) AS res, toTypeName(res)
```

```response title=Response theme={null}
┌──res─┬─toTypeName(intDiv(toFloat64(1), 0.001))─┐
│ 1000 │ Int64                                   │
└──────┴─────────────────────────────────────────┘
```

**몫이 피제수의 표현 범위를 벗어납니다**

```sql title=Query theme={null}
SELECT
intDiv(1, 0.001) AS res,
toTypeName(res)
```

```response title=Response theme={null}
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)
```

<div id="intDivOrNull">
  ## intDivOrNull
</div>

도입 버전: v25.5.0

`intDiv`와 동일하지만, 0으로 나누거나 가장 작은 음수를 -1로 나누는 경우에는 NULL을 반환합니다.

**구문**

```sql theme={null}
intDivOrNull(x, y)
```

**인수**

* `x` — 왼쪽 피연산자. [`(U)Int*`](/ko/reference/data-types/int-uint)
* `y` — 오른쪽 피연산자. [`(U)Int*`](/ko/reference/data-types/int-uint)

**반환 값**

`x`를 `y`로 정수 나눗셈한 결과 또는 NULL입니다.

**예시**

**0으로 나눌 때의 정수 나눗셈**

```sql title=Query theme={null}
SELECT intDivOrNull(1, 0)
```

```response title=Response theme={null}
\N
```

**최솟값인 음수를 -1로 나누기**

```sql title=Query theme={null}
SELECT intDivOrNull(-9223372036854775808, -1)
```

```response title=Response theme={null}
\N
```

<div id="intDivOrZero">
  ## intDivOrZero
</div>

도입 버전: v1.1.0

`intDiv`와 동일하지만, 0으로 나누거나 가장 작은 음수를 -1로 나눌 경우 0을 반환합니다.

**구문**

```sql theme={null}
intDivOrZero(a, b)
```

**인수**

* `a` — 왼쪽 피연산자. [`(U)Int*`](/ko/reference/data-types/int-uint)
* `b` — 오른쪽 피연산자. [`(U)Int*`](/ko/reference/data-types/int-uint)

**반환 값**

a와 b를 정수 나눗셈한 결과 또는 0입니다.

**예시**

**0으로 나누는 정수 나눗셈**

```sql title=Query theme={null}
SELECT intDivOrZero(1, 0)
```

```response title=Response theme={null}
0
```

**가장 작은 음수를 -1로 나누기**

```sql title=Query theme={null}
SELECT intDivOrZero(0.05, -1)
```

```response title=Response theme={null}
0
```

<div id="isFinite">
  ## isFinite
</div>

도입 버전: v1.1.0

Float32 또는 Float64, 또는 BFloat16 인수가 무한대가 아니고 `NaN`도 아니면 `1`을 반환하며,
그렇지 않으면 `0`을 반환합니다.

**구문**

```sql theme={null}
isFinite(x)
```

**인수**

* `x` — 유한한 값인지 확인할 숫자입니다. [`Float*`](/ko/reference/data-types/float) 또는 [`BFloat16`](/ko/reference/data-types/float)

**반환 값**

x가 무한대가 아니고 `NaN`도 아니면 `1`이고, 그렇지 않으면 `0`입니다.

**예시**

**숫자가 유한한지 확인**

```sql title=Query theme={null}
SELECT isFinite(inf)
```

```response title=Response theme={null}
0
```

<div id="isInfinite">
  ## isInfinite
</div>

도입 버전: v1.1.0

Float32 또는 Float64, 또는 BFloat16 인수가 무한대이면 `1`을 반환하고, 그렇지 않으면 `0`을 반환합니다.
`NaN`의 경우에는 `0`을 반환합니다.

**구문**

```sql theme={null}
isInfinite(x)
```

**인수**

* `x` — 무한 여부를 확인할 숫자입니다. [`Float*`](/ko/reference/data-types/float) 또는 [`BFloat16`](/ko/reference/data-types/float)

**반환 값**

`x`가 무한대이면 `1`, 그렇지 않으면 `0`입니다(`NaN`인 경우 포함).

**예시**

**숫자가 무한대인지 확인**

```sql title=Query theme={null}
SELECT isInfinite(inf), isInfinite(NaN), isInfinite(10))
```

```response title=Response theme={null}
1 0 0
```

<div id="isNaN">
  ## isNaN
</div>

도입 버전: v1.1.0

Float32 또는 Float64, 또는 BFloat16 인수가 `NaN`이면 `1`을, 그렇지 않으면 `0`을 반환합니다.

**구문**

```sql theme={null}
isNaN(x)
```

**인수**

* `x` — `NaN`인지 여부를 평가할 인수입니다. [`Float*`](/ko/reference/data-types/float) 또는 [`BFloat16`](/ko/reference/data-types/float)

**반환 값**

`NaN`이면 `1`, 그렇지 않으면 `0`입니다.

**예시**

**사용 예시**

```sql title=Query theme={null}
SELECT isNaN(NaN)
```

```response title=Response theme={null}
1
```

<div id="lcm">
  ## lcm
</div>

도입 버전: v1.1.0

두 값 `x`와 `y`의 최소공배수를 반환합니다.

0으로 나누거나, 표현 가능한 가장 작은 음수를 -1로 나누면 예외가 발생합니다.

**구문**

```sql theme={null}
lcm(x, y)
```

**인수**

* `x` — 첫 번째 정수입니다. [`(U)Int*`](/ko/reference/data-types/int-uint)
* `y` — 두 번째 정수입니다. [`(U)Int*`](/ko/reference/data-types/int-uint)

**반환 값**

`x`와 `y`의 최소공배수를 반환합니다. [`(U)Int*`](/ko/reference/data-types/int-uint)

**예시**

**사용 예시**

```sql title=Query theme={null}
SELECT lcm(6, 8)
```

```response title=Response theme={null}
24
```

<div id="max2">
  ## max2
</div>

도입 버전: v21.11.0

두 숫자 값 `x`와 `y` 중에서 더 큰 값을 반환합니다.

**구문**

```sql theme={null}
max2(x, y)
```

**인수**

* `x` — 첫 번째 값 [`(U)Int8/16/32/64`](/ko/reference/data-types/int-uint), [`Float*`](/ko/reference/data-types/float) 또는 [`Decimal`](/ko/reference/data-types/decimal)
* `y` — 두 번째 값 [`(U)Int8/16/32/64`](/ko/reference/data-types/int-uint), [`Float*`](/ko/reference/data-types/float) 또는 [`Decimal`](/ko/reference/data-types/decimal)

**반환 값**

`x`와 `y` 중 더 큰 값을 반환합니다. [`Float64`](/ko/reference/data-types/float)

**예시**

**사용 예시**

```sql title=Query theme={null}
SELECT max2(-1, 2)
```

```response title=Response theme={null}
2
```

<div id="midpoint">
  ## midpoint
</div>

도입 버전: v25.11.0

제공된 인수의 평균값을 계산해 반환합니다.
숫자 및 시간 타입을 지원합니다.

**구문**

```sql theme={null}
midpoint(x1[, x2, ...])
```

**인수**

* `x1[, x2, ...]` — 평균을 계산할 단일 값 또는 여러 값을 받습니다.

**반환 값**

주어진 인수의 평균값을 반환하며, 가장 큰 호환 타입으로 승격됩니다.

**예시**

**숫자 타입**

```sql title=Query theme={null}
SELECT midpoint(1, toUInt8(3), 0.5) AS result, toTypeName(result) AS type;
-- 반환 유형은 Float64입니다. UInt8이 비교를 위해 64비트로 승격되어야 하기 때문입니다.
```

```response title=Response theme={null}
┌─result─┬─type────┐
│    1.5 │ Float64 │
└────────┴─────────┘
```

**Decimal 타입**

```sql title=Query theme={null}
SELECT midpoint(toDecimal32(1.5, 2), toDecimal32(1, 1), 2) AS result, toTypeName(result) AS type;
```

```response title=Response theme={null}
┌─result─┬─type──────────┐
│    1.5 │ Decimal(9, 2) │
└────────┴───────────────┘
```

**날짜 타입**

```sql title=Query theme={null}
SELECT midpoint(toDate('2025-01-01'), toDate('2025-01-05')) AS result, toTypeName(result) AS type;
```

```response title=Response theme={null}
┌─────result─┬─type─┐
│ 2025-01-03 │ Date │
└────────────┴──────┘
```

**DateTime 타입**

```sql title=Query theme={null}
SELECT midpoint(toDateTime('2025-01-01 00:00:00'), toDateTime('2025-01-03 12:00:00')) AS result, toTypeName(result) AS type;
```

```response title=Response theme={null}
┌──────────────result─┬─type─────┐
│ 2025-01-02 06:00:00 │ DateTime │
└─────────────────────┴──────────┘
```

**Time64 타입**

```sql title=Query theme={null}
SELECT midpoint(toTime64('12:00:00', 0), toTime64('14:00:00', 0)) AS result, toTypeName(result) AS type;
```

```response title=Response theme={null}
┌───result─┬─type──────┐
│ 13:00:00 │ Time64(0) │
└──────────┴───────────┘
```

<div id="min2">
  ## min2
</div>

도입된 버전: v21.11.0

두 숫자 값 `x`와 `y` 중 더 작은 값을 반환합니다.

**구문**

```sql theme={null}
min2(x, y)
```

**인수**

* `x` — 첫 번째 값: [`(U)Int8/16/32/64`](/ko/reference/data-types/int-uint), [`Float*`](/ko/reference/data-types/float) 또는 [`Decimal`](/ko/reference/data-types/decimal)
* `y` — 두 번째 값: [`(U)Int8/16/32/64`](/ko/reference/data-types/int-uint), [`Float*`](/ko/reference/data-types/float) 또는 [`Decimal`](/ko/reference/data-types/decimal)

**반환 값**

`x`와 `y` 중 더 작은 값을 반환합니다. [`Float64`](/ko/reference/data-types/float)

**예시**

**사용 예시**

```sql title=Query theme={null}
SELECT min2(-1, 2)
```

```response title=Response theme={null}
-1
```

<div id="minus">
  ## minus
</div>

도입 버전: v1.1.0

두 값 `a`와 `b`의 차이를 계산합니다. 결과는 항상 부호 있는 값입니다.
plus와 마찬가지로, date 또는 date with time 값에서 정수를 뺄 수 있습니다.
또한 date with time 값 간의 뺄셈도 지원하며, 이 경우 두 값 사이의 시간 차이가 반환됩니다.

**구문**

```sql theme={null}
minus(x, y)
```

**인수**

* `x` — 피감수. - `y` — 감수.

**반환 값**

x에서 y를 뺀 값

**예시**

**두 수 빼기**

```sql title=Query theme={null}
SELECT minus(10, 5)
```

```response title=Response theme={null}
5
```

**정수와 날짜의 뺄셈**

```sql title=Query theme={null}
SELECT minus(toDate('2025-01-01'),5)
```

```response title=Response theme={null}
2024-12-27
```

<div id="modulo">
  ## modulo
</div>

도입 버전: v1.1.0

두 값 a를 b로 나눈 나머지를 계산합니다.

두 입력이 모두 정수이면 결과 유형은 정수입니다. 입력 중 하나가
부동소수점 수이면 결과 유형은 Float64입니다.

나머지는 C++와 같은 방식으로 계산됩니다. 음수는 버림 나눗셈으로
계산합니다.

0으로 나누거나 가장 작은 음수를 -1로 나누면
예외가 발생합니다.

**구문**

```sql theme={null}
modulo(a, b)
```

**별칭**: `mod`

**인수**

* `a` — 피제수 - `b` — 제수(모듈러스)

**반환 값**

a % b의 나머지입니다.

**예시**

**사용 예시**

```sql title=Query theme={null}
SELECT modulo(5, 2)
```

```response title=Response theme={null}
1
```

<div id="moduloLegacy">
  ## moduloLegacy
</div>

도입 버전: v1.1.0

나눗셈의 나머지를 계산합니다. 이 함수는 C++ `%` 연산자를 사용하는 레거시 modulo 구현이며, 인수가 음수이면 결과도 음수가 될 수 있습니다. 이 함수는 이전 테이블 파티셔닝 로직과의 하위 호환성을 위해 제공됩니다. 표준 동작이 필요하면 `modulo` 또는 `positiveModulo`를 사용하십시오.

**구문**

```sql theme={null}
moduloLegacy(a, b)
```

**인수**

* `a` — 피제수입니다. [`(U)Int*`](/ko/reference/data-types/int-uint) 또는 [`Float*`](/ko/reference/data-types/float)
* `b` — 나누는 수입니다. [`(U)Int*`](/ko/reference/data-types/int-uint) 또는 [`Float*`](/ko/reference/data-types/float)

**반환 값**

나눗셈의 나머지를 반환합니다. [`(U)Int*`](/ko/reference/data-types/int-uint) 또는 [`Float*`](/ko/reference/data-types/float)

**예시**

**기본 사용법**

```sql title=Query theme={null}
SELECT moduloLegacy(10, 3)
```

```response title=Response theme={null}
1
```

<div id="moduloOrNull">
  ## moduloOrNull
</div>

도입 버전: v25.5.0

`a`를 `b`로 나눌 때의 나머지를 계산합니다. 함수 `modulo`와 유사하지만, `moduloOrNull`은 오른쪽 인수가 0이면 NULL을 반환합니다.

**구문**

```sql theme={null}
moduloOrNull(x, y)
```

**별칭**: `modOrNull`

**인수**

* `x` — 피제수입니다. [`(U)Int*`](/ko/reference/data-types/int-uint) 또는 [`Float*`](/ko/reference/data-types/float)
* `y` — 제수(모듈러스)입니다. [`(U)Int*`](/ko/reference/data-types/int-uint) 또는 [`Float*`](/ko/reference/data-types/float)

**반환 값**

`x`를 `y`로 나눈 나머지를 반환합니다. 제수가 0이면 null을 반환합니다.

**예시**

**제수가 0일 때의 moduloOrNull**

```sql title=Query theme={null}
SELECT moduloOrNull(5, 0)
```

```response title=Response theme={null}
\N
```

<div id="moduloOrZero">
  ## moduloOrZero
</div>

도입 버전: v20.3.0

`modulo`와 유사하지만, 나누는 수가 0이면 `modulo` 함수처럼
예외가 발생하지 않고 0을 반환합니다.

**구문**

```sql theme={null}
moduloOrZero(a, b)
```

**인수**

* `a` — 피제수입니다. [`(U)Int*`](/ko/reference/data-types/int-uint) 또는 [`Float*`](/ko/reference/data-types/float)
* `b` — 제수(모듈러스)입니다. [`(U)Int*`](/ko/reference/data-types/int-uint) 또는 [`Float*`](/ko/reference/data-types/float)

**반환 값**

a % b의 나머지를 반환합니다. 제수가 `0`이면 `0`을 반환합니다.

**예시**

**사용 예시**

```sql title=Query theme={null}
SELECT moduloOrZero(5, 0)
```

```response title=Response theme={null}
0
```

<div id="multiply">
  ## multiply
</div>

도입 버전: v1.1.0

두 값 `x`와 `y`를 곱한 결과를 계산합니다.

**구문**

```sql theme={null}
multiply(x, y)
```

**인수**

* `x` — 곱할 값입니다. [`(U)Int*`](/ko/reference/data-types/int-uint) 또는 [`Float*`](/ko/reference/data-types/float) 또는 [`Decimal`](/ko/reference/data-types/decimal)
* `y` — 곱할 값입니다. [`(U)Int*`](/ko/reference/data-types/int-uint) 또는 [`Float*`](/ko/reference/data-types/float) 또는 [`Decimal`](/ko/reference/data-types/decimal)

**반환 값**

x와 y의 곱을 반환합니다.

**예시**

**두 수를 곱하기**

```sql title=Query theme={null}
SELECT multiply(5,5)
```

```response title=Response theme={null}
25
```

<div id="multiplyDecimal">
  ## multiplyDecimal
</div>

도입 버전: v22.12.0

두 개의 decimal 값에 대해 곱셈을 수행합니다. 결과 값의 유형은 [Decimal256](/ko/reference/data-types/decimal)입니다.
결과 스케일은 `result_scale` 인수(범위 `[0, 76]`의 상수 Integer)로 명시적으로 지정할 수 있습니다. 지정하지 않으면 결과 스케일은 주어진 인수 중 최대 스케일이 됩니다.

<Note>
  이 함수들은 일반적인 `multiply`보다 훨씬 느리게 동작합니다.
  정밀도를 제어할 필요가 없거나 빠른 계산이 필요하다면 [multiply](#multiply) 사용을 고려하십시오.
</Note>

**구문**

```sql theme={null}
multiplyDecimal(a, b[, result_scale])
```

**인수**

* `a` — 첫 번째 값입니다. [`Decimal`](/ko/reference/data-types/decimal)
* `b` — 두 번째 값입니다. [`Decimal`](/ko/reference/data-types/decimal)
* `result_scale` — 결과의 scale입니다. [`(U)Int*`](/ko/reference/data-types/int-uint)

**반환 값**

지정된 scale로 곱셈한 결과입니다. 유형: [`Decimal256`](/ko/reference/data-types/decimal)

**예시**

**사용 예시**

```sql title=Query theme={null}
SELECT multiplyDecimal(toDecimal256(-12, 0), toDecimal32(-2.1, 1), 1)
```

```response title=Response theme={null}
25.2
```

**일반 곱셈과의 차이점**

```sql title=Query theme={null}
SELECT multiplyDecimal(toDecimal256(-12, 0), toDecimal32(-2.1, 1), 1)
```

```response title=Response theme={null}
┌─multiply(toDecimal64(-12.647, 3), toDecimal32(2.1239, 4))─┐
│                                               -26.8609633 │
└───────────────────────────────────────────────────────────┘
┌─multiplyDecimal(toDecimal64(-12.647, 3), toDecimal32(2.1239, 4))─┐
│                                                         -26.8609 │
└──────────────────────────────────────────────────────────────────┘
```

**Decimal 형 오버플로우**

```sql title=Query theme={null}
SELECT
    toDecimal64(-12.647987876, 9) AS a,
    toDecimal64(123.967645643, 9) AS b,
    multiplyDecimal(a, b);
SELECT
    toDecimal64(-12.647987876, 9) AS a,
    toDecimal64(123.967645643, 9) AS b,
    a * b;
```

```response title=Response theme={null}
┌─────────────a─┬─────────────b─┬─multiplyDecimal(toDecimal64(-12.647987876, 9), toDecimal64(123.967645643, 9))─┐
│ -12.647987876 │ 123.967645643 │                                                               -1567.941279108 │
└───────────────┴───────────────┴───────────────────────────────────────────────────────────────────────────────┘
Received exception from server (version 22.11.1):
Code: 407. DB::Exception: Received from localhost:9000. DB::Exception: Decimal math overflow:
While processing toDecimal64(-12.647987876, 9) AS a, toDecimal64(123.967645643, 9) AS b, a * b. (DECIMAL_OVERFLOW)
```

<div id="negate">
  ## negate
</div>

도입 버전: v1.1.0

인수 `x`의 부호를 반대로 바꿉니다. 결과는 항상 부호 있는 형식입니다.

**구문**

```sql theme={null}
negate(x)
```

**인수**

* `x` — 부호를 반전할 값입니다.

**반환 값**

x의 부호를 반전한 값인 -x를 반환합니다.

**예시**

**사용 예시**

```sql title=Query theme={null}
SELECT negate(10)
```

```response title=Response theme={null}
-10
```

<div id="plus">
  ## plus
</div>

도입된 버전: v1.1.0

두 값 `x`와 `y`의 합을 계산합니다. 별칭: `x + y` (연산자)입니다.
정수와 날짜 또는 날짜/시간 값을 더할 수도 있습니다. 전자의 경우
날짜의 일 수가 증가하고, 후자의 경우
날짜/시간 값의 초 수가 증가합니다.
날짜와 시간도 더할 수 있습니다. `Date`와 `Time`을 더하면
`DateTime`이 됩니다. `Date`와 `Time64`를 더하거나, `Date32`와
`Time` 또는 `Time64`를 더하면 `DateTime64`가 됩니다.

**구문**

```sql theme={null}
plus(x, y)
```

**인수**

* `x` — 왼쪽 피연산자. - `y` — 오른쪽 피연산자.

**반환 값**

x와 y를 더한 값을 반환합니다

**예시**

**두 숫자 더하기**

```sql title=Query theme={null}
SELECT plus(5,5)
```

```response title=Response theme={null}
10
```

**정수와 날짜 더하기**

```sql title=Query theme={null}
SELECT plus(toDate('2025-01-01'),5)
```

```response title=Response theme={null}
2025-01-06
```

**날짜 및 시간 추가**

```sql title=Query theme={null}
SELECT toDate('2025-01-01') + CAST('14:30:25', 'Time')
```

```response title=Response theme={null}
2025-01-01 14:30:25
```

<div id="positiveModulo">
  ## positiveModulo
</div>

도입 버전: v22.11.0

`x`를 `y`로 나눌 때의 나머지를 계산합니다. `modulo` 함수와
유사하지만 `positiveModulo`는 항상 음수가 아닌 값을 반환합니다.

**구문**

```sql theme={null}
positiveModulo(x, y)
```

**별칭**: `positive_modulo`, `pmod`

**인수**

* `x` — 피제수입니다. [`(U)Int*`](/ko/reference/data-types/int-uint) 또는 [`Float*`](/ko/reference/data-types/float) 또는 [`Decimal`](/ko/reference/data-types/decimal)
* `y` — 제수(모듈러스)입니다. [`(U)Int*`](/ko/reference/data-types/int-uint) 또는 [`Float*`](/ko/reference/data-types/float) 또는 [`Decimal`](/ko/reference/data-types/decimal)

**반환 값**

`x`에서 `y`로 나누어떨어지는 `x` 이하의 가장 가까운 정수를 뺀 값을 반환합니다.

**예시**

**사용 예시**

```sql title=Query theme={null}
SELECT positiveModulo(-1, 10)
```

```response title=Response theme={null}
9
```

<div id="positiveModuloOrNull">
  ## positiveModuloOrNull
</div>

도입 버전: v25.5.0

`a`를 `b`로 나눌 때의 나머지를 계산합니다. 함수 `positiveModulo`와 유사하지만, `positiveModuloOrNull`은 오른쪽 인수가 0이면 NULL을 반환합니다.

**구문**

```sql theme={null}
positiveModuloOrNull(x, y)
```

**별칭**: `positive_modulo_or_null`, `pmodOrNull`

**인수**

* `x` — 피제수입니다. [`(U)Int*`](/ko/reference/data-types/int-uint)/[`Float32/64`](/ko/reference/data-types/float). - `y` — 제수(모듈러스)입니다. [`(U)Int*`](/ko/reference/data-types/int-uint)/[`Float32/64`](/ko/reference/data-types/float).

**반환 값**

`x`에서 `y`로 나누어떨어지는 `x` 이하의 가장 큰 정수를 뺀 값을 반환합니다. 제수가 0이면 `null`을 반환합니다.

**예시**

**positiveModuloOrNull**

```sql title=Query theme={null}
SELECT positiveModuloOrNull(5, 0)
```

```response title=Response theme={null}
\N
```
