Shop OBEX P1 Docs P2 Docs Learn Events
Negative numbers — Parallax Forums

Negative numbers

Ozzie NeilOzzie Neil Posts: 16
edited 2011-05-20 05:08 in Propeller 1
i have a Propeller question which should be quite easy but is stumping me.

Suppose i have a negative number -33 and i want the number to be positive 33

I have some code displaying the value from cnt so i can easily test it, the terminal correctly displays a negative number but my code to change it to positive gives strange results

i have tried multiplying it by -1 and a few other methods but no joy.

I am certain the answer will be deceptively simple

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2011-05-19 01:44
    A code sample would be nice. Or we're guessing all day.
  • Ozzie NeilOzzie Neil Posts: 16
    edited 2011-05-19 01:59
    the code looks like this

    repeat ctr from 1 to 10
    data := cnt
    debug.dec (data)

    if data <=0
    data := data * -1 'this is the line i am after
    debug.dec (data)
    else debug.str(string(number is positive",13))

    the loop works fine i just get some strange results depending on the calc
    i have also tried
    data := -data
    data := data & $7fffff
    and a few others
  • kuronekokuroneko Posts: 3,623
    edited 2011-05-19 02:02
    Ozzie Neil wrote: »
    the code looks like this
    repeat ctr from 1 to 10
      data := cnt
      debug.dec (data)
      
       if data [COLOR="red"]<=[/COLOR]0
          data := data * -1                    'this is the line i am after
          debug.dec (data) 
       else
          debug.str(string(number is positive",13))
    
    Easy enough to make that mistake. In SPIN that's an assignment. The comparison would be =<. Also you could simply say -data which has the same effect as multiply by -1. HTH
  • Clive WakehamClive Wakeham Posts: 152
    edited 2011-05-19 03:15
    Why not use the operator " || " Absolute value. PAge 156 of the propeller manual.
  • Ozzie NeilOzzie Neil Posts: 16
    edited 2011-05-19 04:46
    the line concerned is:

    data :=cnt 'places the value of cnt into variable data (long)
    data := data * -1 'calculation

    when i convert that to
    data := || data the response i get is 1 no matter what the negative value of data is. I also tried dta := || data

    Interestingly when i went to pg 156 of the manual and tried that using x and y it works.

    using absolute in another form was one of the methods i tried previously so i am not sure why it doesn't like using the internal cnt value. I am only using the internal cnt because it generates a negative value for testing but i would liek to understand why it doesn't like using it
  • kuronekokuroneko Posts: 3,623
    edited 2011-05-19 04:55
    Ozzie Neil wrote: »
    I am only using the internal cnt because it generates a negative value for testing but i would liek to understand why it doesn't like using it
    Is the why still unclear? I thought my second posting cleared that up (assignment vs comparison).
  • davidsaundersdavidsaunders Posts: 1,559
    edited 2011-05-19 06:57
    Ozzie Neil:
    Just change:
    if data [COLOR=red]<=[/COLOR]0
          data := data * -1                    'this is the line i am after[FONT=Arial]
    [/FONT]
    
    TO
    if data =< 0
          data := -data                    'this is the line i am after
    
    this is what Kuroneko was saying in post #4.
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-05-19 07:43
    Ozzie,

    The "<=" operator performs an operation on a variable and assigns the results to that variable. It is the same type of operator as "+=". The statement "if data <= 0" is functionally equivalent to "if (data := (data < 0)) <> 0". This can be written more clearly as
    data := (data < 0)
    if data <> 0
    
    If data has a value greater than or equal to zero it will be set to zero, and the body of the if will not be executed. If data is less than zero it will be set to TRUE, which is a -1. The body of the if will be executed and the value of data will become 1.

    As others have said, you want to use "=<" instead of "<=".
  • Ozzie NeilOzzie Neil Posts: 16
    edited 2011-05-19 22:39
    Thanks guys for all your help.

    As soon as i made the change it worked fine, the operators don't quite line up with what you think your are doing do they? <= means less than or equal to when i went to school and i guess it still does but the response is presented in a different way which unless you know leads you down the wrong path.

    I knew the answer would be simple, thanks again everyone, i will do some reading on this and brush up a bit.


    Neil
  • schillschill Posts: 741
    edited 2011-05-20 04:54
    I find the operators in Spin have been the hardest thing to keep track of. The ones I use a lot I remember; the ones I don't use very much I end up looking up. I've printed out the pages of the manual with the operators lists so they are always handy :) .

    A basic rule I follow: If the operator ends with "=", double check to make sure it's the one you want. Often, this just means thinking twice about it (not actually checking the manual). Most of the ones than end in "=" are assignment operators.

    I agree that the behavior of "<=" is counter-intuitive (at least to me). I think "less than or equal to" and "<=" makes sense. It took a little work to start thinking "equal to or less than" (=<) instead.

    Of course, if I'm writing "math" instead of code then "less than or equal to" is neither of the above: it's a "<" with a line underneath ("≤" if it shows up correctly).
  • max72max72 Posts: 1,155
    edited 2011-05-20 05:08
    in the propeller tool there is a link to a lot of pdf stuff.. the manual, the demoboard schematics, and.... a 4 page quick reference.
    You can also find a 2 page command list for both spin and pasm here
    http://ucontroller.com/index_tut.html

    Massimo
Sign In or Register to comment.