Neg to Positive
Monkey Forums/Monkey Programming/Neg to Positive
| ||
Local x:int = 1 x=-x ' This returns -1 as I expected '*********************** Works as expected Local x:int = -1 x=+x 'this returns -1 when I expected 1 returns wrong value? Am I doing this wrong? |
| ||
Well math 5th grade here... ;) +- stays - -- will become + If you want to invert the sign do: x = - x If you always want a positive number but don't know the sign do: x = Abs(x) If you always want a negative number but don't know the sign do: x = - Abs(x) If you want to know if the number is positive or negative do: sign = Sgn(x) |
| ||
thanks for the in depth reply but am I really adding using x=+x surely I am just redefining its value, if thats not the case then the first example should be 0 and its not. I did ultimately settle on abs(x) but it just feels wrong |
| ||
Local x:int = -1 x = +x -------> -1 because +( -1 ) = -1 x = -x --------> +1 because -( -1 ) = 1 + whatever = whatever You are redefining its value - to the value it already is. You can also imagine you are adding it to 0: x = +x means the same as x = 0 + x |
| ||
thanks for clarifying it |