From: utzoo!decvax!harpo!npoiv!alice!rabbit!ark
Newsgroups: net.math
Title: integer division
Article-I.D.: rabbit.755
Posted: Fri Sep 17 09:27:33 1982
Received: Tue Sep 21 08:54:38 1982

The C Reference Manual has the following to say about integer division:

"The binary / operator indicates division.  When positive integers
are divided truncation is toward 0, but the form of truncation is
machine-dependent if either operand is negative.  On all machines covered
by this manual, the remainder has the same sign as the dividend.
It is always true that (a/b)*b + a%b is equal to a (if b is not 0)."

In other words, you get what the machine gives you, and that is usually
obtained by making the remainder have the same sign as the dividend.

It is consistent to have the remainder have the same sign as the dividend
or as the divisor.  Provided that division and remainder are consistent
with each other (in other words, that (a/b)*b + a%b is equal to a)
exercising the choice one way or the other has different implications.

For instance, if you say the divisor should have the same sign as the
remainder, you make the remainder operator behave more like the
normal mathematical usage, so that (-1)%3 would be 2.  This usage
is actually nice for preventing surprises:  if you take a%b and b
is positive, this choice guarantees a non-negative result.  Thus,
for instance, you could use % for division hashing with the assurance
that the remainder would indeed be a valid index in your hash table.
However, if division is to be kept consistent, it must always truncate
down in this case.  This violates the sometimes useful idea that
(-a)/b should always equal -(a/b), and would have (-3)/2 equal to -2,
for example.

The other choice is to make the remainder and dividend have the same sign.
This preserves (-a)/b equal to -(a/b) but now means that (-1)%3 is -1.

I suppose intuitive behavior on division is better than on remainder.

The language that has my favorite treatment of this issue is APL.
In APL, the remainder has the sign of the divisor, and the issue
of counterintuitive division is handled by having division return
a real number at all times.  If you want truncating division,
you must do the truncation yourself and it will truncate any
way you asked.