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

From cppreference.com

Defined in header

<cstdlib>

std::div_tdiv(intx,inty); (1) (constexpr since C++23)std::ldiv_tdiv(longx,longy); (2) (constexpr since C++23)std::lldiv_tdiv(longlongx,longlongy); (3)(since C++11)
(constexpr since C++23)std::ldiv_tldiv(longx,longy); (4)(constexpr since C++23)std::lldiv_tlldiv(longlongx,longlongy); (5)(since C++11)
(constexpr since C++23)Defined in header

<cinttypes>

std::imaxdiv_tdiv(std::intmax_tx,std::intmax_ty); (6)(since C++11)
(constexpr since C++23)std::imaxdiv_timaxdiv(std::intmax_tx,std::intmax_ty); (7)(since C++11)
(constexpr since C++23)Computes both the quotient (the member quot) and the remainder (the member rem) of the division of the numerator x by the denominator y.

The quotient is the algebraic quotient with any fractional part discarded (truncated towards zero). The remainder is such that quot*y+rem==x.

(until C++11)The quotient is the result of the expression x/y. The remainder is the result of the expression x%y.

(since C++11)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, longlong,

std::intmax_t

, respectively), returns both as an object of type std::div_t, std::ldiv_t, std::lldiv_t, std::imaxdiv_t defined as follows:

std::div_t

structdiv_t{intquot;intrem;};or

structdiv_t{intrem;intquot;};std::ldiv_t

structldiv_t{longquot;longrem;};or

structldiv_t{longrem;longquot;};std::lldiv_t

structlldiv_t{longlongquot;longlongrem;};or

structlldiv_t{longlongrem;longlongquot;};std::imaxdiv_t

structimaxdiv_t{std::intmax_tquot;std::intmax_trem;};or

structimaxdiv_t{std::intmax_trem;std::intmax_tquot;};If either the remainder or the quotient cannot be represented, the behavior is undefined.

Notes

Until

CWG issue 614

was resolved (

N2757

), the rounding direction of the quotient and the sign of the remainder in the

built-in division and remainder operators

was implementation-defined if either of the operands was negative, but it was well-defined in std::div.

On many platforms, a single CPU instruction obtains both the quotient and the remainder, and this function may leverage that, although compilers are generally able to merge nearby / and % where suitable.

Example

Run this code

#include<cassert>#include<cmath>#include<cstdlib>#include<iostream>#include<sstream>#include<string>std::stringdivision_with_remainder_string(intdividend,intdivisor){autodv=std::div(dividend,divisor);assert(dividend==divisor*dv.quot+dv.rem);assert(dv.quot==dividend/divisor);assert(dv.rem==dividend%divisor);autosign=[](intn){returnn>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::stringitoa(intn,intradix/*[2..16]*/){std::stringbuf;std::div_tdv{};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()};}intmain(){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';}Output:

+369 = +10 * (+36) +9 +369 = -10 * (-36) +9 -369 = +10 * (-36) -9 -369 = -10 * (+36) -9 12345 -12345 101010 ffff See also

External links