If I have an int with a positive value, I can return a negative value by placing the – operator directly in front of it. (e.g. -myInt) But if I have an int with a negative value, using the + operator doesn’t return a positive value. I’d like to understand why this is.
I am using C# and Unity, outputting to the Unity console. E.g:
myInt = 5;
Debug.Log(-myInt);
Gives an expected value of -5. But:
myInt = -5
Debug.Log(+myInt);
Gives a value of -5.
I can see I’ve flipped the values and operators between the examples, and so I sort of feel I should see what I am seeing; but the second example doesn’t feel intuitive and I don’t understand it. I expected the negative value to become a plus value.
This feels like a dumb question – thanks in advance.
ps: (Also, what’s the correct way of referring to the minus in ‘-myInt’ – am I correct in calling it an operator in this instance?)
Occultation is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2