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
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 wird in <cinttypes> bereitgestellt, wenn und nur wenn std::intmax_t ein erweiterter Ganzzahltyp ist.
(seit C++11)

Der Quotient ist der algebraische Quotient, wobei alle Nachkommastellen verworfen werden (abgeschnitten in Richtung Null). 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)

Inhaltsverzeichnis

Parameter

x, y - Ganzzahlwerte

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, gibt beide als ein Objekt vom Typ std::div_t , std::ldiv_t , std::lldiv_t , std::imaxdiv_t zurück, die wie folgt definiert sind:

std:: div_t

struct div_t { int quot; int rem; };

oder

struct div_t { int rem; int quot; };

std:: ldiv_t

struct ldiv_t { long quot; long rem; };

oder

struct ldiv_t { long rem; long quot; };

std:: lldiv_t

struct lldiv_t { long long quot; long long rem; };

oder

struct lldiv_t { long long rem; long long quot; };

std:: imaxdiv_t

struct imaxdiv_t { std::intmax_t quot; std::intmax_t rem; };

oder

struct imaxdiv_t { std::intmax_t rem; std::intmax_t quot; };

Wenn entweder der Rest oder der Quotient nicht dargestellt werden kann, ist das Verhalten undefiniert.

Hinweise

Bis zur Lösung von CWG Issue 614 ( N2757 ) war die Rundungsrichtung des Quotienten und das Vorzeichen des Rests bei den eingebauten Divisions- und Restoperatoren implementierungsdefiniert, wenn einer der Operanden negativ war, jedoch in std::div wohldefiniert.

Auf vielen Plattformen erhält eine einzelne CPU-Instruktion sowohl den Quotienten als auch den Rest, und diese Funktion kann dies nutzen, obwohl Compiler generell in der Lage sind, nahegelegene / und % Operationen zusammenzuführen, wo dies geeignet ist.

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

Siehe auch

(C++11) (C++11)
Rest der Gleitkomma-Divisionsoperation
(Funktion)
(C++11) (C++11) (C++11)
Vorzeichenbehafteter Rest der Divisionsoperation
(Funktion)
(C++11) (C++11) (C++11)
Vorzeichenbehafteter Rest sowie die drei letzten Bits der Divisionsoperation
(Funktion)

Externe Links

1. Euklidische Division — Von Wikipedia.
2. Modulo (und Truncated division) — Von Wikipedia.