Closed Spivoxity closed 5 years ago
That is what the Oberon Report requires: see Sec 8.2.2 of this version:
https://www.inf.ethz.ch/personal/wirth/Oberon/Oberon.Report.pdf
It is different from what you get in many other programming languages, and what is provided by the DIV instruction on many machines.
In particular, the Java Language Specification very carefully specifies an interpretation where (-1) DIV 2 is 0 and (-1) MOD 2 is -1.
In my opinion, Oberon's interpretation is more useful, because it goes with the convention that (-1) MOD N = N-1, and so we can decrement a counter modulo N with
counter := (counter-1) MOD N
instead of having to write
counter := (counter+N-1) MOD N.
Just to complete the story: Wirth's specification leaves unsettled the question what happens when y is negative. I have chosen an interpretation that maintains the truth of the equation
(k*x) DIV (k*y) = x DIV y
even when k is negative: consequently, 1 DIV (-2) = (-1) DIV 2 = -1.
From email:
In a short program I was making, I ended up with
and I expected the result to be 0 but it remained -1.
Is this mathematically correct?
-- Jan Verhoeven