名稱空間
變體
操作

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

來自 cppreference.com
< cpp‎ | numeric‎ | math
 
 
 
常用數學函式
函式
基本操作
divldivlldivimaxdiv
(C++11)
(C++11)  
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
指數函式
(C++11)
(C++11)

(C++11)
(C++11)
冪函式
(C++11)
(C++11)
三角
雙曲函式
(C++11)
(C++11)
(C++11)

誤差函式和伽馬函式
(C++11)
(C++11)
(C++11)
(C++11)
取整浮點運算
(C++11)(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
浮點操縱函式
(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)
(C++11)
分類和比較
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
型別
div_t
ldiv_t
lldiv_t
(C++11)
imaxdiv_t
(C++11)
(C++11)
(C++11)
宏常量
分類
(C++11)(C++11)(C++11)(C++11)(C++11)


 
定義於標頭檔案 <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::intmax_t擴充套件整型時,在 <cinttypes> 中才提供 std::div 對於 std::intmax_t 的過載。
(C++11 起)

商是代數商,其任何小數部分都被丟棄(截斷為零)。餘數滿足 quot * y + rem == x

(C++11 前)

商是表示式 x / y 的結果。餘數是表示式 x % y 的結果。

(C++11 起)

目錄

[編輯] 引數

x, y - 整數值

[編輯] 返回值

如果餘數和商都能表示為相應型別(分別為 intlonglong longstd::intmax_t)的物件,則將它們作為型別為 std::div_tstd::ldiv_tstd::lldiv_tstd::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 文件div

[編輯] 外部連結

1.  歐幾里得除法 — 來自維基百科。
2.  模運算(和截斷除法) — 來自維基百科。