How to prevent variable going below zero?


Closed Thread
Results 1 to 28 of 28

Hybrid View

  1. #1
    Join Date
    Aug 2011
    Posts
    460


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    if you want your number staying between 0 and 255 ... use a BYTE, instead of a WORD
    That's his problem... he DOES want to use a byte but as he adds or subtracts to the byte value it wraps and gives false operation.

    The way I usually handle this sort of thing is to have a routine that does the addition or subtraction with a temp copy of the variable, and only update the variable if the operation is valid.

    You don't have to detect a negative number... what you want to know is if you add X to Y will it be < Y (wrapped 255) and if you subtract X from Y will it be > Y (wrapped 0). Don't modify Y until you know the answer. You can have the routine limit the results to 255 or 0 if that's how you want it to work, or ignore the operation.

  2. #2
    Join Date
    Feb 2013
    Posts
    1,158


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    So this "wrapping" is compiler related issue and can't be fixed, as I understand?

  3. #3
    Join Date
    Feb 2013
    Posts
    1,158


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    Technically, I solved this problem by making I change between 5 and 260, and outputting HPWM 2,I-5,10000.

  4. #4
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,172


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    If you mean a compiler problem, no. It is not.

    If you mean how binary arithmetic works, yes. That is how you count, add or subtract in binary having byte, word or other size variables. In any case that is how you do it in decimal also but do not give it much attention.

    The "fix" is in the answers given already to you. SELECT CASE, test before updating for valid result, etc...

    Ioannis

Similar Threads

  1. Assigning a string variable to another string variable
    By Christopher4187 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 2nd June 2013, 18:55
  2. Variable within Variable
    By munromh in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 8th January 2009, 15:44
  3. Electrical Current Does Prevent Corrosion?
    By T.Jackson in forum Off Topic
    Replies: 13
    Last Post: - 18th March 2008, 14:13
  4. One variable made up of another variable
    By DynamoBen in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 1st September 2007, 01:18
  5. 2 variable
    By isaac in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 24th May 2004, 22:59

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts