I've seen some Math definitions that place the additional restriction
that the remainder r and divisor d is such that
0 <=r < d
It looks like the mod operator in this case accounting for such a
restriction.
No, that's not what's going on. C's definition for the modulus operator
contains no such restriction. In fact, it requires that a%b be negative
if a and b are both signed, and a is negative, unless 'a' is a multiple
of b.
What's causing this C code to correctly produce a value other than the
one Michael was expecting is the fact that 'a' has the type 'long',
while 'b' has the type 'unsigned long', with the result that 'a' is
converted to the 'unsigned long' before evaluating the modulus. The
conversion produces the value ULONG_MAX-57, which is positive, so that
a%b produces a value of 21. Michael's assertion to the contrary
notwithstanding, a - a%b is in fact divisible by b (because the same
conversion occurs to both occurrences of 'a'). If Michael had written
a %= (long)b;
b would have been converted to long, without change of value, and he
would have gotten a result of -21, which violates the requirement you
mention, but satisfies the one he mentions. Whether that's the value he
was expecting is unclear; he might have been expecting 16.cd