Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

I have a theory that it's impossible to prove anything, but I can't prove it.


devel / comp.lang.misc / Re: Integer division - semantics and syntax

SubjectAuthor
* Integer division - semantics and syntaxJames Harris
`- Re: Integer division - semantics and syntaxJames Harris

1
Integer division - semantics and syntax

<sbsns5$dss$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=271&group=comp.lang.misc#271

  copy link   Newsgroups: comp.lang.misc
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: james.harris.1@gmail.com (James Harris)
Newsgroups: comp.lang.misc
Subject: Integer division - semantics and syntax
Date: Sun, 4 Jul 2021 17:33:08 +0100
Organization: A noiseless patient Spider
Lines: 109
Message-ID: <sbsns5$dss$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 4 Jul 2021 16:33:09 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="b3af7839fcc8076f4903d66651e5d01c";
logging-data="14236"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+9fsaa8bLxB3ONp9bZsxTrBuSFRhJ+9Bg="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:5dKbT4xv20Dy4ZfiL4SUTkl3UfQ=
Content-Language: en-GB
X-Mozilla-News-Host: snews://news.eternal-september.org/
 by: James Harris - Sun, 4 Jul 2021 16:33 UTC

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

where

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

remrd
remrtz
remrne

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

divrd

would be paired with

remrd

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

Re: Integer division - semantics and syntax

<sd72ej$6uf$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=275&group=comp.lang.misc#275

  copy link   Newsgroups: comp.lang.misc
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: james.harris.1@gmail.com (James Harris)
Newsgroups: comp.lang.misc
Subject: Re: Integer division - semantics and syntax
Date: Tue, 20 Jul 2021 18:51:15 +0100
Organization: A noiseless patient Spider
Lines: 39
Message-ID: <sd72ej$6uf$1@dont-email.me>
References: <sbsns5$dss$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 20 Jul 2021 17:51:15 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="1f365fd120c100f059d4bd246cb8e761";
logging-data="7119"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18XaQRetim2JfuxOVWrmHQlX5CB4v45n/w="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:6vZpXI0QTojKtr/XWRSaWuDEpWg=
In-Reply-To: <sbsns5$dss$1@dont-email.me>
Content-Language: en-GB
 by: James Harris - Tue, 20 Jul 2021 17:51 UTC

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.

https://en.wikipedia.org/wiki/Euclidean_division

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

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor