Negative numbers
Ozzie Neil
Posts: 16
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
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
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
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
Just change: TO this is what Kuroneko was saying in post #4.
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 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 "<=".
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
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).
You can also find a 2 page command list for both spin and pasm here
http://ucontroller.com/index_tut.html
Massimo