K
Keith Thompson
Don Bruder said:Don Bruder said:if ( (SomeFunc(Item, Col) <= Limit) == (V == 1))
Would appreciate any comments/suggestions/code-simplifications that
you may be able to offer.
You have a compound boolean expression working here. And you seem to be
confused about order of evaluation.
Order of evaluation has very little to do with this.
Based on the specific way the query was phrased I'd say that order of
evaluation has practically *EVERYTHING* to do with his question. (an
impression which is further reinforced by seeing his responses in the
rest of the thread)
I disagree. In fact, I honestly don't understand how you've reached
that conclusion.
The full expression is:
(SomeFunc(Item, Col) <= Limit) == (V == 1)
which can be broken down to two subexpressions, both operands of "==":
SomeFunc(Item, Col) <= Limit)
(V == 1)
Breaking each of those down, we get 4 subexpressions:
SomeFunc(Item, Col)
Limit
V
1
(I could break it down further, but this suffices.)
There are 12 possible orders in which those 4 subexpressions can be
evaluated. The order *doesn't matter*. The result will be exactly
the same regardless of the order of evaluation, and assuming that they
must be evaluated left-to-right is not helpful in understanding what
it means. (Unless SomeFunc has side effects that can change the value
of Limit or V, but if it does then the fact that order of evaluation
is unspecified becomes very significant.)
[...]
True enough - at the compiler level. At the "I'm trying to understand
what's going on here" level, it seems to me like something pretty
crucial.
It's true at *every* level. I wasn't talking about compilers, I was
talking about what the expression actually means based on the language
definition.
Sure, left-to-right is one of the legal evaluation orders, and if
mentally processing it that way is helpful, that's fine. It's your
assertion that the expression is necessarily evaluated that way that I
dispute.
In this case, it appears that the order is critical, at least to the
poster's understanding of what the code is trying to do. It may or may
not be of importance to the compiler. But since it isn't the compiler
asking the question...
If the OP's understanding depends on assuming a left-to-right order of
evaluation, then the OP doesn't really understand it.
Consider trying to tailor your response to the question being asked - If
someone asks "what's 2+2?" you wouldn't launch into some obscure chunk
of number theory that proves that the answer is 5 in certain dimensions,
would you?
Nope.
(Well, I'd take into account the fact that it's probably a trick
question, since hardly anybody would ask such a question seriously,
and I might go off a tangent about modular arithmetic or base 3 -- or,
more likely, I might make a joke like "What kind of answer did you
have in mind?" But I digress.)
By flinging around "chapter and verse according to things deep in the
guts of the compiler", that's pretty much what you're doing.
Not at all. Again, I didn't mention compilers at all.
The fact that evaluation order is, in many cases, unspecified is a
vital fact about the C language (though some might wish it weren't).