CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM

Operator precedence

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   September 13, 2005, 13:32
Default Hi, I just got bitten by a, f
  #1
Senior Member
 
Rasmus Hemph
Join Date: Mar 2009
Location: Sweden
Posts: 108
Rep Power: 17
hemph is on a distinguished road
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
hemph is offline   Reply With Quote

Old   September 13, 2005, 13:40
Default That's because operator& and o
  #2
Senior Member
 
Hrvoje Jasak
Join Date: Mar 2009
Location: London, England
Posts: 1,907
Rep Power: 33
hjasak will become famous soon enough
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
__________________
Hrvoje Jasak
Providing commercial FOAM/OpenFOAM and CFD Consulting: http://wikki.co.uk
hjasak is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about fvmlaplacian operator cosimobianchini OpenFOAM Running, Solving & CFD 0 February 1, 2007 14:40
Template for operator overloading shuo OpenFOAM 0 November 10, 2006 22:17
RunTimevalue and runTimetimeName with %3d%3d operator maka OpenFOAM 1 May 1, 2006 20:22
Operator Splitting. Maria. Main CFD Forum 5 September 17, 2005 23:10
a math operator in UDF lichun Dong FLUENT 7 June 18, 2005 23:04


All times are GMT -4. The time now is 21:45.