[Oberon] Modulus on negative number

Jörg joerg.straube at iaeth.ch
Sun May 14 08:51:07 CEST 2017


Hi Chris

In his note Wirth only reasons why REM and MOD (with positive y) are different for negative x. The example he brought up is a circular buffer, where (in-out) can get negative but MOD should always be positive as it's the index in the buffer. The result of REM would be negative for negative x

There is absolutely no hint why he did not define MOD for negative y. Most probably as he didn't see a need for it. 
In my previous mail I did not consider how I implemented MOD for negative y, I only mentioned what the result is / should be if it were.

br
Jörg

> Am 14.05.2017 um 05:40 schrieb Chris Burrows <chris at cfbsoftware.com>:
> 
> Yes - one could if one wanted to if they were designing a programming language but there are at least two possible negative consequences they should weigh up: 
> 
> a) Enforcing conformity with that requirement could have a negative effect on the performance on some platforms of the much more common cases of DIV and MOD with positive y. 
> 
> b) The definition could be inconsistent with the definitions used in other common programming languages. The resulting confusion could be the source of additional programming errors.
> 
> With reference to Modula-2, Wirth commented on DIV and MOD in a letter dated January 17, 1986 which was published in Issue #5 of Modus News, the Newsletter for the Modula-2 Users Association. You can download a copy from here:
> 
> http://www.cfbsoftware.com/Modula2/Modus.aspx
> 
> With reference to Oberon-07, note that DIV is NOT defined for *any* x and y. DIV (like MOD) is only defined for positive y in Oberon 07. The quotient is constrained similarly to the remainder:
> 
> <quote>
> The operators DIV and MOD apply to integer operands only. Let q = x DIV y, and r = x MOD y.
> Then quotient q and remainder r are defined by the equation
> 
>   x = q*y + r           0 <= r < y
> 
> </quote>
> 
> Consequently, the Astrobe for Cortex-M and Project Oberon RISC5 compilers will result in a 'divisor must be > 0' or 'bad divisor' compilation error message for both statements in the following example:
> 
>  i := 10 DIV (-2);
>  i := -10 DIV (-2);
> 
> Regards, 
> Chris
> 
>> -----Original Message-----
>> From: Oberon [mailto:oberon-bounces at lists.inf.ethz.ch] On Behalf Of
>> Claudio Nieder
>> Sent: Saturday, 13 May 2017 11:11 PM
>> To: ETH Oberon and related systems
>> Subject: Re: [Oberon] Modulus on negative number
>> 
>> Hi,
>> 
>> One could say that r must be within 0 and y, or to state it more
>> formally
>> 
>> 0<=r<y for y>0
>> y<r<=0 for y<0.
>> 
>> This gives the possibility to have a remainder also for negative y so
>> that the condition x = (x DIV y) * y + (x MOD y) is satisfied.
>> 
>> 1 DIV -12 = -1; 1 MOD -12=-11 satisfying the condition 1 = (-1) * (-
>> 12) + (-11)
>> 
>> -1 DIV -12 = 0; -1 MOD 12= -1 satisfying the condition -1 = 0 * (-12)
>> + (-1)
>> 
>> Without this extension when y is negative then x DIV y is defined but
>> x MOD y not.
>> 
>> But as you say, in his definitions of Oberon as well as of Modula-2
>> by stating 0<= r < y Wirth actually created this situation where DIV
>> is defined for any x and y but MOD is not.
>> 
>> claudio
>> 
>>> On 13. May. 2017, at 14:26, Chris Burrows <chris at cfbsoftware.com>
>> wrote:
>>> 
>>> 'completeness'? 'should be'? That statement needs some sort of
>> context to have any validity.
>>> 
>>> It is not true in the context of Oberon-07. As Peter has already
>> stated - results for negative divisors are not defined in the
>> language. The Astrobe for Cortex-M and Project Oberon RISC5 compilers
>> will give you 'bad modulus' compilation errors for both statements in
>> the following example:
>>> 
>>> VAR
>>> i: INTEGER;
>>> ...
>>> ...
>>> i := 1 MOD (-12);
>>> i := -1 MOD (-12);
>>> 
>>> NOTE: If the parentheses are omitted they do not even parse as
>> valid expressions.
>>> 
>>> Regards,
>>> Chris Burrows
>>> CFB Software
>>> http://www.astrobe.com/RISC5
>>> 
>>> 
>>>> -----Original Message-----
>>>> From: Oberon [mailto:oberon-bounces at lists.inf.ethz.ch] On Behalf
>> Of
>>>> J rg
>>>> Sent: Saturday, 13 May 2017 6:43 PM
>>>> To: ETH Oberon and related systems
>>>> Subject: Re: [Oberon] Modulus on negative number
>>>> 
>>>> Hi
>>>> 
>>>> Just for completeness, the result should be
>>>>  1 MOD -12 = -11
>>>> -1 MOD -12 = -1
>>>> 
>>>> J rg
>>>> 
>>>>> Am 13.05.2017 um 10:12 schrieb Peter Matthias
>>>> <PeterMatthias at web.de>:
>>>>> 
>>>>> Agreed. However, -1 MOD -12 or 1 MOD -12 is not defined in
>> Oberon.
>>>>> 
>>>>> 
>>>>>> Am 12.05.2017 um 22:43 schrieb Aubrey McIntosh:
>>>>>> for -1 MOD 12, the mathematically correct answers which are
>>>>>> consistent with the language report, are q=-1, r=11.
>>>>>> 
>>>>>> This definition works very well, for example, to implement wrap
>>>>>> around strip chart displays.
>>>>>> 
>>>>>> 
>>>>>> On Fri, May 12, 2017 at 2:28 PM, Peter Matthias
>>>> <PeterMatthias at web.de
>>>>>> <mailto:PeterMatthias at web.de>> wrote:
>>>>>> 
>>>>>>  Warming up the thread to give supposedly correct answer:
>>>>>> 
>>>>>>  Am 16.02.2017 um 00:00 schrieb Peter Matthias:
>>>>>> 
>>>>>> 
>>>>>> 
>>>>>>      Am 15.02.2017 um 04:21 schrieb Srinivas Nayak:
>>>>>> 
>>>>>>          Dear All,
>>>>>> 
>>>>>>          Recently I come across modulus on a negative number.
>>>>>>          Will it produce a negative number or positive?
>>>>>>          Someone says both are correct!
>>>>>>          http://stackoverflow.com/a/4403556
>>>>>>          <http://stackoverflow.com/a/4403556>
>>>>>>          Which one is mathematically correct?
>>>>>>          Surprisingly different languages calculate it
>>>> differently even!
>>>>>>          What is Oberon's way?
>>>>>> 
>>>>>> 
>>>>>>      The theory was already answered. In practice, all compiler
>>>>>>      implementations I used (native X86, Shark, MIPS), give
>>>> wrong
>>>>>>      result when
>>>>>>      both, divident and divisor are negative. I fixed it just
>>>>>>      yesterday for
>>>>>>      all non x86 versions.
>>>>>> 
>>>>>> 
>>>>>>  I should have read the language report before making such
>>>> claims.
>>>>>> 
>>>>>>  Oberon Report says:
>>>>>> 
>>>>>>  "The operators DIV and MOD apply to integer operands only.
>> They
>>>> are
>>>>>>  related by the following formulas defined for any dividend x
>>>> and
>>>>>>  positive divisors y:
>>>>>>  x = (x DIV y) * y + (x MOD y)
>>>>>>  0 = (x MOD y) < y"
>>>>>> 
>>>>>>  Oberon07-Report says:
>>>>>> 
>>>>>>  "The operators DIV and MOD apply to integer operands only. Let
>>>> q = x
>>>>>>  DIV y, and r = x MOD y.
>>>>>>  Then quotient q and remainder r are defined by the equation
>>>>>>  x = q*y + r              0 <= r < y"
>>>>>> 
>>>>>>  Last statement obviously cannot be met if y is negative.
>>>>>> 
>>>>>>  So in short: Don't use DIV/MOD for negative divisors as the
>>>> result
>>>>>>  is not defined.
>>>>>> 
>>>>>>> From the implemtation point of view this perfectly makes sense
>>>> as
>>>>>>  negative divisors are seldom used and correction for DIV of
>> the
>>>>>>  usually stupid hardware implementation only takes 3 additional
>>>>>>  instructions compared to at least 6 for a complete definition.
>>>>>>  Simple SHIFT/AND instructions for power of 2 divisors easily
>>>>>>  outwight these 3 additional instructions.
>>>>>> 
>>>>>> 
>>>>>>  Peter
>>>>>> 
>>>>>>  --
>>>>>>  Oberon at lists.inf.ethz.ch <mailto:Oberon at lists.inf.ethz.ch>
>>>> mailing
>>>>>>  list for ETH Oberon and related systems
>>>>>>  https://lists.inf.ethz.ch/mailman/listinfo/oberon
>>>>>>  <https://lists.inf.ethz.ch/mailman/listinfo/oberon>
>>>>>> 
>>>>>> 
>>>>>> 
>>>>>> 
>>>>>> --
>>>>>> Aubrey McIntosh, Ph.D.
>>>>>> 1502 Devon Circle
>>>>>> Austin TX 78723
>>>>>> (512) 348-7401
>>>>>> 
>>>>>> 
>>>>>> 
>>>>>> --
>>>>>> Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related
>>>>>> systems https://lists.inf.ethz.ch/mailman/listinfo/oberon
>>>>>> 
>>>>> --
>>>>> Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related
>>>>> systems https://lists.inf.ethz.ch/mailman/listinfo/oberon
>>>> 
>>>> --
>>>> Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related
>>>> systems https://lists.inf.ethz.ch/mailman/listinfo/oberon
>>> 
>>> --
>>> Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related
>>> systems https://lists.inf.ethz.ch/mailman/listinfo/oberon
>> 
>> --
>> Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related
>> systems https://lists.inf.ethz.ch/mailman/listinfo/oberon
> 
> --
> Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related systems
> https://lists.inf.ethz.ch/mailman/listinfo/oberon



More information about the Oberon mailing list