DIV and MOD ( was: Something IBM did right )

Richard A. O'Keefe ok at quintus.uucp
Sun Nov 13 16:51:11 AEST 1988


In article <1003 at goofy.megatest.UUCP> djones at megatest.UUCP (Dave Jones) writes:
>From article <664 at quintus.UUCP>, by ok at quintus.uucp (Richard A. O'Keefe):
>> Dave Jones' arguments, and arguments I have received by E-mail from
>> someone else, boil down to
>> 	"_I_ haven't had the wit to see a use for it, so it's _baaaad_."
>I suggest that the reader compare the two postings side by side
>and determine for themselves which speaks to mathematical utility
>and which makes unsubstantuated value claims.
>
>I also hope that in the future, Mr. O'Keefe will refrain from
>impugning the "wit" of fellow participants.

I did take the trouble to write several pages of mathematical-style
justification, but decided that it would be a waste of bandwidth to
post it.  I will here show four examples:

Given a block of memory N bytes long, and M objects to distribute it
among, how much can each object have?
	Answer = floor(N/M)		[definition 1]
How much memory will not be allocated to any object?
	Answer = N-floor(N/M)*M = N "floor-mod" M

Given that I need N bytes of memory, but that I have to ask for
pages M bytes long, how many pages should I ask for?
	Answer = ceil(N/M)		[definition 2]
How much excess memory did I have to ask for?
	Answer = ceil(N/M)*M-N = -(N "ceil-mod" M)

I have an axis ranging from 0 to N (sign unknown) which I want to
divide into M "full" buckets with perhaps one "partial" bucket
left over.  What should the width of each full bucket be?
	Answer = trunc(N/M) = N div M	[definition 3]
How wide will the partial bucket be?
	Answer = N-trunc(N/M)*M = N mod M

Which integer most accurately approximates N/M?
	Answer = round(N/M)		[definition 4]
What is the error in this approximation?
	Answer = N-round(M/M)*M = N "round-mod" M.

I did not impugn anyone's wit at all, I was attacking the form of
their arguments.  No amount of showing that a particular definition
is useful will show that other definitions are _not_ useful.  Nor is
it legitimate to claim that certain mathematical properties useful
for _some_ application are essential for _all_ uses of division.
All that the "speaking to mathematical utility" amounted to may
fairly be paraphrased as "I find this definition useful and don't
see how to use another, so another definition is bad".

For example, one person claimed that an essential property of remainder
was that X (mod N) classify all integers X into N equivalence classes.
That is a very useful property indeed, and definition 3 lacks it.
But there are applications (like the one shown) where that property is
_not_ needed, and demanding that _the_ remainder operation have it
really doesn't help.

Honestly, friends, each of the four versions of integer division
(and its corresponding remainder) shown above has its uses.  I find
myself wanting _each_ of them (though I have to admit that I want
[4] less often than the others, but what of that? another programmer
may have other needs).  A compiler could generate more efficient
code for them (whatever the hardware support for division) than I
could.  I don't care which of them gets the privilege of having its
own infix notation and update operator, I just wish they were all
available _somehow_.  [It might be a good idea to reserve the infix
notation for when it doesn't matter whether you're using floor or
trunc, then the compiler could pick whichever was more efficient.]



More information about the Comp.lang.c mailing list