On 18/06/2021 09:59, James Harris wrote:
> On 18/06/2021 09:12, James Harris wrote:
>
> ...
>
>> In other words, I make it that there are ten potential rounding modes for
>> signed integers and six potential rounding modes for unsigned ints, and the
>> question is over which to choose (at least as default).
>
> ...
>
>> That's the overview. I'll reply with some of the mathematics of the issue.
>
> If we are dividing A by B there are two potentially useful results: the quotient
> and the remainder, and that there are two types of remainder. One is where the
> remainder is what's left over of A. The other is where the remainder is what's
> left over of B.
>
> For example,
>
> -35 / 10
>
> can lead to a remainder of 5 or -5, depending on how it's seen. Following what
> seems to be the most common convention for MOD and REM operators,
>
> REM = -5 (i.e. what's left over of the -35).
> MOD = 5 (i.e. what's left over of the 10).
>
Yes, though I would put it differently.
The 'modulo' function is a well-known mathematical construct, such that
a mod b
maps a into numbers of the form 0<=m<b if b>=0
and m>b<=0 if b<=0
so if you plot a mod b against a you get a sawtooth waveform which shows nothing
unusual in the vicinity of zero.
Then, if you use 'mod' as your remainder function, and you plot a/b against a,
you get a set of discrete points which all lie on a straight line, which is a
mathematically clean property.
So it follows that (a+n)/b == a/b +n*b
which is useful if, for example, you are trying to compute the coordinates of
some item in a matrix, and then want to shift the origin.
BUT if you choose the so-called 'rem' as your remainder function, and you then
plot a/b against a, you do NOT get a straight line, and if you want to shift the
origin in some coordinate system, then you get a complete mess (and likely some
bugs too).
So it is clear which version any mathematician would prefer.
Now when the first computers were built, division was not built into the order
code, but had to be performed by a subroutine (and in those days subroutines
were usually written by mathematicians.
BUT when division came to be incorporated into the hardware, the hardware was
designed by Engineers, and unless those engineers were closely supervised by
Mathematicians (which they usually were in Europe, though not in America) they
did it wrong. So when I was designing the Orion computer for Ferranti, I
consulted the people in Portland Place and did it right. IBM, in general, did it
wrong, and hence so did FORTRAN (AIUI).
I as pretty sure Algol 60 got it right, and I made sure that Algol 68 did
likewise, and so, it would seem, did Ada. C sat on the fence (they recognised
there was a problem) but then fell off the wrong side. C++ also sat on the fence
(and still sits there SFAIK); but it also defined the % operator to be either
'rem' or 'mod' so as to be consistent with '/'. Bjarne had a good mathematical
background, and knew his Algol 68.
> In other words, REM has the sign of the dividend, MOD has the sign of the
> divisor. I think that, also, REM will always be between 0 and the dividend
> whereas MOD will always be between 0 and the divisor.
>
> Tedious, isn't it! Now you know why I've been putting this off.....!
>
> Where it gets slightly interesting is that I think that with the appropriate
> rounding mode for division the other operations may be natural consequences.
> Taking the above division,
>
> -35 / 10
>
> could be seen as either
>
> -4 remainder 5
> -3 remainder -5
>
> so that in all cases if you add the remainder to (the divisor times the
> quotient) then you get the original dividend. That's the Ada definition of rem
> as set out in point 5 of
>
>
>
https://www.adaic.org/resources/add_content/standards/05rm/html/RM-4-5-5.html
>
>
>
> But that's enough of that for now. I may come back to that particular point but
> to save this post getting even more turgid let me ask you what rounding mode or
> modes you think a programming language ought to support for integer division.
> And which do you think should be default?
>
> I think my own personal preference for integers is "round down" but I see that
> Intel's signed divide operation uses "round towards zero" which seems to me all
> kinds of wrong. :-(
Stick to your Guns.
--
Charles H. Lindsey ---------At my New Home, still doing my own thing------
Tel:
+44 161 488 1845 Web:
https://www.clerew.man.ac.uk
Email:
c...@clerew.man.ac.uk Snail-mail: Apt 40, SK8 5BF, U.K.
PGP: 2C15F1A9 Fingerprint: 73 6D C2 51 93 A0 01 E7 65 E8 64 7E 14 A4 AB A5