home .. forth .. misc mail list archive ..

Re: nForth signed comparisons


This is one set of solutions. Pardon my use of C syntax in the stack
diagrams.

: 0=            ( n -- flag=n==0)
    IF 0 ELSE -1 THEN ;

: =             ( n1 n2 -- flag=n1==n2)
    XOR 0= ;

HEX
: 0<            ( n -- flag=n<0)
    80000000 AND IF -1 THEN ;

: <             ( n1 n2 -- flag=n1<n2)
    OVER OVER XOR 80000000 AND IF       ( if signs are different)
      DROP 0< ELSE                      ( then return sign of n1)
      - 0< THEN ;                       ( else return sign of n1-n2)

: >             ( n1 n2 -- flag=n1>n2)
    SWAP < ;

: <=            ( n1 n2 -- flag=n1<=n2)
    OVER OVER < ROT ROT = OR ;

: >=            ( n1 n2 -- flag=n1>=n2)
    SWAP <= ;