[R] Integer division
Martin Maechler
m@ech|er @end|ng |rom @t@t@m@th@ethz@ch
Wed Dec 21 09:51:54 CET 2022
>>>>> Richard O'Keefe
>>>>> on Wed, 21 Dec 2022 16:44:51 +1300 writes:
> Lack of consensus: I should mention Python's // operator,
> which does flooring division. I should mention Common
> Lisp, where (floor - -), (ceiling - -), (round - -), and
> (truncate - -) all return a quotient and appropriate
> remainder. I should mention Smalltalk, where // and \\
> are flooring quotient and remainder and quo: and rem: are
> truncating quotient and remainder. I should give
> dishonourable mention to certain programming languages
> where the quotient and remainder operators do not actually
> fit together.
> Why the lack of consensus: It starts with the fact that
> there wasn't an agreed *mathematical* definition. Number
> theorists, as a rule, don't care about negative numbers
> all that much. To the extent that they do care, x mod y
> has to go around in neat cycles, which flooring division
> does satisfy and truncating division does not.
> It then goes on to early computers which used sign-and-
> magnitude or ones-complement representation. In those
> computers, truncating division was the *obvious* thing to
> do. It also had the nice property that n / (2**k) was the
> same thing as an arithmetic right shift by k bits. And
> then twos-complement became popular. And not only is the
> twos-complement range asymmetric (so that x might be
> representable but -x not) but arithmetic right shifts
> aren't the same as truncating division any more. Whoops!
> And then, although flooring division still made sense
> for twos-complement but truncating division didn't really,
> new programming languages kept on specifying truncating
> division because the programming languages of the 1960s
> for the hardware of the 1960s did so. So new hardware
> designers supported the new programming languages without
> supporting the *reasons* why truncating division had been
> used.
>> (-8:7)%%4
> [1] 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3
> Think about for example histogramming a collection of
> integers by their remainders and what would happen with
> truncating remainder.
> On Tue, 20 Dec 2022 at 19:53, Göran Broström <gb using ehar.se>
> wrote:
>> Thanks Richard,
>>
>> the "rounding claim" was my mistake (as I replied to
>> Martin), I should said "truncates toward zero" as you
>> explain.
>>
>> However, my point was that these two mathematical
>> functions should be defined in the documentation, as you
>> also say. And I was surprised that there is no consensus
>> regarding the definition of such elementary functions.
>>
>> Göran
[...........]
Thank you all for your contributions, notably Richard's last one
providing really interesting historical context (of "why this mess?").
Note that the Wikipedia page
https://en.m.wikipedia.org/wiki/Modulo_operation
also does mention "Euclidean division" which does have possibly
even nicer mathematical properties than the floored division
R (and quite a few other good softwares) use.
Still, be assured that we won't change R here. Mathematical
(Algebra) related R packages can easily introduce corresponding
versions of div() and mod() functions, I'd say, and I'd guess
these would already exist somewhere.
Yesterday, I've updated the ?Arithmetic help page which now
does mention (more clearly if it was really already derivable
from the previous doc) what happens, also mentioning Knuth and
the Wikipedia page.
--> https://stat.ethz.ch/R-manual/R-devel/library/base/html/Arithmetic.html
(search for "R-devel R-manual ETH" in your browser, then
-> 'Packages' -> 'base' ..)
Martin
--
Martin Maechler
ETH Zurich and R Core team
More information about the R-help
mailing list