[Oberon] Modulus on negative number

Andreas Pirklbauer andreas_pirklbauer at yahoo.com
Sat May 13 12:01:49 CEST 2017


Other languages have avoided the problem of undefined values by defining a remainder rather than a modulo operator. For example, in Swift (*) we have:

"The remainder operator (a % b) works out how many multiples of b will fit inside a and returns the value that is left over (known as the remainder). To determine the answer for a % b, the % operator calculates the following equation and returns remainder as its output:

          a = (b x some multiplier) + remainder

where some multiplier is the largest number of multiples of b that will fit inside a.

The sign of b is ignored for negative values of b. This means that a % b and a % -b always give the same answer.

Note: The remainder operator (%) is also known as a modulo operator in other languages. However, its behavior in Swift for negative numbers means that it is, strictly speaking, a remainder rather than a modulo operation."

Examples:

 1 % -12   =   1 % 12   =   1
-1 % -12   =  -1 % 12   =  -1


(*) https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/BasicOperators.html#//apple_ref/doc/uid/TP40014097-CH6-ID60


——————————————

> Jörg joerg.straube at iaeth.ch Sat May 11 11:13:19 CEST 2017
> 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 <https://lists.inf.ethz.ch/mailman/listinfo/oberon>>:
> 
> 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 <https://lists.inf.ethz.ch/mailman/listinfo/oberon>
>> <mailto:PeterMatthias at web.de <https://lists.inf.ethz.ch/mailman/listinfo/oberon>>> 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>
>>            <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 <https://lists.inf.ethz.ch/mailman/listinfo/oberon> <mailto:Oberon at lists.inf.ethz.ch <https://lists.inf.ethz.ch/mailman/listinfo/oberon>> mailing
>>    list for ETH Oberon and related systems
>>    https://lists.inf.ethz.ch/mailman/listinfo/oberon <https://lists.inf.ethz.ch/mailman/listinfo/oberon>
>>    <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
>> 
>> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.inf.ethz.ch/pipermail/oberon/attachments/20170513/fdd62e64/attachment.html>


More information about the Oberon mailing list