Shop OBEX P1 Docs P2 Docs Learn Events
Negative Help! — Parallax Forums

Negative Help!

Commander_BobCommander_Bob Posts: 17
edited 2007-04-17 22:28 in BASIC Stamp
I have been trying to get my 3 Axis accelerometer to work with positive and negative numbers and use commands like > or < but if I do something like 1 > -1 it says false and when I use debug it says -1 = 65535. How can I make the BS2 know it is a negative number and use this code?

IF··Gforce < -110 THEN
HIGH 3
ELSE
LOW 3
ENDIF

Thanks

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-04-17 04:11
    The Stamp's native arithmetic is unsigned 16-bit. Addition and subtraction work with signed or unsigned numbers, but greater and less than are backwards. For your example, you could try:
    if (Gforce < 0) and (abs(Gforce) > 110) then
       high 3
    else
       low 3
    endif
    
    
  • metron9metron9 Posts: 1,100
    edited 2007-04-17 05:29
    Some additional negative number information.

    gforce VAR Word
    
    FOR gforce= 32758 TO 32778
     DEBUG DEC gforce," =  "
     IF gforce>32768 THEN DEBUG "-" ELSE DEBUG "+"
     DEBUG DEC ABS(gforce),CR
    NEXT
    



    The numbers in a word variable range from 0 to 65535. When you want to think about negative numbers using a word variable you split the 65535 in half. You then have 32768 positive numbers and 32768 negative numbers. Remember zero is one of the positive numbers so really 65536/2 so the range is 0 thru 32767 positive and 32768 thru 65535 represent negative numbers.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!
  • Commander_BobCommander_Bob Posts: 17
    edited 2007-04-17 22:16
    Thanks. I tried this and it works too.

    sign = zg.BIT15


    IF (sign = 0) AND (ABS(zg) > 0) THEN
    HIGH 3
    ELSE
    LOW 3
    ENDIF
  • Mike GreenMike Green Posts: 23,101
    edited 2007-04-17 22:28
    Yes, your statement is true for numbers from 1 to 32767 and false for zero and negative numbers.
Sign In or Register to comment.