std::div, std::ldiv, std::lldiv, std::imaxdiv
| Definiert in Header <cstdlib> |
||
| std::div_t div( int x, int y ); |
(1) | (constexpr seit C++23) |
| std::ldiv_t div( long x, long y ); |
(2) | (constexpr seit C++23) |
| std::lldiv_t div( long long x, long long y ); |
(3) | (seit C++11) (constexpr seit C++23) |
| std::ldiv_t ldiv( long x, long y ); |
(4) | (constexpr seit C++23) |
| std::lldiv_t lldiv( long long x, long long y ); |
(5) | (seit C++11) (constexpr seit C++23) |
| Definiert in Header <cinttypes> |
||
| std::imaxdiv_t div( std::intmax_t x, std::intmax_t y ); |
(6) | (seit C++11) (constexpr seit C++23) |
| std::imaxdiv_t imaxdiv( std::intmax_t x, std::intmax_t y ); |
(7) | (seit C++11) (constexpr seit C++23) |
Berechnet sowohl den Quotienten als auch den Rest der Division des Zählers x durch den Nenner y.
|
6,7) Überladung von
std::div für std::intmax_t ist in <cinttypes> genau dann vorhanden, wenn std::intmax_t ein erweiterter Ganzzahltyp ist. |
(seit C++11) |
|
Der Quotient ist der algebraische Quotient, wobei der Bruchteil verworfen wird (gegen Null abgeschnitten). Der Rest ist so beschaffen, dass quot * y + rem == x. |
(bis C++11) |
|
Der Quotient ist das Ergebnis des Ausdrucks x / y. Der Rest ist das Ergebnis des Ausdrucks x % y. |
(seit C++11) |
Inhalt |
[edit] Parameter
| x, y | - | Ganzzahlwerte |
[edit] Rückgabewert
Wenn sowohl der Rest als auch der Quotient als Objekte des entsprechenden Typs (int, long, long long, std::intmax_t, jeweils) dargestellt werden können, werden beide als Objekt des Typs std::div_t, std::ldiv_t, std::lldiv_t, std::imaxdiv_t zurückgegeben, die wie folgt definiert sind
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; };
Wenn der Rest oder der Quotient nicht dargestellt werden kann, ist das Verhalten undefiniert.
[edit] Anmerkungen
Bis zur Behebung des CWG-Problems 614 (N2757) war die Rundungsrichtung des Quotienten und das Vorzeichen des Rests in den eingebauten Divisions- und Restoperatoren implementierungsabhängig, wenn einer der Operanden negativ war, aber in std::div war es wohldefiniert.
Auf vielen Plattformen liefert ein einzelner CPU-Befehl sowohl den Quotienten als auch den Rest, und diese Funktion kann davon Gebrauch machen, obwohl Compiler im Allgemeinen in der Lage sind, nahe beieinander liegende / und % zusammenzuführen, wo dies geeignet ist.
[edit] Beispiel
#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'; }
Ausgabe
+369 = +10 * (+36) +9 +369 = -10 * (-36) +9 -369 = +10 * (-36) -9 -369 = -10 * (+36) -9 12345 -12345 101010 ffff
[edit] Siehe auch
| (C++11)(C++11) |
Rest der Gleitkommadivisionsoperation (Funktion) |
| (C++11)(C++11)(C++11) |
Vorzeichenbehafteter Rest der Divisionsoperation (Funktion) |
| (C++11)(C++11)(C++11) |
vorzeichenbehafteter Rest sowie die letzten drei Bits der Divisionsoperation (Funktion) |
| C-Dokumentation für div
| |
[edit] Externe Links
| 1. | Euklidische Division — Von Wikipedia. |
| 2. | Modulo (und abgeschnittene Division) — Von Wikipedia. |