Original Post
I'm designing a circuit that performs integer division on 32-bit 2's-complement numbers, and I'm curious on what the standards are for integer division with negative operands. For example, what should the results of 7 / (-3), (-7) / 3, and (-7) / (-3) be? Similarly, what should the results of 7 % (-3), (-7) % 3, and (-7) % (-3) be? On my computer, I get +/-2 for all of the quotients and +/-1 for all of the remainders, but there are a few mathematical points of note: However you choose to define integer division, you should have for all a, b (b != 0) that a == (a / b) * b + (a % b). In analysis, when we divide one number a by another positive number b, there are unique numbers q, r such that a = qb + r, q is an integer, and 0 <= r < b. So, you could make the argument that (-7) / 3 should be -3 and (-7) % 3 should be +2. However, for negative dividends, you obviously cannot satisfy 0 <= r < b, so the natural extensions are b < r <= 0 or 0 <= r < |b|. Personally, I prefer to always have a positive remainder, but choosing to do division that way also has some problems: for example, (-a) / b != -(a / b) if a, b > 0. Thoughts?