C++ functions for integer division with well defined rounding strategy

前端 未结 3 1568
星月不相逢
星月不相逢 2021-02-06 06:14

I want something in C++ that lets me do efficient integer division with specified rounding behavior, something like this:

div_down(-4,3)        ==> -2
div_up(         


        
3条回答
  •  北恋
    北恋 (楼主)
    2021-02-06 06:43

    The last draft of C++11, n3242 which is almost identical to the actual C++11 standard, says this in 5.6 point 4 (page 118):

    For integral operands the / operator yields the algebraic quotient with any fractional part discarded; (see note 80)

    Note 80 states (note that notes are non-normative):

    80) This is often called truncation towards zero.

    For completeness, Point 4 goes on to state:

    if the quotient a/b is representable in the type of the result, (a/b)*b + a%b is equal to a.

    which can be shown to require the sign of a%b to be the same as the sign of a (when not zero).

    Note: The actual C++11 standard is not legally online available. However, the drafts are. Luckily, the differences between the last draft (N3242) and the actual standard are small. See this answer.

    NOTE: I am not sure which compilers adhere to the C++11 standard yet.


    So div_to_zero() is the regular / division.

    For the other functions, I'm afraid you'll have to test the signs of a and b and adjust accordingly. Sometimes, an additional check whether a%b equals zero might be needed. So we're looking at 12 test cases per function here (3 for the sign or zeroness of a, times 2 for the sign of b, times 2 whether a%b equals zero or not).

    That's too much for me to get right right now, so maybe someone else will jump in and provide the correct answer.

    I'm aware that I have not answered your question, but the info above seemed valuable and was too large to fit in a comment.

提交回复
热议问题