Shop OBEX P1 Docs P2 Docs Learn Events
Evaluating for Less Than Zero — Parallax Forums

Evaluating for Less Than Zero

JDOhioJDOhio Posts: 72
edited 2006-07-20 23:33 in General Discussion
I have found many methods to evaluate if a variable is less than zero but I hadn't found any topics here. Consider

myValue1 VAR Byte
myValue2 VAR Byte

myValue1=0
myValue2=0

DEC myValue2

IF myValue2 < myValue1 THEN
  'some code
ENDIF




I would prefer that the code inside the IF execute however I have not found it to be the case. The same is true if I substitute

myValue2=myValue2-1

for

DEC myValue2

My questions:

Can I use a combination of the Carry bit or Digital Carry bit to indicate that a value is less than zero?
Does this depend on CARRYX?
Is there an assembly instruction I could use to evalute the result (S/X B uses CJAE, I believe)?

Joe

Comments

  • BeanBean Posts: 8,129
    edited 2006-07-19 22:34
    Joe,
    A value is only negative (less than zero) if you choose to look at it that way.
    So with byte variables -1 is equal to 255. It just depends on what YOU want it to be.

    Normal signed values use the MSB to indicate if the value is negative.

    If the MSB is high, then the value is negative.

    It gets tricky to do the comparision because either one, none, or both of the value could be negative. And you must trap all conditions.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com

    "Remember, you are unique, just like everyone else." Unknown.
    ·
  • JDOhioJDOhio Posts: 72
    edited 2006-07-19 22:40
    Bean,

    You're right but I hadn't thought of it that way! (Too spoiled with client/server tools I bet)

    Thanks,

    Joe
  • Dave HeinDave Hein Posts: 6,347
    edited 2006-07-20 12:42
    Joe,

    You could use the following code:

    myValue1·VAR·Byte
    myValue2·VAR·Byte
    diff·VAR Byte

    myValue1=0
    myValue2=0

    DEC·myValue2

    diff·= myValue2 - myValue1

    IF·diff >= 128·THEN
    ··'some·code
    ENDIF
    The MSB of diff will be set if myValue2 is less than myValue1.

    Dave
  • JDOhioJDOhio Posts: 72
    edited 2006-07-20 23:33
    Cool! I was using two IF statements - one to check if the value is 255 and one to check if one value is less than the other. I much prefer a single line for the subtraction and a single IF statement. Much cleaner!

    Thanks!

    Joe
Sign In or Register to comment.