Integer division

Matthew P. Wiener weemba at brahms.BERKELEY.EDU
Sat Feb 1 23:00:34 AEST 1986


In article <1666 at utah-gr.UUCP> thomas at utah-gr.UUCP (Spencer W. Thomas) 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.

In previous articles, I and Gene Smith, both mathematicians, have stated
our disapproval of defining % to be sometimes + and sometimes -.  We did
not get around to stating our opinions of the two identities involved, so
here's mine.

	(a/b)*b + a%b == a
This identity is clearly needed.  It enables one to go back and forth
between / and % in a consistent way, irregardless of whether % is MOD.
Indeed, among mathematicians, the official definition of quotient and
remainder for a over b (a,b integers with b>0) is to find the unique
integer values of q and r such that a==b*q+r and 0<=r<b.  You will see
both that the identity holds and remainder is non-negative.

	(-a)/b == -(a/b)
I see no reason for this.  It may look nice formally, it corresponds to the
school algorithms for how one goes about dividing with negative numbers by
hand, but I see no reason for it.  In fact, since to have both identities
holding requires what I consider the mathematically obnoxious choice of %,
I must oppose the identity.

Notice that I mentioned that b>0 in the official mathematical definition.
I really have no idea what one should do with b<0 in actual implementations.
I have never seen it come up in or out of mathematics.  Indeed, it perhaps
seems preferable to generate some sort of arithmetic fault here.  (Similarly
there are rare cases where you want 3./0. to NOT cause a divide by zero fault,
but you keep the fault because programmer error is the more likely explanation
for 3./0..)  But I am straying.

(Of course, these comments about division only refer to integer division.)
-----------------------------------------------------------------------------
Sorry if we seemed a little heavy handed last time.  They've always seemed
like defects that we users have to program around, and with no chance of
ever changing, burned into all programming languages forever because--before
I was even born!--someone chose the wrong convention.  (Physicists sometimes
dream of a world were the electron has a *positive* charge; if only we could
go back in time and tell Ben Franklin to try the other convention; believe
us, Ben, it makes a difference; etc.; sigh)  Suddenly we get attached to the
network and discover that maybe there is a way--why someone out there even
ASKED us mathematicians what we thought!  (Thanks!)

We are both ex-Fortran programmers, and feel C and C++ are the wave of the
future among scientific programmers.  Now to convince the others....  (You
powers that be might want to help!)

ucbvax!brahms!weemba	Matthew P Wiener/UCB Math Dept/Berkeley CA 94720



More information about the Comp.lang.c mailing list