[Oberon] RISC emulator and floating-point
Peter De Wachter
pdewacht at gmail.com
Tue Mar 25 22:30:01 CET 2014
On 25-03-14 22:11, Paul Onyschuk wrote:
> I think I found issue. Right now in Oberon running under emulator:
>
> -1 DIV 10 = -1
> -1 MOD 10 = 9
>
> Is that indented behavior?
Yes, see the Oberon07 report, section 8.2.2:
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
> Now version of procedure WriteInt() with
> comments:
>
> PROCEDURE WriteInt* (VAR W: Writer; x, n: LONGINT);
> VAR i: INTEGER; x0: LONGINT;
> a: ARRAY 10 OF CHAR;
> BEGIN i := 0;
> (* Taking this branch, since x = 80000000H = -2147483648 *)
> IF x < 0 THEN
> IF x = 0 THEN WriteString(W, " -2147483648")
> (* Negating x results in integer overflow and x0 wraps
> around to -2147483648 *)
> ELSE DEC(n); x0 := -x
> END
> ELSE x0 := x
> END;
> REPEAT
> a[i] := CHR(x0 MOD 10 + 30H); x0 := x0 DIV 10; INC(i)
> (* Loop never terminates, since -1 DIV 10 = -1 and x0 is never 0 *)
> UNTIL x0 = 0;
> WHILE n > i DO Write(W, " "); DEC(n) END;
> IF x < 0 THEN Write(W, "-") END;
> REPEAT DEC(i); Write(W, a[i]) UNTIL i = 0
> END WriteInt;
The intention was obviously for -2147483648 to be a special case, so the
procedure should be rearranged into something like this:
PROCEDURE WriteInt* (VAR W: Writer; x, n: LONGINT);
VAR i: INTEGER; x0: LONGINT;
a: ARRAY 10 OF HAR;
BEGIN
IF x = 80000000H THEN WriteString(W, " -2147483648")
ELSE
(* the rest of the code *)
END
END WriteInt;
More information about the Oberon
mailing list