Real.mesa
Copyright Ó 1985, 1986, 1987, 1988, 1991 by Xerox Corporation. All rights reserved.
For Portable Cedar
Stewart, September 1, 1982 10:03 am
Rovner, May 13, 1983 1:13 pm
Levin, August 8, 1983 4:15 pm
Russ Atkinson (RRA) February 19, 1985 5:03:31 pm PST
Carl Hauser, February 10, 1988 10:10:14 am PST
Doug Wyatt, August 27, 1991 1:49 pm PDT
Operations on REAL numbers.
See IEEE floating point standard for more information.
Real:
CEDAR
DEFINITIONS =
BEGIN
FScale: PROC [a: REAL, scale: INTEGER] RETURNS [REAL]; -- a*(2scale)
Rounding
Fix:
PROC [
REAL]
RETURNS [
INT];
... rounds toward zero (mode rz).
Round:
PROC [
REAL]
RETURNS [
INT];
... rounds to nearest, unbiased (mode rn).
Ceiling:
PROC [
REAL]
RETURNS [
INT];
... rounds toward plus infinity (mode rp).
Floor:
PROC [
REAL]
RETURNS [
INT];
... rounds toward minus infinity (mode rm).
Decimal conversion
MaxSinglePrecision:
CARDINAL = 9;
# of decimal places needed to always exactly reproduce the given real number.
DefaultSinglePrecision:
CARDINAL = 7;
# of decimal places that are normally fully significant
PairToReal:
PROC [fr:
INT, exp10:
INTEGER]
RETURNS [
REAL];
... converts the value fr*10**exp10 to real.
RealToPair:
PROC [r:
REAL, precision:
NAT ¬ DefaultSinglePrecision]
RETURNS [type: NumberType, fr:
INT, exp10:
INTEGER];
... converts value r to fr*10**exp10; fr will have precision significant digits.
Constants
PlusZero: REAL = LOOPHOLE[LONG[00000000000B]];
MinusZero: REAL = LOOPHOLE[20000000000B];
PlusInfinity: REAL = LOOPHOLE[17740000000B];
MinusInfinity: REAL = LOOPHOLE[37740000000B];
LargestNumber: REAL = LOOPHOLE[17737777777B]; -- almost infinity
SmallestNormalizedNumber: REAL = LOOPHOLE[00040000000B];
NonTrappingNaN: REAL = LOOPHOLE[17740000001B];
TrappingNaN:
REAL =
LOOPHOLE[17740000002B];
You may want to use TrappingNaN to initialize storage.
TrapNonTrappingNaN: CARD = LONG[1];
TrapTrappingNaN: CARD = LONG[2];
AddInfinityNaN: CARD = LONG[3];
MultiplyInfinityNaN: CARD = LONG[4];
DivideInfinityNaN: CARD = LONG[5];
SqRtNaN:
CARD =
LONG[6];
These values may be encountered as vp.frac during an exception.
Exceptions
Flag:
TYPE =
BOOL ¬
FALSE;
Exception:
TYPE =
MACHINE
DEPENDENT{
fixOverflow, inexactResult, invalidOperation, divisionByZero, overflow, underflow};
ExceptionFlags:
TYPE =
PACKED
ARRAY Exception
OF Flag;
NoExceptions: ExceptionFlags = ALL[FALSE];
AllExceptions: ExceptionFlags = ALL[TRUE];
UsualExceptions: ExceptionFlags = [
fixOverflow: TRUE,
invalidOperation: TRUE,
divisionByZero: TRUE,
overflow: TRUE
];
NumberType:
TYPE =
MACHINE
DEPENDENT { normal, zero, infinity, nan };
Extended:
TYPE =
RECORD [type: NumberType, sign:
BOOL, exp:
INTEGER, frac:
CARD];
Extended is the internal form of a single precision floating point number. If the type of a value is infinity or zero, only the sign is interesting. In these cases, exp and frac are undefined. If type = nan, then exp is undefined and frac contains the nan significand. Some constant nans are defined above. If type = normal, then sign, exp, and frac describe the value: if sign is true, then the number is negative; exp is the binary exponent; frac is the significand (the binary point is between bits 0 and 1 - normalized numbers are between 1 and 2).
RealException: SIGNAL [flags: ExceptionFlags, vp: REF Extended]
RETURNS [clientFixup: BOOL ¬ FALSE];
RealError:
ERROR;
RealException is raised if any enabled exception occurs. Flags reports all the exceptions which occurred during the faulted operation (including those which are disabled). On RESUME, if clientFixup is TRUE, the client is expected to have fixed up the reference. If clientFixup is FALSE the standard fixup will happen. Operations in this interface only raise RealException on the conditions mentioned in UsualExceptions, above. Usually, RealException can be resumed, but certain exceptions, such as invalidOperation raised as a result of compare or one of the Fixes, cannot be resumed. If a RESUME is done in such a case, the ERROR RealError is raised.
GetStickyFlags: PROC RETURNS [ExceptionFlags];
SetStickyFlags:
PROC [new: ExceptionFlags ¬ NoExceptions]
RETURNS [old: ExceptionFlags];
The six kinds of exceptions have independent "sticky" flags that remember if the exception has occurred since the last call to SetStickyFlags. SetStickyFlags is provided so that procedures may save and restore the state for others.
END.
4-Feb-81 18:42:11, L. Stewart, fixed TrappingNaN to be of type REAL
June 3, 1982 10:36 am, L. Stewart, added REF, SqRt, FScale, removed STRING
September 1, 1982 10:03 am, L. Stewart, CompareREAL, Cedar 3.4
May 13, 1983 1:13 pm, Paul Rovner, conversion to Cedar 5.0
Russ Atkinson (RRA) February 19, 1985 5:02:31 pm PST
General cleanup, especially to improve readability
Doug Wyatt, February 25, 1985 2:17:58 pm PST
More cleanup. Removed initialization stuff.
Doug Wyatt, December 11, 1986 2:03:29 pm PST
Removed FixLI and RoundLI (use Fix and Round).
Carl Hauser, January 20, 1988 3:13:21 pm PST
Removed all the machine code procedures for Portable Cedar version