CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM (http://www.cfd-online.com/Forums/openfoam/)
-   -   Operator precedence (http://www.cfd-online.com/Forums/openfoam/61023-operator-precedence.html)

hemph September 13, 2005 12:32

Hi, I just got bitten by a, f
 
Hi,
I just got bitten by a, for me, strange behaviour.
As it turns out, + takes precedence over ^ (crossProduct) for vectors, so

a^b + c^d == a^(b + c)^d, and NOT
(a^b) + (c^d) as with matematical rules.

As I am not overly profficent in C++ I was wondering if this due to overloading of ^ or some other reason.

//Rasmus Hemph

Example code:

vector x = vector(1,0,0);
vector y = vector(0,1,0);
vector z = vector(0,0,1);


vector a = (x ^ z) + (y ^ z);
vector b = x ^ z + y ^ z;
vector c = x ^ (z + y) ^ z;

Info<<"vector a: " << a <<endl
<<"vector b: " << b <<endl
<<"vector c: " << c <<endl;

outputs:

vector a: (1 -1 0)
vector b: (-1 0 0)
vector c: (-1 0 0

hjasak September 13, 2005 12:40

That's because operator& and o
 
That's because operator& and operator^ are really binary C++ operators in C++ and the language does not allow you to define your own operators or change operator precedence.

We have chosen to re-use the operators for nice sytnax - makes the code look nice. Therefore, all I can recommend is lots of brackets in vector/tensor products.

Enjoy,

Hrv


All times are GMT -4. The time now is 18:29.