Minus variable in C ++?

I have this equation:

R= 2*(-I dot N)*N + I

Can I just type (-I) or something like that, or do I need to multiply I by -1?

+4
source share
2 answers

C ++ has a unary minus operator that does negation.

 - x 

This negates x , just as 0 - x or -1 * x will negate x . Note that - in -1 * x also a unary minus operator.

+13
source

You mentioned in your comment that:

I - direction vector

If you use operators for non-native types, you cannot assume that they are defined. It depends on whether the operator overloading in the class has been implemented: if the Vector Vector operator-() member function exists

Also, mathematically speaking, (-I dot N) == -(I dot N) :)

+1
source

All Articles