PDA

View Full Version : a simple question of arithmetic



agentile
01-07-2006, 02:04 AM
Hello,
· I am writing a program to demonstrate how do deal with signed integers, and how to know if and when there is an overflow condition.· So I have the following code

k VAR Byte
q VAR Byte
x VAR Byte

k=5
q=6
x=k-q

DEBUG DEC k," - ",DEC q, " = ",SDEC x,CR

And the value I get for x is 255, which is the two's complement form of -1.· But I have used the signed decimal modifier, and I would like to get -1 instead of 255.

So, if I write·DEBUG··SDEC k-q, the answer comes out as -1.· Also, if I declare my variables as Words, it also works properly.· So, why won't it work with Bytes?

thanks,
Andrew

Newzed
01-07-2006, 02:42 AM
Because 5-6 = 65534 with a Stamp.··X must be sized as a word to handle 65534.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sid Weaver
Do you have a Stamp Tester yet?
http://hometown.aol.com/newzed/index.html

·

allanlane5
01-07-2006, 04:33 AM
The conclusion I've come to is that PBasic doesn't really HAVE 'negative numbers'. Sure, you can use the 'SDEC' modifier to have it printed as if it was a negative number. But that doesn't help you in 'IF-THEN' or 'FOR' loops. The upshot being that
"IF A < 0" is NEVER true.

This is not really a problem, though, you can say "IF A.Bit7 = 1 THEN" if you know that you always use 'A' in signed arithmetic.

Jon Williams
01-07-2006, 06:21 AM
You can always test Bit15 of a value to determine its sign -- it's up to you to determine whether your span for a word is 0 - 65535, or -32768 - 32767.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax

agentile
01-07-2006, 07:12 AM
I'm not sure I asked my question properly.· I·understand the method of two's complement, and how the MSB is the sign bit, but I don't understand why DEBUG cannot handle·negative numbers using bytes instead of words.··I should have a range of -128 to 127 with a signed byte.···So, for the following code:

x = 5 - 6

DEBUG SDEC x

The value displayed on the screen for x varies depending on how x was declared.· If x is a byte, the screen displays 255; if x is a word, the screen displays -1.· Both answers are correct, numerically.· The only part of this which appears to be incorrect is the way DEBUG SDEC is handling x.··This proper display would be -1 because I have asked for a signed value.· If I just said DEBUG x, then I would have expected 255 just based on variable overflow.· So, why can't I use DEBUG SDEC with bytes?



thanks,

Andrew

Jon Williams
01-07-2006, 08:13 AM
Internally, all math is done with 16 bits and the result truncated to the variable size you specify.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax

Tracy Allen
01-07-2006, 09:58 AM
When you say,
x VAR Byte
x=-1
DEBUG SDEC x
The Stamp assigns the number %1111111111111111 to x, But since x is just a Byte, it holds %11111111. Then in the DEBUG, it temporarily puts x into a word variable %0000000011111111 and then prints that in twos complement, so you get 255.

There are situations where you might need to work with twos complement bytes, say, to save memory. Or, some sensors return their data in a twos complement format in 8 or 9 bits, and you have to extend the sign. For example, the DS1620 returns 9 bits with x.bit9 being the sign. So to turn that into a proper twos complement word you have to extend the sign:
x VAR Word
x.byte1 = - x.bit8

You can go ahead and do twos complement arithmetic on Bytes, but suppose your sign bit is x.bit7. Then extend sign before or during the printing:



x VAR Byte
y VAR Word
x = 3 - 7 * 2
y.byte0 = x
y.byte1 = -x.bit7 ' extend sign
DEBUG SDEC y, TAB
DEBUG SDEC -x.bit7<<8 + x ' to extend sign right in the DEBUG



What that math does, for either DEBUG, is fill in the extra ones, so that %000000001ddddddd becomes %111111111ddddddd, where d is an arbitrary binary 0 or 1.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com (http://www.emesystems.com)

Post Edited (Tracy Allen) : 1/7/2006 6:19:59 PM GMT

agentile
01-07-2006, 10:11 PM
Thank you both very much.· Now I get it.

Tracy Allen
01-08-2006, 02:52 AM
I edited my previous post to include the statement
y.byte0 = x
without which y will not equal x, even with sign extended.

The same principle applies to most of the calculations in a Stamp. It internally works with a 16 bit variable. So if the starting variable is a byte, nib or bit, the resulting word has its most significant bits set to zeros. And if you assign the result to a smaller chuck (byte, nib, bit), then the most significant bits are dropped.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com (http://www.emesystems.com)