std::div, std::ldiv, std::lldiv, std::imaxdiv
From cppreference.com
| ヘッダ <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で割ったときの、商と剰余の両方を計算します。
| (C++11以降) |
|
商は、分数部分を切り捨てた(ゼロに向かって切り捨てた)代数的な商です。剰余は、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; };
or
struct div_t { int rem; int quot; };
std::ldiv_t
struct ldiv_t { long quot; long rem; };
or
struct ldiv_t { long rem; long quot; };
std::lldiv_t
struct lldiv_t { long long quot; long long rem; };
or
struct lldiv_t { long long rem; long long quot; };
std::imaxdiv_t
struct imaxdiv_t { std::intmax_t quot; std::intmax_t rem; };
or
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) |
除算操作における符号付き剰余および商の下位3ビット (関数) |
| C のドキュメント (div について)
| |
[編集] 外部リンク
| 1. | ユークリッド除算 - Wikipediaより。 |
| 2. | 剰余(および切り捨て除算) - Wikipediaより。 |