Shop OBEX P1 Docs P2 Docs Learn Events
Negative values — Parallax Forums

Negative values

WILLQHWILLQH Posts: 11
edited 2005-12-22 15:41 in Learn with BlocklyProp
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 regardsroll.gif

Comments

  • Bruce BatesBruce Bates Posts: 3,045
    edited 2005-12-22 01:45
    WILLQH -

    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
  • Martin HebelMartin Hebel Posts: 1,239
    edited 2005-12-22 02:44
    Negative numbers work well, except for a few things you need to watch for:
    - 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.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-12-22 15:41
    Thanks Martin.· Great information!· You can also get some information at Tracy Allen's website by going to the following link:

    http://www.emesystems.com/BS2math4.htm#negatives

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
Sign In or Register to comment.