Skip to main content

Div

Defined in headers <cstdlib>, <cinttypes>.

Description

Computes both the quotient and the remainder of the division of the numerator x by the denominator y.

Declarations

Defined in <cstdlib>
// 1)
constexpr std::div_t div( int x, int y );
// 2)
constexpr std::ldiv_t div( long x, long y );
// 3)
constexpr std::lldiv_t div( long long x, long long y );
// 4)
constexpr std::ldiv_t ldiv( long x, long y );
// 5)
constexpr std::lldiv_t lldiv( long long x, long long y );
Defined in <cinttypes>
// 6)
std::imaxdiv_t div( std::intmax_t x, std::intmax_t y );
// 7)
std::imaxdiv_t imaxdiv( std::intmax_t x, std::intmax_t y );

Parameters

x, y - integer values

Return Value

If both the remainder and the quotient can be represented as objects of the corresponding type (int, long, long long, std::intmax_t, respectively), returns both as an object of type std::div_t, std::ldiv_t, std::lldiv_t, std::imaxdiv_t.

Examples

#include <cassert>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>

std::string euclidean_division_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
<< euclidean_division_string(369, 10) << '\n'
<< euclidean_division_string(369, -10) << '\n'
<< euclidean_division_string(-369, 10) << '\n'
<< euclidean_division_string(-369, -10) << "\n\n";

std::cout
<< itoa(12345, 10) << '\n'
<< itoa(-12345, 10) << '\n'
<< itoa(42, 2) << '\n'
<< itoa(65535, 16) << '\n';
}
Result
+369 = +10 * (+36) +9
+369 = -10 * (-36) +9
-369 = +10 * (-36) -9
-369 = -10 * (+36) -9

12345
-12345
101010
ffff

Div

Defined in headers <cstdlib>, <cinttypes>.

Description

Computes both the quotient and the remainder of the division of the numerator x by the denominator y.

Declarations

Defined in <cstdlib>
// 1)
constexpr std::div_t div( int x, int y );
// 2)
constexpr std::ldiv_t div( long x, long y );
// 3)
constexpr std::lldiv_t div( long long x, long long y );
// 4)
constexpr std::ldiv_t ldiv( long x, long y );
// 5)
constexpr std::lldiv_t lldiv( long long x, long long y );
Defined in <cinttypes>
// 6)
std::imaxdiv_t div( std::intmax_t x, std::intmax_t y );
// 7)
std::imaxdiv_t imaxdiv( std::intmax_t x, std::intmax_t y );

Parameters

x, y - integer values

Return Value

If both the remainder and the quotient can be represented as objects of the corresponding type (int, long, long long, std::intmax_t, respectively), returns both as an object of type std::div_t, std::ldiv_t, std::lldiv_t, std::imaxdiv_t.

Examples

#include <cassert>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>

std::string euclidean_division_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
<< euclidean_division_string(369, 10) << '\n'
<< euclidean_division_string(369, -10) << '\n'
<< euclidean_division_string(-369, 10) << '\n'
<< euclidean_division_string(-369, -10) << "\n\n";

std::cout
<< itoa(12345, 10) << '\n'
<< itoa(-12345, 10) << '\n'
<< itoa(42, 2) << '\n'
<< itoa(65535, 16) << '\n';
}
Result
+369 = +10 * (+36) +9
+369 = -10 * (-36) +9
-369 = +10 * (-36) -9
-369 = -10 * (+36) -9

12345
-12345
101010
ffff