- Code: Select all
`MODULE Fred;`

IMPORT SYSTEM;

PROCEDURE IsNaN* (s : SHORTREAL) : BOOLEAN; (* Detects non-signalling NaNs *)

CONST

nanS = {22 .. 30};

BEGIN

RETURN nanS - BITS (SYSTEM.VAL (INTEGER, s)) = {}

END IsNaN;

PROCEDURE Mantissa* (s : SHORTREAL) : SHORTREAL;

CONST

mask = {0 .. 19, 31};

zeroExp = {20 .. 29};

VAR

a : REAL;

exp, k, msh : INTEGER;

BEGIN

IF IsNaN (s) THEN RETURN 1.5

ELSIF s = 0. THEN RETURN 0.

ELSIF s = INF THEN RETURN 1.

ELSIF s = -INF THEN RETURN -1.

ELSE

a := ABS (s);

exp := ASH (SYSTEM.VAL (INTEGER, a), -23);

WHILE exp = 0 DO

s := s + s; a := ABS (s);

exp := SHORT (ASH (SYSTEM.VAL (INTEGER, a), -23))

END;

k := SYSTEM.VAL (INTEGER, s);

msh := ORD (BITS (SHORT (ASH (k, -32))) * mask + zeroExp);

RETURN SYSTEM.VAL (SHORTREAL, k)

END

END Mantissa;

END Fred.

The line that causes the problem is

- Code: Select all
`exp := ASH (SYSTEM.VAL (INTEGER, a), -23);`

a should be declared to be of type SHORTREAL, but this programming error should not cause a TRAP.