Negative numbers in a lookup table?


Closed Thread
Results 1 to 7 of 7

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    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

  2. #2


    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