Integer division - semantics and syntax

Skip to first unread message

James Harris

Jul 4, 2021, 12:33:11 PMJul 4
Thanks for the feedback in the thread about integer division and
rounding. I've been thinking about how it might all be put into practice
and that's what I'll go in to, below.

But first, some of the key points that came out about integer division:

* Unlike for floats, division of integers naturally produces /two/
results: a quotient and a remainder.

* On top of that, there are multiple ways in which integer division can
be defined including different rounding modes and Euclidean.

* Despite there being many options two ways stand out as preferred by
programmers: round down and round towards zero.

* Quotient and remainder can always be mathematically linked by saying that

n - dq = r


n = Numerator (the dividend)
d = Denominator (the divisor)
q = The calculated quotient
r = The remainder

For example, for 47 / 10 = 4 remainder 7 the remainder would be

47 - (10 * 4) = 7

There is no law to say that the four elements have to be related but
since the mathematical options of integer division are already many it
seems to me to be useful to provide a guarantee which makes the whole
topic a little bit easier to understand. So I intend to define division
and remainder to always be paired in that way.

The next question is how to let a programmer express the various
options. Here's a first stab at it.

For integer division and remainder there would be two operators

div ;integer division
rem ;integer remainder

As no rounding mode is specified in those operators the rounding would
be whatever was lexically defined in the program for the piece of code
in which they appear - something I may come back to in a separate post.

There would be other operators which would explicitly include the
rounding direction in their names such as

divrd ;Divide rounding down
divrtz ;Divide rounding towards zero
divrne ;Divide rounding to nearest even

For example, to divide a bank balance by 12 rounding down

balance divrd 12

For each div operator there would be a corresponding rem operator such as


which are for rem-round-down, etc.

For rem the rounding mode would refer not to the remainder operation
itself but to the division from which the remainder would be produced. Thus


would be paired with


etc, and the two results would be mathematically related as set out above.

In summary, there would be explicit division and remainder operators for
each of the ten possible rounding modes but there would also be a pair
of operators (div and rem) for use:

a) where the programmer either has no interest in what the rounding mode
is or

b) wants to use the rounding mode which is defined for that piece of code.

There could also be operators for Euclidean division and remainder if I
ever get my head around it. ;-)

I should add a comment that a rem which either implicitly or explicitly
uses rounding down is the mathematical modulus operation (what's left of
the denominator) and a rem in which the rounding is towards zero is what
you might think of as the remainder of the numerator.

Well, that's the basic idea.

I know there would be many operators but believe it or not that's the
best way I've found for making code as easy as possible to read while
still supporting the various options.

How does the proposal look to you? Any changes you would make to it?

James Harris

James Harris

Jul 20, 2021, 1:51:17 PMJul 20
On 04/07/2021 17:33, James Harris wrote:

> Thanks for the feedback in the thread about integer division and
> rounding. I've been thinking about how it might all be put into practice
> and that's what I'll go in to, below.

In summary, I would have ten (now eleven) rounding modes and eleven (now
twelve) mnemonic operators for each of division and remainder.

I'm surprised that no one has objected to it. Not sure whether that's
because it's a tedious and fiddly subject (it is) or because people
agreed with the approach. Knowing Usenet it's unlikely to be the latter.

The additions since the earlier post are

diveuc - Euclidean division
remeuc - Euclidean remainder

because ISTM that Euclidean division (and remainder) can be treated as a
rounding mode.

Essentially, Euclidean division always produces a non-negative
remainder. In the words of Wikipedia:

0 ≤ r < |b|,

where |b| denotes the absolute value of b.

Have to say I don't like having so many mnemonic operators but I haven't
yet thought of a better solution which is still comprehensive.

James Harris

Reply all
Reply to author
0 new messages