"if (A & B)" gives different result than "if (B & A)"

tsambou's icon

I was having a problem with a patch I am working on, and after not understanding why it wasn't working I figured something which I am quite confident should be called a bug. Changing the two sides of an if statement fixed the problem. What do you think?

Max Patch
Copy patch and select New From Clipboard in Max.

Roman Thilenius's icon


"is not" seems all you need:

[if ($i1&$i2) != ($i2&$i1) then yes else no]

Roman Thilenius's icon


oh sorry this is gen.

i think you forgot to put the second half into brackets?

if (delta(Variable) != (0 && LocalCh == SelCh))

tsambou's icon

Doesn't seem to make any difference.

Max Patch
Copy patch and select New From Clipboard in Max.

Nick Holmes's icon

Software Engineering Rule #16: NEVER mix side-effected functions in short circuit logical evaluation.

Short-circuit logic - in the case of AND - can directly return false if the left operand is false. The right hand operand does not need to be evaluated, because AND will return false no matter what it evaluates too.

delta() is returning the delta with respect to the last time it was called - i.e. if you call is call it three in a row times with the same value, it will return x, 0, 0. So calling delta() has a side effect, in that some state is stored to be used in the next call.

Now, put these together;

Due to short-circuit evaluation, you are calling delta() 3 times in one gen, and only once in the other. I've updated your second gen to call delta outside of the it (so, it will be called 3 times), but kept the operand order the same, and now they function identically.

Max Patch
Copy patch and select New From Clipboard in Max.

Roman Thilenius's icon


" 16) Error correction is most efficiently done by the document´s author(s). "

https://www.ics.uci.edu/~emilyo/SimSE/se_rules.html

tsambou's icon

Wow thank you so much, I think this really clarified the situation. If I was aware of it earlier it would have definitely saved me alot of time and trouble.

Cheers!