<div dir="ltr">for -1 MOD 12, the mathematically correct answers which are consistent with the language report, are<div>q=-1, r=11.  </div><div><br></div><div>This definition works very well, for example, to implement wrap around strip chart displays.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, May 12, 2017 at 2:28 PM, Peter Matthias <span dir="ltr"><<a href="mailto:PeterMatthias@web.de" target="_blank">PeterMatthias@web.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Warming up the thread to give supposedly correct answer:<span class=""><br>
<br>
Am 16.02.2017 um 00:00 schrieb Peter Matthias:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<br>
Am 15.02.2017 um 04:21 schrieb Srinivas Nayak:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Dear All,<br>
<br>
Recently I come across modulus on a negative number.<br>
Will it produce a negative number or positive?<br>
Someone says both are correct! <a href="http://stackoverflow.com/a/4403556" rel="noreferrer" target="_blank">http://stackoverflow.com/a/440<wbr>3556</a><br>
Which one is mathematically correct?<br>
Surprisingly different languages calculate it differently even!<br>
What is Oberon's way?<br>
</blockquote>
<br>
The theory was already answered. In practice, all compiler<br>
implementations I used (native X86, Shark, MIPS), give wrong result when<br>
both, divident and divisor are negative. I fixed it just yesterday for<br>
all non x86 versions.<br>
</blockquote>
<br></span>
I should have read the language report before making such claims.<br>
<br>
Oberon Report says:<br>
<br>
"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:<br>
x = (x DIV y) * y + (x MOD y)<br>
0 ≤ (x MOD y) < y"<br>
<br>
Oberon07-Report says:<br>
<br>
"The operators DIV and MOD apply to integer operands only. Let q = x DIV y, and r = x MOD y.<br>
Then quotient q and remainder r are defined by the equation<br>
x = q*y + r              0 <= r < y"<br>
<br>
Last statement obviously cannot be met if y is negative.<br>
<br>
So in short: Don't use DIV/MOD for negative divisors as the result is not defined.<br>
<br>
>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.<div class="HOEnZb"><div class="h5"><br>
<br>
Peter<br>
<br>
--<br>
<a href="mailto:Oberon@lists.inf.ethz.ch" target="_blank">Oberon@lists.inf.ethz.ch</a> mailing list for ETH Oberon and related systems<br>
<a href="https://lists.inf.ethz.ch/mailman/listinfo/oberon" rel="noreferrer" target="_blank">https://lists.inf.ethz.ch/mail<wbr>man/listinfo/oberon</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr">Aubrey McIntosh, Ph.D.<div>1502 Devon Circle</div><div>Austin TX 78723</div><div>(512) 348-7401</div><div><br></div></div></div>
</div>