ehsan.akhgari@gmail.com wrote:> Hi all,>> Is there any upper bounds on the number of temporary variables that are> needed to translate any given mathematical expression?

How about (ab+cd)/(ef+gh) ?

Whichever you start from, (ab+cd) or (ef+gh), you need one temporary
variable to hold the result:

*, a, b, t1
*, c, d, t2
+, t1, t2, t1

and then, to calculate the other expression, you need two temporary
variables:

*, e, f, t2
*, g, h, t3 (!)
+, t2, t3, t2

Then, how about (ab+cd)/(ef+gh)-(ij+kl)/(mn+op)? You start with either
sides of the -, and use one temporary to hold the result. Then you need
another three to calculate the other side.

It's true that mathematically speaking, this is equivalent to
((ab+cd)(mn+op)-(ij+kl)(ef+gh))/(ef+gh)(mn+op), which is equivalent to
(abmn+abop+cdmn+dcop-ijef-ijgh-klef-klgh)/(efmn+efop+ghmn+ghop), which
can be calculated using three temporary variables, but I'm not sure the
compiler can assume that. For example, here's a program that shows the
difference:

Since the compiler cannot modify expression using mathematical
"simplifications" (making the expression much larger, but using only
three temporary variables to calculate it), it must use the parse tree
as it is, and when that's the case, you can always create parse trees
that require more and more temporary variables.