<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Other languages have avoided the problem of undefined values by defining a remainder rather than a modulo operator. For example, in Swift (*) we have:</div><div class=""><br class=""></div><div class=""><i class="">"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:</i></div><div class=""><i class=""><br class=""></i></div><div class=""><i class="">          a = (b x some multiplier) + remainder<br class=""><br class="">where some multiplier is the largest number of multiples of b that will fit inside a.</i></div><div class=""><i class=""><br class=""></i></div><div class=""><i class="">The sign of b is ignored for negative values of b. This means that a % b and a % -b always give the same answer.</i></div><div class=""><i class=""><br class=""></i></div><div class=""><i class="">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."</i></div><div class=""><br class=""></div><div class="">Examples:</div><div class=""><br class=""></div><div class=""> 1 % -12   =   1 % 12   =   1</div><div class="">-1 % -12   =  -1 % 12   =  -1</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">(*) <a href="https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/BasicOperators.html#//apple_ref/doc/uid/TP40014097-CH6-ID60" class="">https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/BasicOperators.html#//apple_ref/doc/uid/TP40014097-CH6-ID60</a></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">——————————————</div><div class=""><br class=""></div><div class=""><span style="white-space: pre-wrap; background-color: rgb(255, 255, 255);" class="">> Jörg <a href="mailto:joerg.straube@iaeth.ch" class="">joerg.straube@iaeth.ch</a> Sat May 11 11:13:19 CEST 2017</span></div><div class=""><pre style="white-space: pre-wrap; background-color: rgb(255, 255, 255);" class="">> Hi,</pre><pre style="white-space: pre-wrap; background-color: rgb(255, 255, 255);" class="">> Just for completeness, the result should be</pre><pre style="white-space: pre-wrap; background-color: rgb(255, 255, 255);" class="">>   1 MOD -12 = -11
>  -1 MOD -12 = -1</pre><pre style="white-space: pre-wrap; background-color: rgb(255, 255, 255);" class="">><i class=""> </i><br class="">> Jörg

><i class=""> Am 13.05.2017 um 10:12 schrieb Peter Matthias <<a href="https://lists.inf.ethz.ch/mailman/listinfo/oberon" class="">PeterMatthias at web.de</a>>:
</i>><i class=""> 
</i>><i class=""> Agreed. However, -1 MOD -12 or 1 MOD -12 is not defined in Oberon.
</i>><i class=""> 
</i>><i class=""> 
</i>><i class=""> Am 12.05.2017 um 22:43 schrieb Aubrey McIntosh:
</i>>><i class=""> for -1 MOD 12, the mathematically correct answers which are consistent
</i>>><i class=""> with the language report, are
</i>>><i class=""> q=-1, r=11.
</i>>><i class=""> 
</i>>><i class=""> This definition works very well, for example, to implement wrap around
</i>>><i class=""> strip chart displays.
</i>>><i class=""> 
</i>>><i class=""> 
</i>>><i class=""> On Fri, May 12, 2017 at 2:28 PM, Peter Matthias <<a href="https://lists.inf.ethz.ch/mailman/listinfo/oberon" class="">PeterMatthias at web.de</a>
</i>>><i class=""> <mailto:<a href="https://lists.inf.ethz.ch/mailman/listinfo/oberon" class="">PeterMatthias at web.de</a>>> wrote:
</i>>><i class=""> 
</i>>><i class="">    Warming up the thread to give supposedly correct answer:
</i>>><i class=""> 
</i>>><i class="">    Am 16.02.2017 um 00:00 schrieb Peter Matthias:
</i>>><i class=""> 
</i>>><i class=""> 
</i>>><i class=""> 
</i>>><i class="">        Am 15.02.2017 um 04:21 schrieb Srinivas Nayak:
</i>>><i class=""> 
</i>>><i class="">            Dear All,
</i>>><i class=""> 
</i>>><i class="">            Recently I come across modulus on a negative number.
</i>>><i class="">            Will it produce a negative number or positive?
</i>>><i class="">            Someone says both are correct!
</i>>><i class="">            <a href="http://stackoverflow.com/a/4403556" class="">http://stackoverflow.com/a/4403556</a>
</i>>><i class="">            <<a href="http://stackoverflow.com/a/4403556" class="">http://stackoverflow.com/a/4403556</a>>
</i>>><i class="">            Which one is mathematically correct?
</i>>><i class="">            Surprisingly different languages calculate it differently even!
</i>>><i class="">            What is Oberon's way?
</i>>><i class=""> 
</i>>><i class=""> 
</i>>><i class="">        The theory was already answered. In practice, all compiler
</i>>><i class="">        implementations I used (native X86, Shark, MIPS), give wrong
</i>>><i class="">        result when
</i>>><i class="">        both, divident and divisor are negative. I fixed it just
</i>>><i class="">        yesterday for
</i>>><i class="">        all non x86 versions.
</i>>><i class=""> 
</i>>><i class=""> 
</i>>><i class="">    I should have read the language report before making such claims.
</i>>><i class=""> 
</i>>><i class="">    Oberon Report says:
</i>>><i class=""> 
</i>>><i class="">    "The operators DIV and MOD apply to integer operands only. They are
</i>>><i class="">    related by the following formulas defined for any dividend x and
</i>>><i class="">    positive divisors y:
</i>>><i class="">    x = (x DIV y) * y + (x MOD y)
</i>>><i class="">    0 ≤ (x MOD y) < y"
</i>>><i class=""> 
</i>>><i class="">    Oberon07-Report says:
</i>>><i class=""> 
</i>>><i class="">    "The operators DIV and MOD apply to integer operands only. Let q = x
</i>>><i class="">    DIV y, and r = x MOD y.
</i>>><i class="">    Then quotient q and remainder r are defined by the equation
</i>>><i class="">    x = q*y + r              0 <= r < y"
</i>>><i class=""> 
</i>>><i class="">    Last statement obviously cannot be met if y is negative.
</i>>><i class=""> 
</i>>><i class="">    So in short: Don't use DIV/MOD for negative divisors as the result
</i>>><i class="">    is not defined.
</i>>><i class=""> 
</i>>><i class="">    >From the implemtation point of view this perfectly makes sense as
</i>>><i class="">    negative divisors are seldom used and correction for DIV of the
</i>>><i class="">    usually stupid hardware implementation only takes 3 additional
</i>>><i class="">    instructions compared to at least 6 for a complete definition.
</i>>><i class="">    Simple SHIFT/AND instructions for power of 2 divisors easily
</i>>><i class="">    outwight these 3 additional instructions.
</i>>><i class=""> 
</i>>><i class=""> 
</i>>><i class="">    Peter
</i>>><i class=""> 
</i>>><i class="">    --
</i>>><i class="">    <a href="https://lists.inf.ethz.ch/mailman/listinfo/oberon" class="">Oberon at lists.inf.ethz.ch</a> <mailto:<a href="https://lists.inf.ethz.ch/mailman/listinfo/oberon" class="">Oberon at lists.inf.ethz.ch</a>> mailing
</i>>><i class="">    list for ETH Oberon and related systems
</i>>><i class="">    <a href="https://lists.inf.ethz.ch/mailman/listinfo/oberon" class="">https://lists.inf.ethz.ch/mailman/listinfo/oberon</a>
</i>>><i class="">    <<a href="https://lists.inf.ethz.ch/mailman/listinfo/oberon" class="">https://lists.inf.ethz.ch/mailman/listinfo/oberon</a>>
</i>>><i class=""> 
</i>>><i class=""> 
</i>>><i class=""> 
</i>>><i class=""> 
</i>>><i class=""> --
</i>>><i class=""> Aubrey McIntosh, Ph.D.
</i>>><i class=""> 1502 Devon Circle
</i>>><i class=""> Austin TX 78723
</i>>><i class=""> (512) 348-7401
</i>>><i class=""> 
</i>>><i class=""> </i></pre><div class=""><br class=""></div></div></body></html>