# Multiply by -1

213

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.
17393

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;`

. . .  ?

17393

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);
213

Thanks
369

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?

17393

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.