View Full Version : Negative numbers in a lookup table?
keithv
- 16th September 2016, 17:42
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.
AvionicsMaster1
- 16th September 2016, 22:38
Have you tried making it a long variable? Those are signed and I assume can be stored in an array.
HenrikOlsson
- 17th September 2016, 12:40
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.
Dave
- 17th September 2016, 19:43
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.
Art
- 20th September 2016, 13:44
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.
HenrikOlsson
- 20th September 2016, 14:06
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
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
picster
- 25th September 2016, 14:58
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
Powered by vBulletin® Version 4.1.7 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.