Shop OBEX P1 Docs P2 Docs Learn Events
How do we in Spine declare a signed variable? — Parallax Forums

How do we in Spine declare a signed variable?

EMHmark7EMHmark7 Posts: 93
edited 2012-12-19 14:24 in Propeller 1
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

Comments

  • Heater.Heater. Posts: 21,230
    edited 2012-12-19 13:50
    All variables in the Spin language are signed.
    The problem might come when you want them not to be signed.
  • AribaAriba Posts: 2,690
    edited 2012-12-19 14:04
    Actually, all long variables are treated as signed, but word- and byte-variables as unsigned.

    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):
     VAR
      byte val1
    
    PUB
      val1 := -3
      longvar := val1    'you get 253  (256-3)
      longvar := ~val1   'you get -3
    
      val1 := 253
      longvar := val1    'you get 253
      longvar := ~val1   'you get -3
    

    Andy
  • EMHmark7EMHmark7 Posts: 93
    edited 2012-12-19 14:09
    Thanks for reply,

    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
  • EMHmark7EMHmark7 Posts: 93
    edited 2012-12-19 14:15
    I saw Speedy Gonsalez's reply after I posted my comment, Arrrriba, Arrrrriba!

    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.
  • AribaAriba Posts: 2,690
    edited 2012-12-19 14:24
    EMHmark7 wrote: »
    But when I do x:=x-1 when x was 0, I got in the 255, 254, etc.
    How do you know that it is 255,254... and not -1, -2... that is just a definition.

    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) 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?

    [/SIZE][/SIZE]
    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
Sign In or Register to comment.