Negative values
Hii,
· I am thinking of comparing the readings from my sensor to a preset reference value, and depending on the difference between the 2 values, the controller will determine an appropriate action. However, what will happen if the difference between the 2 values is a negative result ? Can Basic stamp 'recognise' the negative value ? For example I will like to have a algoritherm that goes something like : If value < = -1, GOSUB moveforward .. is it possible ?? Thank you.
Best regards
· I am thinking of comparing the readings from my sensor to a preset reference value, and depending on the difference between the 2 values, the controller will determine an appropriate action. However, what will happen if the difference between the 2 values is a negative result ? Can Basic stamp 'recognise' the negative value ? For example I will like to have a algoritherm that goes something like : If value < = -1, GOSUB moveforward .. is it possible ?? Thank you.
Best regards
![roll.gif](http://forums.parallax.com/images/smilies/roll.gif)
Comments
The simple answer is - if you check your input values BEFORE doing any calculations, you can ensure that your result will always be positive. Ranging your values (upwards) may be required as well, just to ensure positive result numbers. What you do after the fact to get your final answer, is another matter entirely.
Although there is some intelligent processing of negative numbers with the PBASIC Stamp, you get into the area of two's complement notation, which becomes a quagmire for some folks, and thus should be avoided unless you're familiar with how they operate, and how to manipulate them.
Regards,
Bruce Bates
- Always use a Word sized variable
- Limits are +32,767 and -32,768
- When displaying, use SDEC
- If performing division, mod (//), and a few other math operations, this is the method I use:
BitSign var bit
BitSign = variable.Bit15 ' Save most significant bit, 1 = -, 0 = +
variable = (ABS variable) / 10 ' do math on absolute value
If BitSign = 1 THEN Variable = Variable * -1 ' re-apply sign by making negative
Hope this helps,
Martin
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
<i>Electronic Systems Technologies
Southern Illinois University Carbondale
Personal Links - Lot of BASIC Stamp info
and
SelmaWare Solutions
StampPlot Pro Version 3 Release 4
Graphical Data Acquisition for your Micro and Imagination!
Now allows additional controls to be added, or developed.
http://www.emesystems.com/BS2math4.htm#negatives
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com