operation order

Started by
5 comments, last by acw83 22 years, 7 months ago
I''m working on matrix stuff and need to know how C++ performs a series of operations. For example, what order is: finalMat = mat1 * mat2 * mat3 * mat4; Would it be?: finalMat = (mat1 * (mat2 * (mat3 * mat4))); Thanks for the help.
Advertisement
With the peice of code you have it won''t matter what order they are multiplied in since what you are trying to do would come up with the same result if you did mat4 * mat2 * mat 3 * mat1.

Well, C++ essentially does all the operations the same way you would have learned in any basic algebra class (i.e. multiplication and division before addition and subtraction).

Also, if you aren''t sure use lots of parentheses since it doesn''t make it any slower once it is converted to machine code. The parentheses are just for the compiler to do it''s "magic" and get things to work right.

Just remember that something like mat4 * mat 1 / mat2 * mat3 is not the same as (mat4 * mat 1) / (mat2 * mat3).

Also, I hope those mat1 and mat2 etc are objects since the mulitplication would have to be overloaded operators if you wanted it done right.

Hope I was of some help.
With the peice of code you have it won''t matter what order they are multiplied in since what you are trying to do would come up with the same result if you did mat4 * mat2 * mat 3 * mat1.

Well, C++ essentially does all the operations the same way you would have learned in any basic algebra class (i.e. multiplication and division before addition and subtraction).

Also, if you aren''t sure use lots of parentheses since it doesn''t make it any slower once it is converted to machine code. The parentheses are just for the compiler to do it''s "magic" and get things to work right.

Just remember that something like mat4 * mat 1 / mat2 * mat3 is not the same as (mat4 * mat 1) / (mat2 * mat3).

Also, I hope those mat1 and mat2 etc are objects since the mulitplication would have to be overloaded operators if you wanted it done right.

Hope I was of some help.
If those are ints, you''re right. If those are not, you might be wrong. They appear to be matrices from their names, e.g. operator * is overloaded. In matrix math, multiplacation does not commute. A * B != B * A.

Since the question was about operator order and not operator precedence, the answer (to both) is:
- C evaluates operations of highest precedence first
- C evaluates operations of equal equivalence from left-to-right.

Maybe I should have said C++ multiplies from left to right, since you can''t overload in C. =)

Here''s a sample program to convince yourself of what''s going on:

  #include <iostream>using namespace std;class Tester{public:    Tester () : m_obnum (s_nextObnum)    {        cout << "Creating Tester #" << m_obnum << endl;        ++s_nextObnum;     }    friend Tester operator * (const Tester &lhs, const Tester &rhs);private:    int m_obnum;    static int s_nextObnum;};int Tester::s_nextObnum = 0;Tester operator * (const Tester &lhs, const Tester &rhs){    cout << "Multiplying " << lhs.m_obnum << " into " << rhs.m_obnum        << endl;    return Tester ();}int main (int argc, char **argv){    Tester a, b, c;    Tester d = a * b * c;    return 0;}  

Annotated program output:
Creating Tester #0   // aCreating Tester #1   // bCreating Tester #2   // cMultiplying 0 into 1 // a * bCreating Tester #3   // result of a * bMultiplying 3 into 2 // (result of a * b) * cCreating Tester #4   // d 
aight, thanks man
And of course, you do know that if you are not sure of the order of precedence, you can use as many parentheses as required, and the C++ compiler will honor your request

And that is any time ... it''s quicker to type parentheses than it is to find a copy of the order of precedence rules.

--

MP3 Dancer


Games, Anime and more at GKWorld

Meet Bunny luv'' and the girls, ready to perform on your desktop just for you (warning: adult content).

This topic is closed to new replies.

Advertisement