Detecting negative REALs: IEEE 'isSignMinus' function

Programming language questions
Post Reply
User avatar
Robert
Posts: 177
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Detecting negative REALs: IEEE 'isSignMinus' function

Post by Robert »

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:

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;
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).
Josef Templ
Posts: 262
Joined: Tue Sep 17, 2013 6:50 am

Re: Detecting negative REALs: IEEE 'isSignMinus' function

Post by Josef Templ »

IsNeg looks good to me.
As far as I remember the IEEE standard it should also work for -0.0, -INF, and negative NaN
because it simply tests the sign bit, which always exists.
It will not trap for NaN because it does not use the FPU but return the sign bit of the NaN value
depending on the NaN representation being used.

An open question is if -0.0 is a negative number or in other words
if the name IsNeg is appropriate.
The IEEE standard uses the name IsSignMinus possibly for avoiding the conflict with the definition of
"negative".

- Josef
User avatar
Robert
Posts: 177
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: Detecting negative REALs: IEEE 'isSignMinus' function

Post by Robert »

Josef Templ wrote:An open question is if -0.0 is a negative number or in other words
if the name IsNeg is appropriate.
The IEEE standard uses the name IsSignMinus possibly for avoiding the conflict with the definition of
"negative".
1 - The IEEE name isSignMinus seems to be quite clear.
2 - A mathematician would not describe the number 0 as being either positive or negative.
3 - The Intel documentation for FXAM says it detects the sign bit which has 2 states, positive or negative.

So, in that context, the name isNeg is ok (at least for private use).
Post Reply