Shop OBEX P1 Docs P2 Docs Learn Events
if Val <0 then val =0 (val reaches 0) returns incorrect answer — Parallax Forums

if Val <0 then val =0 (val reaches 0) returns incorrect answer

gstrekkiegstrekkie Posts: 2
edited 2009-02-11 15:54 in BASIC Stamp
I am run into a problem with this

Say i am decrementing a value that when it reaches zero I want it to remain zero

value=value-1
if value <=0 then Value = 0

what I get is
value = -1
returns
value= 15

if i try the min command I get the same result
also if i switch from declaring the variable from nib to byte the returned value will be 255 instead which is the largest value it will hold in both cases. what am i doing wrong as this isnt making sense to me any help will be appreciated.

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-02-10 20:19
    You're decrementing the value before checking it, so once it reaches zero it will end up wrapping back around before you catch it. If you want to decrement only until 0 I would suggest an approach such as this one:

    IF·value > 0 THEN·value =·value - 1

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Engineering
  • gstrekkiegstrekkie Posts: 2
    edited 2009-02-10 20:33
    That worked just fine it was driving me nuts thanks for your help!
  • vaclav_salvaclav_sal Posts: 451
    edited 2009-02-10 22:38
    So why it did not work with value <= 0 ?

    His value was < 0 or -1· or more precisely $F ( he did not state· value dismesion)

    Someting to do with internal value being 16 bits or better yet signed integer ?

    I just tryed this

    VS VAR Bit



    · VS = 0
    ··· DEBUG ? VS··············· prints VS = 0
    ··· DEBUG ? ~VS············· prints ~VS = 65535
    ··· DEBUG BIN1 ? ~VS······ prints ~VS = 1
    ··· DEBUG BIN16 ? ~VS···· prints ~VS = 1111111111111111

    ··· IF ~VS = 1 THEN
    ····· DEBUG "Debug ~VS = 0· " , ? VS skips debug· -· this should work!!!
    ··· ENDIF


    ··· IF ~VS = $FFFF THEN
    ····· DEBUG "Debug ~VS = $FFFF ", ? VS· prints ~VS = 0
    ··· ENDIF
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-02-10 22:49
    Comparisons use unsigned math, so -1 in this case (the way it is stored) is greater than 0. I hope this helps. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Engineering
  • vaclav_salvaclav_sal Posts: 451
    edited 2009-02-10 22:54
    No, it did not help.
    Are you saying·that I cannot use ~ (complement) and then compare??
    I do not have·to do it that way, but like to know the answer.
    ·
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-02-11 15:54
    I guess I don't understand exactly what you're asking. If you check to see if a negative number is less than a positive number your result will be that the negative number will be higher in most cases due to the way it is stored and the way the comparison is made (unsigned).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Engineering
Sign In or Register to comment.