Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minus of a variable in C++?

Tags:

c++

variables

I have this equation:

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

Can I simply type (-I) or something similar, or do I have to multiply I by -1?

like image 949
Bojangles Avatar asked Dec 07 '22 00:12

Bojangles


2 Answers

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

- x

This negates x, just like 0 - x or -1 * x would negate x. Note that the - in -1 * x is also the unary minus operator.

like image 152
James McNellis Avatar answered Dec 23 '22 14:12

James McNellis


You mention in your comment that:

I is a direction vector

If you're using operators on non-native types, you can't assume that they are defined. It depends whether operator overloading has been implemented on the class: if there is a Vector Vector operator-() member function

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

like image 29
Eric Avatar answered Dec 23 '22 16:12

Eric