[ Web Proxy ]
URL:
Viewing: https://ru.cppreference.com/cpp/numeric/math/div [Back]  [Original]

std::div, std::ldiv, std::lldiv, std::imaxdiv cppreference.com
cppreference.com

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

cppreference.com
 
C++
(C++20)
(C++20)
(C++11)
(C++20)
/
(C++11)
(C++11)
(C++11)
(C++17)
 
(C++17)
(C++20)
(C++11)
(C++11)
(C++17)
(C++17)
(C++20)
(C++20)
(C++11)
(C++17)
(C++20)
(C++23)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
 
(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)(C++11)(C++11)(C++11)(C++11)
 
<tbody> </tbody>
std::div_t div( int x, int y );
(1) (constexpr C++23)
std::ldiv_t div( long x, long y );
(2) (constexpr C++23)
std::lldiv_t div( long long x, long long y );
(3) ( C++11)
(constexpr C++23)
std::ldiv_t ldiv( long x, long y );
(4) (constexpr C++23)
std::lldiv_t lldiv( long long x, long long y );
(5) ( C++11)
(constexpr C++23)
std::imaxdiv_t div( std::intmax_t x, std::intmax_t y );
(6) ( C++11)
(constexpr C++23)
std::imaxdiv_t imaxdiv( std::intmax_t x, std::intmax_t y );
(7) ( C++11)
(constexpr C++23)

x y.

( 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; };

, .

C++11 , , std::div .

, , , / %, .

#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)]; //    
    }
    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.  ( ) .

Web Proxy Viewer  |  New URL  |  Original Page