DIV and MOD with negative operands

Topics related to the use of Oberon language features
Locked
cfbsoftware
Site Admin
Posts: 493
Joined: Fri Dec 31, 2010 12:30 pm
Contact:

DIV and MOD with negative operands

Post by cfbsoftware » Sat Nov 15, 2014 6:46 am

The behaviour of DIV and MOD operators with negative operands in Astrobe for Cortex-M3/M4 did not conform to the Oberon language report in v5.1 and earlier versions. This has been fixed in v5.2 of both products.

The Oberon language report defines DIV and MOD as follows:
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

Code: Select all

x = q*y + r 
0 <= r < y
The following are examples of the new conforming behaviour:

Constants:

Code: Select all

q := 10 DIV -3;  (* Compile Error: divisor must be > 0 *)
r := 10 MOD -3;  (* Compile Error: modulus must be > 0 *)
q := (-10) DIV 3;  (* q = -4 *)
r := (-10) MOD 3;  (* r = 2 *)
Variables:

Code: Select all

x := 10;
y := -3;
q := x DIV y;  (* Runtime Error 7 *)
r := x MOD y;  (* Runtime Error 7 *)
Note that due to precedence rules, the following are identical statements:

Code: Select all

q := -10 DIV 3;   (* q = -3 *)
q := -(10 DIV 3); (* q = -3 *)
Also:

Code: Select all

r := -10 MOD 3;   (* r = -1 *)
r := -(10 MOD 3); (* r = -1 *)

Locked