[Oberon] RISC emulator and floating-point

Paul Onyschuk blink at bojary.koba.pl
Wed Mar 26 10:16:05 CET 2014


On Wed, 26 Mar 2014 01:22:34 +0100
Jan Verhoeven <jan at verhoeven272.nl> wrote:

> Normally you would make sure the dividend is > 0. To do this, you
> make a conditional like
> 
> IF  x < 0  THEN  sign := -1; x = -x  ELSE  sign := 1  END;
> 
> Do the normal conversion (which WILL terminate) and afterwards just do
> 
> q := q * sign
> 
> and you're done.

WriteInt() does almost exactly that right now.  You omitted most
important part from code I pasted:

(* Negating x results in integer overflow and x0 wraps
                                   around to -2147483648 *)
      ELSE DEC(n); x0 := -x

Just second variable x0 is introduced for negation / absolute value.  On
the other hand original x is used like a sign in your code snippet:

    IF x < 0 THEN Write(W, "-") END;

The problem is that -(-2147483648) is still -2147483648.  It is in a
way special number, more detailed example what is going on:

-(-2147483648) = -1 * (-2147483647 - 1) = 2147483647 + 1 = -2147483648

-- 
Paul Onyschuk



More information about the Oberon mailing list