How to prevent variable going below zero?


Closed Thread
Results 1 to 28 of 28

Hybrid View

  1. #1
    Join Date
    Dec 2010
    Posts
    409


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    Quote Originally Posted by CuriousOne View Post
    Picbasic has no idea about negative numbers, anything goes below 0 gets deducted from 65535.
    Re-read the single line I sent. I was not talking about PBP. I'm talking about representation of integers in a binary system. Read this article: https://www.cs.cornell.edu/~tomf/not.../twoscomp.html What most software developers do when they want to know when the variable goes "below zero", is look at the most significant bit. If it is high, the number is negative, if it is low, the number is positive. Since you are using a word, then your largest positive number will be 32768.

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


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    And since here is PBP forum, not general representation forum, I'm asking how to deal with this in PBP

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    Will this work for you?

    iflag var bit
    let iflag =0

    reader:
    adcin 0,Z
    pause 100
    IF Z>100 AND Z<130 THEN I=I+5
    IF Z<100 and iflag = 0 THEN I=I-5 'only if iflag = 0 it's OK to substract
    if i>255 then i=255
    if i<5 then i=0
    if i = 0 then let iflag = 1 'if i hits 0 put up a flag
    if i >= 5 then let iflag = 0 'once i hits 5 or more, reset the flag
    HPWM 2,I,10000
    XY=I
    gosub decoder
    goto reader

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    Try this:

    if Z.15 = 1 then
    Z = 0
    endif
    Last edited by Dick Ivers; - 22nd June 2020 at 20:09.

  5. #5
    Join Date
    Aug 2011
    Posts
    457


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    In your original code, change
    Code:
    IF Z<100 THEN I=I-5
    to this
    Code:
    IF Z<100 AND I>=5 THEN I=I-5

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, 17:55
  2. Variable within Variable
    By munromh in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 8th January 2009, 14:44
  3. Electrical Current Does Prevent Corrosion?
    By T.Jackson in forum Off Topic
    Replies: 13
    Last Post: - 18th March 2008, 13:13
  4. One variable made up of another variable
    By DynamoBen in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 1st September 2007, 00:18
  5. 2 variable
    By isaac in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 24th May 2004, 21: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