Shop OBEX P1 Docs P2 Docs Learn Events
How to do absolute value in integer math for BS1 — Parallax Forums

How to do absolute value in integer math for BS1

tact1caltact1cal Posts: 3
edited 2005-11-29 19:19 in BASIC Stamp
Hi all,

Is there a simple round-about way to subtract (2)· 16-bit numbers and get the absolute value difference? I need this for the·BS1.

Appreciate any simple solutions...

Thanks,

-Guy

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-11-29 17:15
    Since there's no ABS function in PBASIC 1 you'll have to do a bit more work:

    Get_Abs:
    · IF aVal < bVal THEN B_Bigger
    · result = aVal - bVal
    · GOTO Abs_Done

    B_Bigger:
    · result = bVal - aVal

    Abs_Done:


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • tact1caltact1cal Posts: 3
    edited 2005-11-29 17:20
    Thanks, Jon

    I had thought about this solution....thanks for simplifying it!

    R/

    -Guy
  • foobertfoobert Posts: 3
    edited 2005-11-29 17:38
    or do the subtraction, test the high order bit of the result and, if it's set, negate the result.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "i want to lead a disipate existence play scratchy records and enjoy my declinw" - iggy pop, i need more
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-11-29 19:19
    That would work, but to keep things simple you'd have to force the result into W0 as that's the only BS1 word that allows direct bit access.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
Sign In or Register to comment.