How do we in Spine declare a signed variable?
EMHmark7
Posts: 93
Hi,
I want to declare a Spine variable that will hold negative values (-127 to 127 is enough).
Doc says how to "sign" a variable in a formula,
1) but what is the right way to get a variable that will hold a negative value while not in calculations?
2) Do we need to convert somehow when we do calculations along with unsigned bytes?
Thanks
Marc
I want to declare a Spine variable that will hold negative values (-127 to 127 is enough).
Doc says how to "sign" a variable in a formula,
1) but what is the right way to get a variable that will hold a negative value while not in calculations?
2) Do we need to convert somehow when we do calculations along with unsigned bytes?
Thanks
Marc
Comments
The problem might come when you want them not to be signed.
But you can store signed values also in word and byte variables, without special declaration. Spin does all internal calculation with signed 32bit, so you need to tell Spin if a word or byte variable holds a signed value, when it accesses the variable (and expands it to a 32bit value):
Andy
But when I do x:=x-1 when x was 0, I got in the 255, 254, etc.
Doc specify necessity to use the ~x for getting good results if the variable holds a negative value.
1) So, now I suppose all Long and up are signed.
So, I would just need to declare as Long.
2) so, about a signed byte, can we do this ~x:= ~x-1
in ordert to store the signe value?
Anybody can confirm that?
Thanks,
Marc
So, I catch that Spine will save the signed value in the byte without need for declaring signed,
but would need the tilde when "reading" that variable in order to get the right value.
Anybody tell me if I am misled on that.
Thanks,
I am going to try it right now.
If you print the value out you can use: ser.dec(x) and will get 255, or ser.dec(~x) and will get -1.
1) yes, that is the easiest and best solution. Byte and Word ariables makes only sense for big arrays to spare ram, but not for single variables which you use in calculations.
2) I think this will not work
Andy