 |
intoK Apprentice
Joined: 18 Feb 2007 Posts: 190
|
Posted: Fri Oct 17, 2008 10:04 pm
Expression optimization issue |
just noticed why some of my scripts are behaving wonky - this aspect of optimization is not mentioned in changes for zmud users chapter
#SA %eval(1 OR 0 AND 0 AND 1) -> 0 on zmud, 1 on cmud
obviously, putting OR part in brackets fixes the problem, but in the past i specifically redesigned my expressions to get rid of as many brackets as possible by moving ORs to front
why cmud isnt checking all operators? is still has to check next operator after AND even when previous expression evaluated to 0
hardly a bug, i can definitely see how much easier was it to code it that way, still, pretty important bit to put in manual |
|
|
 |
Zugg MASTER

Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Sat Oct 18, 2008 1:00 am |
CMUD does what most other programming languages do. First, you should never assume what the implied operator precedence is. Your above statement is like doing (1+0*0*1). Now, most of us know that multiplication should be done before addition, but many people don't know how this normally works with AND and OR. So it's *always* good to use extra parenthesis to make your statement more readable and ensure that it gets executed correctly. It's just a good habit that will pay off for years if you do any real programming.
Now, given that AND is executed first, and OR second, you next need to know about expression optimization. Again, this is true in most every modern computer language. When evaluating an expression, as soon as it has executed enough to know the final result, it stops and never executes any remaining statements. For example:
0 AND 0 AND 1
will never evaluate the second 0 and the final 1 because it's obvious the result is 0 based upon the first expression. No matter what the rest of the expressions are, the result is going to be zero. Now, you take this result of zero, and now you perform the OR operation and you have
1 OR 0
and the answer is one. It still needs to evaluate the "1" expression because the result of the AND operations was zero, so the final result could still be a one. If the result of the AND operation was 1, then the first 1 would be skipped because the result is going to be 1 no matter what the first expression is.
The reason languages do this is so that you can have statements like this:
IF (defined(A) AND (A.property = value)) THEN...
Obviously you don't want the "A.Property" to be executed if A is not defined (because that would give an object error). But because modern languages all have expression optimization, the evaluation stops as soon as "defined(A)" is false, preventing any object error.
zMUD actually tried to do this too, so it's not really a change just in CMUD. The problem is that since zMUD was not a properly compiled language with proper operator precedence, there are times when the optimization didn't work properly.
Anyway, the bottom line is that yes, CMUD has expression optimization just like any modern scripting language should, and you should always use parenthesis to ensure that your expressions are executed in the proper order. Learn from this because all modern languages all work like this. |
|
|
 |
intoK Apprentice
Joined: 18 Feb 2007 Posts: 190
|
Posted: Sat Oct 18, 2008 8:11 am |
thanks for that!
thing with zmud was it did left to right order instead precedence, and i assumed engine changed only with not evaluation of elements, while operators were still calculated and grouped left to right
ie (1 or 0 and 0 and 1) -> group and evaluate -> [(1 or 0) and 0 and 1] -> g'n'e -> [(<1> and 0) and 1] -> stop at this
i do use brackets when writing in c, this was one of those inner knowledge shortcuts that are always bad code practice, guess it paid back now  |
|
|
 |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|