std:: div, std:: ldiv, std:: lldiv, std:: imaxdiv
|
헤더 파일에 정의됨
<cstdlib>
|
||
|
std
::
div_t
div
(
int
x,
int
y
)
;
|
(1) | (C++23부터 constexpr) |
|
std
::
ldiv_t
div
(
long
x,
long
y
)
;
|
(2) | (C++23부터 constexpr) |
|
std
::
lldiv_t
div
(
long
long
x,
long
long
y
)
;
|
(3) |
(C++11부터)
(C++23부터 constexpr) |
|
std
::
ldiv_t
ldiv
(
long
x,
long
y
)
;
|
(4) | (C++23부터 constexpr) |
|
std
::
lldiv_t
lldiv
(
long
long
x,
long
long
y
)
;
|
(5) |
(C++11부터)
(C++23부터 constexpr) |
|
헤더 파일에 정의됨
<cinttypes>
|
||
|
std
::
imaxdiv_t
div
(
std::
intmax_t
x,
std::
intmax_t
y
)
;
|
(6) |
(C++11부터)
(C++23부터 constexpr) |
|
std
::
imaxdiv_t
imaxdiv
(
std::
intmax_t
x,
std::
intmax_t
y
)
;
|
(7) |
(C++11부터)
(C++23부터 constexpr) |
분자 x 를 분모 y 로 나눈 몫과 나머지를 모두 계산합니다.
|
6,7)
std::div
의 오버로드는
std::intmax_t
에 대해
<cinttypes>
에 제공되며, 이는
std::intmax_t
가
확장 정수 타입
인 경우에만 해당합니다.
|
(C++11 이후) |
|
몫은 소수 부분이 버려진(0 방향으로 절삭) 대수적 몫입니다. 나머지는 quot * y + rem == x 을 만족합니다. |
(C++11 이전) |
|
몫은 x / y 표현식의 결과입니다. 나머지는 x % y 표현식의 결과입니다. |
(C++11 이후) |
목차 |
매개변수
| x, y | - | 정수 값 |
반환값
나머지와 몫 모두가 해당 타입(
int
,
long
,
long
long
,
std::intmax_t
)의 객체로 표현될 수 있는 경우, 다음과 같이 정의된
std::div_t
,
std::ldiv_t
,
std::lldiv_t
,
std::imaxdiv_t
타입의 객체로 둘 다 반환합니다:
std:: div_t
struct div_t { int quot; int rem; };
또는
struct div_t { int rem; int quot; };
std:: ldiv_t
struct ldiv_t { long quot; long rem; };
또는
struct ldiv_t { long rem; long quot; };
std:: lldiv_t
struct lldiv_t { long long quot; long long rem; };
또는
struct lldiv_t { long long rem; long long quot; };
std:: imaxdiv_t
struct imaxdiv_t { std::intmax_t quot; std::intmax_t rem; };
또는
struct imaxdiv_t { std::intmax_t rem; std::intmax_t quot; };
나머지나 몫 중 하나라도 표현할 수 없는 경우, 동작은 정의되지 않습니다.
참고 사항
CWG issue 614
가 해결되기 전까지 (
N2757
), 피연산자 중 하나라도 음수인 경우
내장 나눗셈 및 나머지 연산자
의 몫의 반올림 방향과 나머지의 부호는 구현에 따라 정의되었으나,
std::div
에서는 명확하게 정의되어 있었습니다.
많은 플랫폼에서 단일 CPU 명령어로 몫과 나머지를 동시에 얻을 수 있으며, 이 함수는 그러한 기능을 활용할 수 있습니다. 비록 컴파일러들이 일반적으로 인접한
/
와
%
연산을 적절한 경우에 결합할 수 있지만요.
예제
#include <cassert> #include <cmath> #include <cstdlib> #include <iostream> #include <sstream> #include <string> std::string division_with_remainder_string(int dividend, int divisor) { auto dv = std::div(dividend, divisor); assert(dividend == divisor * dv.quot + dv.rem); assert(dv.quot == dividend / divisor); assert(dv.rem == dividend % divisor); auto sign = [](int n){ return n > 0 ? 1 : n < 0 ? -1 : 0; }; assert((dv.rem == 0) or (sign(dv.rem) == sign(dividend))); return (std::ostringstream() << std::showpos << dividend << " = " << divisor << " * (" << dv.quot << ") " << std::showpos << dv.rem).str(); } std::string itoa(int n, int radix /*[2..16]*/) { std::string buf; std::div_t dv{}; dv.quot = n; do { dv = std::div(dv.quot, radix); buf += "0123456789abcdef"[std::abs(dv.rem)]; // string literals are arrays } while (dv.quot); if (n < 0) buf += '-'; return {buf.rbegin(), buf.rend()}; } int main() { std::cout << division_with_remainder_string(369, 10) << '\n' << division_with_remainder_string(369, -10) << '\n' << division_with_remainder_string(-369, 10) << '\n' << division_with_remainder_string(-369, -10) << "\n\n"; std::cout << itoa(12345, 10) << '\n' << itoa(-12345, 10) << '\n' << itoa(42, 2) << '\n' << itoa(65535, 16) << '\n'; }
출력:
+369 = +10 * (+36) +9 +369 = -10 * (-36) +9 -369 = +10 * (-36) -9 -369 = -10 * (+36) -9 12345 -12345 101010 ffff
참고 항목
|
(C++11)
(C++11)
|
부동 소수점 나눗셈 연산의 나머지
(함수) |
|
(C++11)
(C++11)
(C++11)
|
부호 있는 나눗셈 연산의 나머지
(함수) |
|
(C++11)
(C++11)
(C++11)
|
부호 있는 나머지 및 나눗셈 연산의 마지막 세 비트
(함수) |
|
C documentation
for
div
|
|
외부 링크
| 1. | Euclidean division — 위키백과 출처. |
| 2. | Modulo (and Truncated division) — 위키백과 출처. |