Shop OBEX P1 Docs P2 Docs Learn Events
Absolute value and word-byte math in SX/B — Parallax Forums

Absolute value and word-byte math in SX/B

ClintClint Posts: 95
edited 2007-02-12 20:28 in General Discussion
I have two questions:

First, is there a command in SX/B for returning the absolute value of a number?

Second, how do I perform math between word and byte sized variables? I have a word sized variable I would like to divide by 100 and store into a byte sized variable. When I try to do this it gives me the error "BYTE PARAMETER EXPECTED"

I want to do something like this:

·· numberX VAR Word
·· numberY VAR Byte

·· numberX = 300
·· numberY = numberX / 100


Thanks for any help. I am new to the SX coming from PBASIC·and a·Basic Stamp 2.

Post Edited (Clint) : 2/12/2007 5:55:08 PM GMT

Comments

  • BeanBean Posts: 8,129
    edited 2007-02-12 18:42
    Client,
    I assume you want the absolute value of a WORD variable ?

    IF value.15 = 1 ' Is value negative ?
    value = -value ' Yes, then negate it
    ENDIF

    As for the division, you'll need to put the result into a WORD variable temporarily. Then use the _LSB suffix.

    tempW = numberX / 100
    numberY = tempW_LSB

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap used 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com
    SX-Video Display Modules www.sxvm.com
    Stuff I'm selling on ebay http://search.ebay.com/_W0QQsassZhittconsultingQQhtZ-1

    "USA Today has come out with a new survey - apparently, three out of every four people make up 75% of the population." - David Letterman
  • ClintClint Posts: 95
    edited 2007-02-12 19:02
    Thanks Bean.
  • PJMontyPJMonty Posts: 983
    edited 2007-02-12 20:05
    Bean,

    How does the line...

    IF value.15 = 1

    ...check for a negative value? Shouldn't it be something like...

    IF value.15 < 0


    Thanks,
    PeterM
  • Sparks-R-FunSparks-R-Fun Posts: 388
    edited 2007-02-12 20:19
    Peter,

    The MSB (Most Significant Bit) of a Two’s Complement Number indicates its sign. A ‘1’ indicates a negative number. The statements check the MSB of the word variable and if it is a ‘1’ (meaning the number is negative) it negates the number again to make it positive. smile.gif

    - Sparks
  • PJMontyPJMonty Posts: 983
    edited 2007-02-12 20:28
    Sparks,

    Since I don't use SX/B (nothing against it, just haven't had the need), I wasn't aware of the subtleties of the language. That also explains the "Value.15", which is obviously checking bit 15 - the MSB. Makes perfect sense now. I'm well versed in the two's complement numbering scheme from 20+ years of assembly language programming, but I appreciate that you took the time to include the link for anyone unfamiliar with it.

    Thanks,
    PeterM
Sign In or Register to comment.