Integer division
Joseph S. D. Yao
jsdy at hadron.UUCP
Tue Feb 11 02:54:51 AEST 1986
In article <685 at brl-smoke.ARPA> gwyn at brl.ARPA writes:
>What I would like is for the language to provide a notation
>for obtaining BOTH the quotient and the remainder of an
>integer division in a single operation.
C doesn't provide a notation for an op with two returns, so this
might be a little hard. (How I Did It:) On a PDP-11 with no FPP
and V6 (that long ago), I wrote the long div/rem functions. Since
the work involved was massive, I saved the operands, the quotient,
and the remainder. If subsequent calls had the same operands, I
just returned the already-computed values!
If a div or rem is "cheap", this loses. However, if it is done
in microcode the same way, this technique is recommended to the
microcoders.
>This % vs. Mod debate is rather silly. C's % operator is
>NOT repeat NOT intended to be a modulo operator, although
>people often use it that way for positive operands.
Hooray. Modulo() != Remainder() except under certain circumstances,
as this branch of net.flame has shown. I was at the point of posting
this myself, the discussion was getting so disgusting (not to mention
ad hominem -- or ad speciem).
My degrees and first job titles all said "mathematics". Subsequent
job titles have included phrases like "computer scientist" and
"software engineer." This may qualify me to speak. (One degree
also says "CS", and > 1 decade of experience lends credence to
"engineer".)
I consider each occupation to be superior to the others in what they
do. Period. I don't expect the M or SE to immediately see a CS
point of view, nor an SE or CS the M point of view, or any of the
other possible permutations. SE's and programmers have made the
remainder function what it is because the specs said so -- great!
CS'ers, who don't use i m p l e m e n t e d languages unless they
have to, are free to use mod() or rem() as they please. Mathematicians
(pure) use anything they want, as long as it's in the realm of pure
thought. Mathematicians (applied) have to use the tools that are
available: so it is necessary for the SE's to provide their users
d o c u m e n t a t i o n that clearly says, e.g., what rem is (it
isn't mod, for instance). Then it is necessary for the users to
r e a d said documentation, before complaining. Or take a class
and l i s t e n . (No, I'm not saying none of my students ever
did -- just the ones that complained most that they didn't
understand. ;-})
All
>reasonable mathematicians agree on what the definition of
> a mod b
>is for positive b and negative a. That should not be
>confused with what the result of
> a % b
>should be under similar circumstances. C intentionally
>hedges a bit on the meaning of % in such a case (which
>makes that a generally inadvisable situation to allow to
>arise in one's C code).
--
Joe Yao hadron!jsdy at seismo.{CSS.GOV,ARPA,UUCP}
More information about the Comp.lang.c
mailing list