Shop OBEX P1 Docs P2 Docs Learn Events
FAQ : How to handle [IF Temp < 0 THEN Loop] when Temp = -5 — Parallax Forums

FAQ : How to handle [IF Temp < 0 THEN Loop] when Temp = -5

ediedi Posts: 19
edited 2005-09-03 11:29 in BASIC Stamp
This post refers to an FAQ "Does the BASIC Stamp handle signed numbers and arithmetic?"


My Question : How to handle

··········· IF Temp < 0 THEN Loop

·When Temp = -5· & the expression should evaluate TRUE ?



The full FAQ is copied below :


Does the BASIC Stamp handle signed numbers and arithmetic?
Yes. The BASIC Stamp uses twos-compliment notation for signed numbers. This means that the expression: 0 -
10 + 5 will result in -5 if viewed as a signed number, however, most instructions see the number as a positive value,
in this case 65531 (the twos-compliment value for -5). All mathematical operators, except division, will work
properly with signed numbers in the BASIC Stamp and signed numbers can be formatted for output properly using
the SDEC, SHEX and SBIN formatters within DEBUG and SEROUT statements on the BASIC Stamp II, IIe and
IIsx. Be careful how you use signed numbers elsewhere. For example, if the value -5 is stored in a variable called
Temp, and you use the following statement:
IF Temp < 0 THEN Loop
it will evaluate to false and will not branch to Loop because -5 is actually 65531 in twos-compliment form and thus
65531 is not less than 0.





·

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-08-31 12:53
    You can add a positive offset to both sides of the equation.·· If, for example, you never expect your negative value to exceed -25 you can add 25 to both sides:

    · testVal = temp + 25
    · IF (testVal < 25) THEN
    ··· ' do something
    · ENDIF

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • ediedi Posts: 19
    edited 2005-09-03 11:29
    Hi Mr. Williams,

    Thanks for your reply to my post.·



    Regards,

    Richard
    ·
Sign In or Register to comment.