% operator with negatives

Michael J. Eager eager at ringworld.Eng.Sun.COM
Wed Dec 19 14:25:39 AEST 1990


In article <1990Dec12.185714.7169 at mp.cs.niu.edu> t901908 at mp.cs.niu.edu (Joe Adamo) writes:
>I know this may sound silly, but what is the effect of using the 
>% (mod) operator with negatives?  I can't seem to find any info on it.


There are two well accepted definitions of the remainder of a division
when one of the arguments is zero.  One of the definitions has the remainder
be negative, the other positive.  The ANSI Standard does not specify
which definition applies. The only guarantee is that when you have

	q = d / n;  r = d % n;
that
	d == (q * n + r)


Best advice is to not use the % operator with negative values.

-- Mike Eager



More information about the Comp.lang.c mailing list