Skip to main content
GameDev.net gamedev.net
🔒 Locked

operation order

Started by acw83 Oct 11, 2001 at 4:37 PM 5 replies 300+ views
Original Post
acw83
acw83
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.
Viscous-Flow
Viscous-Flow
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.
Viscous-Flow
Viscous-Flow
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.
Stoffel
Stoffel
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.

Stoffel
Stoffel
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 // a
Creating Tester #1 // b
Creating Tester #2 // c
Multiplying 0 into 1 // a * b
Creating Tester #3 // result of a * b
Multiplying 3 into 2 // (result of a * b) * c
Creating Tester #4 // d
acw83
acw83
aight, thanks man
taliesin73
taliesin73
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).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.