FAQ : How to handle [IF Temp < 0 THEN Loop] when Temp = -5
edi
Posts: 19
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.
·
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
· testVal = temp + 25
· IF (testVal < 25) THEN
··· ' do something
· ENDIF
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Thanks for your reply to my post.·
Regards,
Richard
·