Multiply by -1

jugivi
213
jugivi  
I want to multiply a variable by -1 (minus 1), to pass the value from negative to positive...

variable1 = variable2 * -1;

but I get a compilation error (unexpected token), is there another way to do this?.
Thank you.
Simon Gniadkowski
17393
Simon Gniadkowski  
jugivi:
I want to multiply a variable by -1 (minus 1), to pass the value from negative to positive...

variable1 = variable2 * -1;

but I get a compilation error (unexpected token), is there another way to do this?.
Thank you.

Did you try . . .

variable1 =  -1 * variable2;

  . . .  ?

Simon Gniadkowski
17393
Simon Gniadkowski  
A minus sign  -  is an operator  . . . 

Changing the operation sign             x = - x;

so your code executes the multiplication first due to precedence rules . . . and  variable2 * -   doesn't make much sense to the compiler as you are trying to multiply a variable by an operator.

My code does this first,  
1 * variable2   and then applies the  "Changing the operation sign"   -   to the result so it works. You could also do this . . . variable1 = variable2 * (-1);
jugivi
213
jugivi  
Thanks
GreenMoney
369
GreenMoney  

Using the minus sign as an operator, why not just do this:

variable1 = - variable2;

Wouldn't that implement the example (x = - x) almost perfectly without the need for multiplication? 

Simon Gniadkowski
17393
Simon Gniadkowski  
Thirteen:

Using the minus sign as an operator, why not just do this:

variable1 = - variable2;

Wouldn't that implement the example (x = - x) almost perfectly without the need for multiplication? 

Yes,  to multiply by -1 . . .  but not to multiply by anything else.