Negative numbers in a lookup table?


Closed Thread
Results 1 to 7 of 7
  1. #1

    Default Negative numbers in a lookup table?

    Is it possible to have negative numbers in a lookup table? I'm trying to make a lookup table with negative numbers, but PBP doesn't seem to like it.

  2. #2
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: Negative numbers in a lookup table?

    Have you tried making it a long variable? Those are signed and I assume can be stored in an array.

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default Re: Negative numbers in a lookup table?

    BYTES and WORDS aren't treated as signed variables which means things like IF A < 0 won't work properly but you can still do A = -3 and if you're putting the value on a LCD or something using the SDEC modifier properly handles a negative value.

    If PBP doesn't allow you to put -3 in a lookuptabe simply put 253 in there instead (or 65533 if it's a WORD).

    /Henrik.

  4. #4
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: Negative numbers in a lookup table?

    Why sure you can. If the number is between +32767 and -32767 then all you need to do is look at the most significant bit (bit 15) to see if it is positive (bit 15 will be 0) and negative (bit 15 will be 1). Then you can use LOOKUP2 which works with words as output variables. It's in the manual.
    Dave Purola,
    N8NTA
    EN82fn

  5. #5
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Negative numbers in a lookup table?

    Pretty much any variable is signed if you want. A byte can be 0-126, and anything higher, subtract 126 from it, print with a “-“ in front of it,
    and is also the value of the negative number.

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default Re: Negative numbers in a lookup table?

    But that's not really "standard" is it? I'd probably stick to the normal two's complement standard.
    Subtract the value from 256 (if the variable is byte sized) and print a - in front. But really, for presentation purposes etc PBP already supports that with the SDEC modifier.

    If you need to multiply two numbers (A) and (B) where one (B) can be negative then
    Code:
    A VAR WORD
    B VAR WORD
    Sign VAR BIT
    
    A = 10
    B = -5
    
    Sign = B.15
    Result = A * ABS(B)
    Result.15 = Sign
    LCDOUT SDEC Result

  7. #7


    Did you find this post helpful? Yes | No

    Default Re: Negative numbers in a lookup table?

    I had to do that, and just used an offset to make them all positive. When it comes time to use the data, I then subtract the offset from the stored number if the stored number is MORE than the offset, OR subtract the number from the OFFSET and set a "negative" flag if the stored number is LESS than the offset.

    Say the range is -50 to +70. Add 50, shifting the range to 0-120. Use these "revised" values for your lookup.
    Now when you do the lookup, you get a value V

    If V > 49 then
    V=V-50
    NEGFLAG=0
    else
    V=50-V 'calculates the difference to restore the original data
    NEGFLAG=1 'sets the negative flag so you can act upon it with calculations if needed
    endif

Similar Threads

  1. Lookup table not accepting strings
    By lerameur in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 17th January 2011, 21:41
  2. How do you create a lookup table?
    By AlexanderWinnig in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 12th November 2010, 14:37
  3. Lookup table syntax.....
    By TerdRatchett in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 19th May 2009, 04:45
  4. Lookup table or Math?
    By ronjodu in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 2nd May 2008, 17:55
  5. Lookup Table
    By yasser hassani in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 11th March 2008, 10:38

Members who have read this thread : 1

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