473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Evaluation of expressions

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.

Jul 23 '05 #1
4 1895
ka****@poczta.onet.pl wrote:
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
Jul 23 '05 #2
Victor Bazarov wrote:

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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #3
Pete Becker wrote:

Victor Bazarov wrote:

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.


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).

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #4
> "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.


Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: der | last post by:
Hello all, I've a question about order of evaluations in expressions that have && and || operators in them. The question is: will the evalution go left-to-right, no matter what -- even if the...
2
by: webposter | last post by:
Hi, I am looking for information on a data structure (and associated algorithm) to do short-circuit evaluation of boolean expressions and haven't found a single one even after googing for two...
4
by: Frank Wallingford | last post by:
Note: For those with instant reactions, this is NOT the common "why is i = i++ not defined?" question. Please read on. I came across an interesting question when talking with my colleagues....
2
by: Jeffrey Ganping Chen | last post by:
I'm trying to build a generic runtime C# expression evaluation engine to allow the user to evaluate any C# code blocks at runtime within the application's context. For example, obj4.p4 =...
21
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
77
by: berns | last post by:
Hi All, A coworker and I have been debating the 'correct' expectation of evaluation for the phrase a = b = c. Two different versions of GCC ended up compiling this as b = c; a = b and the other...
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
32
by: silpau | last post by:
hi, i am a bit confused on expression evaluation order in expressions involving unary increment.decrement operators along with binary operators. For example in the following expression x...
54
by: Rasjid | last post by:
Hello, I have just joined and this is my first post. I have never been able to resolve the issue of order of evaluation in C/C++ and the related issue of precedence of operators, use of...
11
by: Pietro Cerutti | last post by:
Hi group, here I come with a question which is quite simple per se, but for which I can't find an answer. Does the C standard guarantee that inside an expression such as (x && y) "y" is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.