Concerning Joseph Coveney's <jcoveney@bigplanet.com> question about
> : i = 1
>
> : (i, ++i, i, i++, i)
> 1 2 3 4 5
> +---------------------+
> 1 | 2 2 2 2 2 |
> +---------------------+
>
> and
>
> : i = 1
>
> : (i, i, ++i, i, i++, i)
> 1 2 3 4 5 6
> +-------------------------+
> 1 | 1 1 2 2 2 2 |
> +-------------------------+
I have now changed -help [M-2] op_increment- help file from talking about
bad style to saying, don't do it:
++i, i++, --i, and i-- should be the only reference to i in the
expression. Do not code, for instance.
x[i++] = y[i]
x[++i] = y[i]
x[i] = y[i++]
x[i] = y[++i]
The value of i in the above expressions is formally undefined; whatever is
its value, you cannot depend on that value being obtained by earlier or
later versions of the compiler. Instead code
i++ ; x[i] = y[i]
or code
x[i] = y[i] ; i++
according to the desired outcome.
It is, however, perfectly reasonable to code
x[i++] = y[j++]
That is, multiple ++ and -- operators may occur in the same expression, it
is multiple references to the target of the ++ and -- that must be
avoided.
The fact is that when and where ++ and -- operators are actually evaluated
is devlish to figure out: ++ and -- are designed to be fast, and it all
depends on efficiency considerations. All you know for certain is
that when you code
... x[i++] ...
is that x[i] will be used in the expression and that i will be incremented
sometime after that, in that expression. When you code
... x[++i] ...
you know that i will be incremented sometime before x[i] is evaulated.
Moreover, it's even more complicated, because Mata does not always evaluate
expressions from left to right. When you code,
(<exp1>) <op> (<exp2>)
one naturally assumes that Mata compiles <exp1> before <exp2>, and thus
evaluates <exp1> before <exp2>. That may not be true. Based on efficiency
and other considerations, Mata can decide to evaluate <exp2> first.
-- Bill
wgould@stata.com
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/