Namespaces
Variants

std:: div, std:: ldiv, std:: lldiv, std:: imaxdiv

From cppreference.net
Common mathematical functions
Nearest integer floating point operations
(C++11)
(C++11)
(C++11) (C++11) (C++11)
Floating point manipulation functions
(C++11) (C++11)
(C++11)
(C++11)
Classification and comparison
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Types
div_t
ldiv_t
lldiv_t
(C++11)
imaxdiv_t
(C++11)
(C++11)
(C++11)
Macro constants
ヘッダーで定義 <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以降)

商は小数部分を切り捨てた代数商(ゼロ方向への切り捨て)です。剰余は 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)
符号付き剰余および除算操作の下位3ビット
(関数)

外部リンク

1. Euclidean division — Wikipediaより。
2. Modulo (and Truncated division) — Wikipediaより。