5

c99 standard says that result of modulo operation has same sign as first operand. So -9 % 7 = -2 and 9 % -7 = 2.

I read in one book that c89 standard depends on implementation. So -9 % 7 could yield -2 or 5??? How remainder of -9 / 7 could be 5?

2

2 Answers 2

7

Consider two numbers a and b.

The quotient q=a/b and remainder r=a%b satisfy the equation a == q*b + r.

An (hypothetical) implementation of C89 in which -9 % 7 produces 5 is an implementation in which -9 / 7 is computed as -2.

The mathematical (Euclidian) division constrains r to be positive and smaller than b. C99 constrains it to be of the same sign as a and strictly between -b and b. It is all only a matter of convention.

7
  • If integer division does not truncate but "rounds down" (towards -infinity), then this is reasonable, isn't it?
    – user529758
    Nov 3, 2012 at 10:36
  • @H2CO3 I work on a static analyzer in which sets of possible values can be summarized as congruence information (for instance: “in this program at this line it is known that all values of x are equal to 1 modulo 3”). The analyzer uses Euclidian division because it has the best algebraic properties, but I have grumbled against C99's (really, processors') easier-to-implement-but-less-algebraic choice more than once. Nov 3, 2012 at 10:40
  • I see, but 1. I don't think it makes a lot of sense to complain about the standard, it was written so that it's easy to implement (how else would you explain all the undefined behaviors?) and 2. it seems logical to me to be permissive about this, I don't think forcing integer division to round towards 0 is a good idea.
    – user529758
    Nov 3, 2012 at 10:45
  • @H2CO3 I think that your points 1. and 2. explain the choice in C89 (they were trying to make everyone happy). In C99, they must have looked and realized that every hardware implementation was doing truncation, so the standard might as well specify division this way. Division with truncation is easier to implement in hardware because the sign and absolute value of the result can be computed in parallel. Nov 3, 2012 at 10:49
  • 1
    @H2CO3 Because when trying to write a C program, it is a pain not to be able to assume that division works one way or the other. An even more extreme position would be “why specify division at all in the standard? Why not leave it as a compiler-specific extension that (in practice) all compilers would have?". The answer is that a language standard such as C is a compromise between allowing leeway in implementations and allowing programmers to program concisely and portably without having to detect the behavior of division at compile-time. Nov 3, 2012 at 11:00
5

% operator is defined as:

a == (a / b * b) + a % b

so

a % b = a - (a / b * b)

% as a remainder operator

If / rounds towards 0 (like C99):

-9 % 7 == -2

you have -9 / 7 == -1 so the % is -2 because

-9 % 7 == -9 - (-9 / 7 * 7) + 9 == -9 + 7 == -2 

% as a modulo operator

If / rounds towards minus infinity:

-9 % 7 == 5

you have -9 / 7 == -2 so the % is 5

-9 % 7 == -9 - (-9 / 7 * 7) + 9 == -9 + 14 == 5
1
  • If / rounds towards minus infinity That behaviour is prohibited by the standard from C99 onward, right? Apr 16, 2020 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.