C99 says in Section 6.5.5 Multiplicative operators (bold mine):
The result of the /
operator is the quotient from the division of the first operand by the
second; the result of the %
operator is the remainder. In both operations, if the value of
the second operand is zero, the behavior is undefined.
When integers are divided, the result of the /
operator is the algebraic quotient with any
fractional part discarded. If the quotient a/b
is representable, the expression
(a/b)*b + a%b
shall equal a
.
It says that %
is the remainder, and does not use the word "modulus" to describe it. In fact, the word "modulus" only occurs in three places in my copy of C99, and those all relate to the library and not to any operator.
It does not say anything that requires that the remainder be positive. If a positive remainder is required, then rewriting a%b
as (a%b + b) % b
will work for either sign of a
and b
and give a positive answer at the expense of an extra addition and division. It may be cheaper to compute it as m=a%b; if (m<0) m+=b;
depending on whether missed branches or extra divisions are cheaper in your target architecture.
Edit: I know nothing about Objective-C. Your original question was tagged C and all answers to date reflect the C language, although your example appears to be Objective-C code. I'm assuming that knowing what is true about C is helpful.