home *** CD-ROM | disk | FTP | other *** search
- (***************************************************************************
-
- $RCSfile: Reals.mod $
- Description: Low-level floating point conversions
-
- Created by: fjc (Frank Copeland)
- $Revision: 1.3 $
- $Author: fjc $
- $Date: 1994/08/08 16:40:34 $
-
- Copyright © 1994, Frank Copeland.
- This file is part of the Oberon-A Library.
- See Oberon-A.doc for conditions of use and distribution.
-
- Log entries are at the end of the file.
-
- ***************************************************************************)
-
- MODULE Reals;
-
- (*
- ** $C= CaseChk $I= IndexChk $L+ LongAdr $N= NilChk
- ** $P= PortableCode $R= RangeChk $S= StackChk $T= TypeChk
- ** $V= OvflChk $Z= ZeroVars
- *)
-
- IMPORT SYS := SYSTEM;
-
- (*------------------------------------*)
- PROCEDURE Expo* (x : REAL) : INTEGER;
- (*
- * This procedure extracts the exponent part of a REAL value. Quoting from
- * the RKM:Libraries, 3rd Ed, p834:
- *
- * "The exponent is the power of two needed to correctly position the
- * mantissa to reflect the number's true arithmetic value. It is held in
- * excess-64 notation, which means that the two's-complement values are
- * adjusted upward by 64, thus changing $40 (-64) through $3F (+63) to $00
- * through $7F..."
- *
- * The exponent occupies bits 0-6 of the 32 bits of the value.
- *)
-
- BEGIN (* Expo *)
- RETURN SHORT (SYS.VAL (LONGINT, x) MOD 128)
- END Expo;
-
- (*------------------------------------*)
- PROCEDURE ExpoL* (x : LONGREAL) : INTEGER;
-
- BEGIN (* ExpoL *)
- RETURN Expo (SHORT (x))
- END ExpoL;
-
- (*------------------------------------*)
- PROCEDURE SetExpo* (e : INTEGER; VAR x : REAL);
- (*
- * This procedure sets the exponent part of a REAL variable. It clears bits
- * 0-6 using SYS.AND() and ORs the exponent onto the cleared area.
- *
- * Broken down into simple expressions, the algorithm is:
- * i := SYS.VAL (LONGINT, x);
- * i := SYS.AND (i, 0FFFFFF80H);
- * i := SYS.LOR (i, e MOD 128);
- * x := SYS.VAL (REAL, i)
- *)
-
- BEGIN (* SetExpo *)
- x :=
- SYS.VAL
- ( REAL,
- SYS.LOR
- ( SYS.AND ( SYS.VAL (LONGINT, x), 0FFFFFF80H ),
- LONG (e) MOD 128 ) )
- END SetExpo;
-
- (*------------------------------------*)
- PROCEDURE SetExpoL* (e : INTEGER; VAR x : LONGREAL);
-
- VAR y : REAL;
-
- BEGIN (* SetExpoL *)
- y := SHORT (x); SetExpo (e, y); x := LONG (y)
- END SetExpoL;
-
- (*------------------------------------*)
- PROCEDURE Ten* (e : INTEGER) : REAL;
-
- VAR result : REAL; n : INTEGER;
-
- BEGIN (* Ten *)
- result := 1.0; n := ABS (e);
- WHILE n > 0 DO result := result * 10.0; DEC (n) END;
- IF e >= 0 THEN
- RETURN result
- ELSE
- RETURN 1.0 / result
- END;
- END Ten;
-
- (*------------------------------------*)
- PROCEDURE TenL* (e : INTEGER) : LONGREAL;
-
- BEGIN (* TenL *)
- RETURN LONG (Ten (e))
- END TenL;
-
- (*------------------------------------*)
- PROCEDURE Convert* (x : REAL; n : INTEGER; VAR d : ARRAY OF CHAR);
- (*
- * Converts a REAL into a string. d will contain the n most significant
- * digits of x, in REVERSE order.
- *)
-
- VAR i : LONGINT;
-
- BEGIN (* Convert *)
- i := 0;
- REPEAT
- d [i] := CHR (ENTIER (x) MOD 10 + 30H); x := x / 10; INC (i)
- UNTIL i = n;
- END Convert;
-
- (*------------------------------------*)
- PROCEDURE ConvertL* (x : LONGREAL; n : INTEGER; VAR d : ARRAY OF CHAR);
-
- BEGIN (* ConvertL *)
- Convert (SHORT (x), n, d)
- END ConvertL;
-
- (*------------------------------------*)
- PROCEDURE ConvertH* (x : REAL; VAR d : ARRAY OF CHAR);
- (*
- * Converts a REAL into a hexadecimal string.
- *)
-
- VAR i, j, k : LONGINT;
-
- BEGIN (* ConvertH *)
- d [7] := 0X; (* This should cause an index trap if d is too small. *)
- (* $I- Turn off index checking now, since we know there is enough room. *)
- k := SYS.VAL (LONGINT, x);
- i := 8;
- REPEAT
- DEC (i);
- IF k # 0 THEN
- j := k MOD 10H; k := k DIV 10H;
- IF j < 10 THEN d [i] := CHR (j + 30H) ELSE d [i] := CHR (j + 37H) END
- ELSE
- d [i] := "0"
- END;
- UNTIL i = 0;
- (* $I= Set index checking to default. *)
- END ConvertH;
-
- (*------------------------------------*)
- PROCEDURE ConvertHL* (x : LONGREAL; VAR d : ARRAY OF CHAR);
-
- BEGIN (* ConvertHL *)
- ConvertH (SHORT (x), d)
- END ConvertHL;
-
- END Reals.
-
- (***************************************************************************
-
- $Log: Reals.mod $
- Revision 1.3 1994/08/08 16:40:34 fjc
- Release 1.4
-
- Revision 1.2 1994/05/12 20:45:18 fjc
- - Prepared for release
-
- # Revision 1.1 1994/01/15 21:39:12 fjc
- # Start of revision control
- #
- ***************************************************************************)
-
-