Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/17/84; site abic.UUCP Path: utzoo!linus!decvax!cwruecmp!abic!jst From: jst@abic.UUCP (Shack Toms) Newsgroups: net.lang.c Subject: Re: Integer division Message-ID: <739@abic.UUCP> Date: Tue, 18-Feb-86 15:17:03 EST Article-I.D.: abic.739 Posted: Tue Feb 18 15:17:03 1986 Date-Received: Wed, 19-Feb-86 20:23:46 EST References: <332@ism780c.UUCP> <11603@ucbvax.BERKELEY.EDU> <11610@ucbvax.BERKELEY.EDU> <11612@ucbvax.BERKELEY.EDU> <1666@utah-gr.UUCP> Distribution: net Organization: Allen-Bradley Co., Highland Heights, OH 44143 Lines: 27 > [ Thomas Spencer writes...] > There is of course, always another way to look at it. The '%' operator > is not defined to be MOD, it is defined such that > (a/b)*b + a%b = a > In other words, it is the REMAINDER upon dividing a by b. Now, if you > want a%b to always be positive, you must then have > (-a)/b != -(a/b) > which, I think you will agree, is much worse.... This property [(-a)/b == -(a/b)] is *not* guaranteed by the definition of % in C. An implementation of C is free to return nonnegative remainders, so long as the implementation of / rounds toward -infinity. This property does not seem all that precious to me, though. Anyone who writes code which utilizes the entire range of int soon learns not to be too cavalier with negation, since the range of int is not always symmetric about 0. > ... If you really want MOD, > here it is: > mod(a,b) > { > return (( a % b ) + b) % b; > } And if you really want quotients to round toward 0 and negative remainders [although I believe this is desired less frequently] you had better write those too.