% operator with negatives

Dan Bernstein brnstnd at kramden.acf.nyu.edu
Mon Dec 17 05:47:31 AEST 1990


In article <1529 at bbxsda.UUCP> scott at bbxsda.UUCP (Scott Amspoker) writes:
> In article <7461:Dec1310:04:0990 at kramden.acf.nyu.edu> brnstnd at kramden.acf.nyu.edu (Dan Bernstein) writes:
> >And if you're asking ``Where is the information on it?'', the answer is
> >that a % b is defined so that (a / b) * b + (a % b) equals a. It's the
> >ambiguous definition of / that makes % so useless with negatives.
> Well I can really stir up the muck by pointing out that, by definition,
>     a mod 0 == a

We are (unfortunately) not talking about the mathematical mod function.
We are talking about the C % function.

I have never seen a sane use of remainders outside [0,b), b positive.
I have never seen a program use remainders for b negative. It's a shame
that no popular programming language has a mod function defined only for
b positive and guaranteed to be between 0 and b - 1 inclusive, as this
would strike the right balance between portability and efficiency.

---Dan



More information about the Comp.lang.c mailing list