Detecting negative REALs: IEEE 'isSignMinus' function
Posted: Thu May 18, 2017 7:20 pm
What is the best way to check if a REAL is negative? I want to be able to detect -0, and preferably not crash wuth NaNs.
Two ways I have used in the past are:
I would be interested in (gradually) building up a library of the IEEE 754 "Operations" required for 754 compliant implementations (which BlackBox is very far from being). The list is in section 5 of the IEEE 754 standard. If anyone wants to contribute I am happy to collect these operations, and when the collection is of a reasonable size to publish it (maybe on CPC, maybe it could be incorporated as a new standard module).
Two ways I have used in the past are:
Code: Select all
PROCEDURE [code] FLD (x: REAL);
PROCEDURE [code] FXAM 0D9H, 0E5H;
PROCEDURE [code] FSTPst0 0DDH, 0D8H; (* remove st[0] *)
PROCEDURE [code] FSWs (): SET 0DFH, 0E0H;
PROCEDURE IsNeg_ (x : REAL) : BOOLEAN;
VAR
isNeg : BOOLEAN;
BEGIN
FLD(x); FXAM; isNeg := 9 IN FSWs(); FSTPst0; RETURN isNeg
END IsNeg_;
PROCEDURE IsNeg (x : REAL) : BOOLEAN;
BEGIN
RETURN SYSTEM.VAL (LONGINT, x) < 0
END IsNeg;