Integer division
Tom Duff
td at alice.UucP
Sat Feb 1 01:43:15 AEST 1986
Pardon my flamage, but what sort of nonsense is this:
[in reference to divide instructions that give -(a/b)=(-a)/b]
>I have NEVER seen an instance where the first one is preferable. Not
>only is it not preferable, it is just incorrect.
Wrong! That's the definition. It can't be incorrect. It might be different
from what a number theorist wants, but by no stretch of the imagination can
it be called incorrect. A mathematician should be able to to handle this
elementary concept.
>Why such a routine
>has been allowed to be 50% inaccurate in every existing language all
>these years is beyond me.
Well, it's that way because that's the way it's defined in the ANSI Fortran
standard, and Fortran is probably a Good Thing for a computer to support --
certainly more important than niggling know-nothing number-theoretic nonsense.
Why does Fortran do it that way?
Probably because the IBM 701 did it that way. Why did the IBM 701
do it that way? Well, at the time people thought that a divide
instruction that satisfied certain identities was more important
than mod function behavior. Certainly in most of the applications
for which Fortran was designed (i.e. engineering numerical calculations)
the behavior of the mod function is of minimal interest.
In any case, why should you be worried that some operation you want to do
isn't primitive. Most programming languages don't provide arithmetic
on multivariate polynomials with arbitrary precision rational coefficients
either (which I want more often than I want a number-theoretic mod function.)
In any case, it's fairly easy to write:
a=b%c
if(a<0) a+=c
I can't believe that you couldn't discover this code sequence yourself.
(Note that it works whether the range of b%c is [0,c) or (-c,c) -- the
C language definition allows either.)
>[Whether CS people should even be *allowed* to make such mathematical
>decisions is another question. In C on UNIX, for example, one has
>log(-x) == log(x), a rather dangerous identity, not based on anything
>comprehensible. Thus, the implementation of general exponentiation,
>a**b = pow(a,b) = exp( b*log(a) ) will silently return the wrong value
>if a is negative. (Try taking cube roots this way!)]
This sort of nonsense makes me wonder whether the writer should be
allowed to make *any* sort of decision at all. No plausible definition
of the log function will let you use it to take cube roots of arbitrary
reals in this manner.
On a higher level of discourse, this writer (Matthew P Whiner) seems
to think that mathematicians enjoy some sort of moral and intellectual
superiority to engineers and computer scientists. Usually, this
attitude is a symptom of envy, since mathematicians are so hard to
employ, can't get decent salaries when they do find work, and have
a much harder time raising grant money. The smart ones embrace
computer science rather than denigrating it. The dull ones just
say ``Computer Science? Pfui: that's not mathematics,'' thus demonstrating
their lack of understanding of the nature of mathematics and of
computer science.
In summary:
It is better to remain silent and be thought a fool than
to speak up and remove all doubt.
More information about the Comp.lang.c
mailing list