Evaluation of expressions

K

kalita

The expression:

(a + b) * (c + d) * (e + f)

Could be evaluated in the two following ways, among others (in polish
notation):

a b + c d + * e f + *

or

a b + c d + e f + * *

The former way is "better" in a sense that it requires only 2
temporaries, while the latter requires 3. If a, b, c etc. are all of
the same user defined type, can I rely on the compiler to use any
specific order of evaluation? Or is it implementation defined?

I'm asking because it seems to me that it is possible to evaluate any
C++ expression involving objects of the same type using only 2
temporaries, but only if compiler choses the optimal order. Otherwise
number of temporaries needed may be arbitrarily high.

cheers.
M.
 
V

Victor Bazarov

The expression:

(a + b) * (c + d) * (e + f)

Could be evaluated in the two following ways, among others (in polish
notation):

"in reverse Polish notation"
a b + c d + * e f + *

or

a b + c d + e f + * *

The former way is "better" in a sense that it requires only 2
temporaries, while the latter requires 3. If a, b, c etc. are all of
the same user defined type, can I rely on the compiler to use any
specific order of evaluation? Or is it implementation defined?

It's neither. The implementation is free to go one way in one case and
the other way in another case. "Implementation-defined" would require
the implementation (the compiler) to always go a certain way and document
it on top of that.
I'm asking because it seems to me that it is possible to evaluate any
C++ expression involving objects of the same type using only 2
temporaries, but only if compiler choses the optimal order. Otherwise
number of temporaries needed may be arbitrarily high.

Why do you care, anyway? Does it make a difference to the problem you
are solving?

V
 
P

Pete Becker

Victor said:
Why do you care, anyway? Does it make a difference to the problem you
are solving?

Just to add on a bit, floating point math does not obey the distributive
law, so regrouping expressions can change the result. But that's a far
more specialized area than this question implies; people who need
tightly specified results from floating point computation worry about
many things in addition to possible regrouping.
 
K

Karl Heinz Buchegger

Pete said:
Just to add on a bit, floating point math does not obey the distributive
law, so regrouping expressions can change the result. But that's a far
more specialized area than this question implies; people who need
tightly specified results from floating point computation worry about
many things in addition to possible regrouping.

Add over and underflow to that and the same applies to integer arithmetic.

I don't know how reasonable this is, but I seriously expect any compiler
to not to regroup arithmetic expression but to execute them from left
to right (obeying precedence laws of course).
 
M

Marcin Kalicinski

"in reverse Polish notation"

Right.
Why do you care, anyway? Does it make a difference to the problem you
are solving?

Yes. I am trying to write an optimizer for C++ expressions, which are made
of objects of my custom class. If I was sure that the number of temporaries
required to evaluate such an expression is at most 2, it would make my work
much easier. Unfortunately, from your reply it seems that compiler can chose
another way and require arbitrary large number of temporaries.

thanks,
M.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top