## this logical expression seems illogical to me!

Oct 1, 2013 at 10:18am

# this logical expression seems illogical to me!

Can someone explain to me why this is backwards from what i expect?

The expression is:
if (\$i1 == 0) || (\$i2 == 0) || (\$i3 == 0) then 1 else 0

I think this should mean, if all 3 inputs are at 0, then output a 1; otherwise output a 0.

What it _does_ is .. if all outputs are at 1, output a 0; otherwise output a 1.

WTF???

<code>

– Pasted Max Patch, click to expand. –

</code>

#266741
Oct 1, 2013 at 12:06pm

yup, just figured that out. I got my logical operators in a twist. (Not something I use very often!)

thanks

#266749
Oct 1, 2013 at 12:37pm

depending on what it should do, you could solve that with simple math, too, and easier to read:

[if (\$i1+\$i2+\$i3)==0 then 1 else 0]

i havent tried it, but

[if (\$i1+\$i2+\$i3) then 0 else 1]

should do the same.

to optimize for processing power, use expression:

[expr ((\$i1+\$i2+\$i3)==0)]

:)

-110

#266750
Oct 1, 2013 at 12:39pm

[if \$i1 && \$i2 && \$i3 then 0 else 1] might also work. :)

#266751
Oct 1, 2013 at 1:57pm

[if (\$i1+\$i2+\$i3) then 0 else 1]
in this case, if \$i1+\$i2+\$i3 == 0, then \$i1+\$i2+\$i3 will return 0 hence false so it will be the opposite :))
same for [if \$i1 && \$i2 && \$i3 then 0 else 1] if i get it correctly :?

#266764
Oct 1, 2013 at 2:40pm

yes, vichug, you get it correctly :)

but more importantly! I LOVE THE TITLE OF THIS THREAD! :D

#266771
Oct 1, 2013 at 3:14pm

to optimize for processing power, use expression:

[expr ((\$i1+\$i2+\$i3)==0)]

And to optimze the text, lose the parentheses;

[expr \$i1+\$i2+\$i3==0] does the trick, too.

#266772
Oct 1, 2013 at 6:13pm

It’s generally a bad idea to replace boolean operations with arithmetic ones.

If the goal is “if all operands are 0 then 1 else 0″ (say) then the arithmetic expression

if (\$i1 + \$i2 +\$i3) == 0 then 1 else 0

does not generalize as you could trivially have

if ( 2 + 2 – 4) == 0 then 1 else 0

which does NOT provide the desired results as none of the operands is zero yet you will get a 1

You’re going to have to be very careful to ensure that the operands can never be anything other than 0 or 1 particularly in languages where boolean(true) can be any non zero value.

Even in the cases where you can do it, the optimization hides the intent of the algorithm so unless you know that you’re being bit (pun intended), it’s not worth it.

#266791
Oct 1, 2013 at 8:07pm

@DJH: yeah, of course, but for now we assume that it is almost sure that there is only 0 or 1 coming from a checkbox or a comparison operator object. why would someone check if 77.5 is 0 or 1 ?

the goal was to find out if all are zero, and this is true when their sum is zero. that the sum can also be zero when the values are 4, 2, and -2 … i could not care less.

as long as you _use (\$i1+\$i2+\$i3)==0 as boolean, it will also _function like one.

but more importantly: raja is happy of the thread title!

-110

#266793
Oct 2, 2013 at 1:19am

“it is almost sure that there is only 0 or 1″

yes, i agree… though this is probably just a matter of semantics(‘logical’ operators generally deal with boolean values (true,false or 0,1), so this is why we can assume it here, especially because of the thread title :D)

but what dhjdhjdhj said: “the optimization hides the intent of the algorithm”
is also very bitwise(pun returned ;D).

so +1 to dhjdhjdhj, -110 to roman, and we’re all happy! :D

#266810
Oct 2, 2013 at 6:19am

wow, and I thought my brain was hurting before!

Thanks for the pointers to the simplified arithmetical version, and you’re right that I’m only dealing with 0/1s. I’m sticking with the longer form version though, as I think it makes it a bit easier for me to read the logic. (I’ll add the whole patch below – as you’ll see there’s quite a lot of logic!)

(Well, I was going to post it, but when I hit Sumbit all I’m getting is a blank page)

#266827
Oct 2, 2013 at 6:20am

Here’s another attempt, with the duplicate parts (3 of) removed…

<code>

– Pasted Max Patch, click to expand. –

</code>

#266828
Oct 3, 2013 at 6:13am

And that patch turned out to suffer from unexpected consequences (and be too complicated). Much nicer (I hope) solution posted separately here

#266927
Oct 3, 2013 at 6:27am

Why don’t you convert one of those blocks (tracks) into an abstraction (and instantiate through bpatchers) with the appropriate inputs and outputs and just duplicate it 4 times —– it would become simple to both read and understand and it would also be much easier to change it and as well as be trivial to add more tracks?

#266928
Oct 3, 2013 at 6:58am

Just to show you what I mean, I converted one of your identical blocks to a subpatcher (because that automatically creates the needed inputs and outputs) and then created a new abstraction (called ds_track) from that patcher. A couple of send/receives to reduce connections, presentation mode so you only see the UI part, insert each one into a parameterized bpatcher and you have a much improved version.

I left the rest of it as an exercise :-)

ds_track.maxpat
<code>

– Pasted Max Patch, click to expand. –

</code>

ds_main.maxpat
<code>

– Pasted Max Patch, click to expand. –

</code>

###### Attachments:
1. Archive.zip
#266929
Oct 3, 2013 at 7:59am

raja i still disagree.

as an [if] expression or a [>] or [&&] object does not technically output the symbol “true” but the 32 bit integer number “1″, we don´t need to make a difference between the arithmetic and the boolean way of calculating it.

from the engineers viewpoint the difference between the two seems to have vanished as soon as you are in max.

however, DHJ´s point, that my solution wont work in _any case and is actually something different than the other ones, should be taken into account by the OP. somewhen in the future it might be important in a situation to be aware of this little difference.

-110

#266934
Oct 3, 2013 at 9:02am

roman, i think you are actually agreeing with me(you wrote exactly what i wrote, and i agreed with DHJ AND you… read the damn post, i even said the exact words: “i agree” to both of you…) and maybe, perhaps, sometimes, you are too self-absorbed to actually read someone else’s writing comprehensively enough to understand what they actually wrote(and also maybe a bit too self-absorbed to focus on anything but moot points on a subject… in many computer environs, a boolean value of true/false, is represented by 1/0…or maybe your command of the english language just sucks ass and i don’t understand what you’re actually trying to say?).

i think most of the time, you contribute to a discussion because the only person you’re trying to help is your own ego, you rarely actually post something that’s explained well enough to help someone else… you should really work on this…. it’s been years and you’re still lost in your own world without realizing how little you apply yourself(but this is all posted in humor, because i find your ego as this ineffective thing causing you to shoot yourself in the foot, to be quite hilarious).
i don’t think you wrote anything different from me.
And at least, in terms of self-absorption and ego, You are just like me! :D (except i do it on purpose as a sociological experiment conducted in internet forums, and you on the other hand, are actually misguided enough to think you’re offering something respectable :p)
that’s just what i think. >:D

#266943
Oct 3, 2013 at 9:34am

I find it distasteful and pointless when these discussions become personal.

Lets get back to just helping people with their Max related tech problems.

Sociological experiments don’t belong here.

#266945
Oct 3, 2013 at 9:37am

I also find it distasteful when you two in particular, come in on a thread and argue moot points in some white-male-lost-in-their-ego way that completely befuddles the discussion(funny you mention help, when you two are notorious for killing the help).

I won’t stop posting here until DHJ AND Roman stop posting here on these forums(those two are half the reason i have to remain here to fight…. all white-male-ego, and no substance whatsoever).

“Sociological experiments don’t belong here.”

That’s just a matter of opinion. And ‘forums’ are for ancient roman opinions much like these :D

Plus i find your lack of understanding that Roman attacked me first for no apparent reason, to be quite discriminating and prejudiced.

And don’t worry, i don’t mind that your notifications are disabled(hit-and-run coward), it helps with my sociological experiment >:D

#266946
Oct 3, 2013 at 10:44am

David Stevens wrote:

wow, and I thought my brain was hurting before!

#266952
Oct 3, 2013 at 11:15am

@Roman

HAHA, nice, i actually thought that was very funny and makes me happy to hear you not be as reactionary as i expect everyone here to be(i pull this type o shit on many forums, but here is the only place people are the quickest to misjudge and react…. this is definitely a very secluded, somewhat out-of-touch, western-conservative community compared to other artistic communities).

but i just want to point out: i initially came here to this thread, acting very nice and agreeable for once(i did not even initially address you nor DHJ directly… not until you addressed me), and i feel like you raped my intent for no reason… just wondering why you were so bent on a disagreement where there was none?

#266958
Oct 3, 2013 at 5:10pm

it is not about 110 not being as reactionary as the rest of that human fungus on this planet of the infidels, i simply know you well enough to know that you are playing a game. (of course it could also have been the mad cow disease, but the chance was very low.)

as it seems, you actually managed to make the moderation to delete your post with that absurd war declaration upon the poor djh.
they had to do that for the first time since netochka. congratulations.

yes, i raped your intent to make me believe that i do agree with dhj. i agree with him when decide to do so, and not when i am beeing told to.

and if you dont stop to agree with me when i want to disagree with you, i will not only rape your intent, but your internet.

btw. the most funny thing of all is that david, the OP, has decided to open a new thread to post his solution. lol.

-110

#266989
Oct 3, 2013 at 6:56pm

“make the moderation to delete your post”

nope not me(apparently you ARE reactionary and jump to quick wrongful conclusions ;D… if i even understand your broken english correctly :p :D).

“fungus…[blah blah blah]… rape….[blah blah blah]… i agree when decide to…[blah blah blah]…”

HAHA! you sound like me. my job here is done ;)

#266994
Oct 3, 2013 at 8:58pm
#266998
Oct 4, 2013 at 2:37am

OR and AND can be counter intuitive when mixing logical and arithmetic thinking. In natural language (at least in English), it’s common to use the word “and” in an additive context so it can seem confusing.

1 AND 0 = 1 * 0 = 0
1 OR 0 = 1 + 0 = 1

Likewise, there is a great deal of ambiguity in language regarding the inclusivity/exclusivity of ‘or’. If a parent says “Go directly home and clean your room. Don’t watch TV or play video games.” They don’t mean ‘take your pick’. So even though it’s less common, it’s understandable how “a or b or c” can sometimes be thought of including all three.

#267011
Oct 4, 2013 at 3:24am

Hey Metamax, though you’re right about inclusive or, this example you gave is not one :
it is NOT ( a OR b )
so 1 – ( a + b )
so that, if ( a OR b ) is true ; it can be either a = 1 , b = 0 OR a = 1, b = 1
which brings us back to the case of normal OR
(hm… there is a limit of arithmetic vs bitwise logical : 1 + 1 = 1…)
(or, in the case of a OR b, is it false if a is true and b is true too ? )

#267012
Oct 4, 2013 at 7:30am

thats a good question. if it would be, then OR and AND would mean the same.

where needed, you can always combine multiple booleans and check the input for OR OR AND or OR AND AND .. no wait, the latter will never be true.

but maybe one should not try to express or think of “if at least one of the inputs is true then output true” using OR OR AND at all.

i mean, it could be written like [if \$i1==1 && \$i2==1 then 1], and as you can see, it will of course also output true, when both inlets receive a 1.

for the exclusion mode (or however it is called) you have to use more than one boolean operator, for example:
[if (\$i1==1 && \$i2==0) || (\$i1==0 && \$i2==1) then 1]

but in this case i would prefer the arithmetic version and only check if the sum of a and b is zero or nonzero: [if (\$i1+\$i2) then 1]

btw, how do i write “clean your room or else!” in max? else \$10? else #0?

#267028
Oct 4, 2013 at 8:04am

I’ll also add this way of doing it: (using multiplication as an inverted or)
1 – (\$i1==0)*(\$i2==0)*(\$i3==0)

It has the advantage of being easy to implement with signals.

#267041
Oct 4, 2013 at 8:18pm

vichug, I may start sending you first drafts of my messages to prevent these kinds of problems. :)

I overcomplicated the simple point that natural language can be problematic when processing certain kinds of logic. But yeah, I totally agree that my example is best interpreted as NOR. It’s only true (i.e. what mom meant) when neither watching TV nor playing video games. I suppose that most examples of inclusive-or in language are implied/inflected more than they are explicit.

re: bit addition… you need to carry the 1. Bitwise addition is _twice_ the value of the overflow bits + whatver bits didn’t overflow.

In the case of 1 + 1, the first position overflowed and no positions didn’t overflow, so it’s 2*1 + 0 = 2

Consider that overflow only occurs when both A = 1 and B = 1. Also, a value of 1 with no overflow occurs whenever A is different than B. So A AND B gives you the carry (C). A XOR B gives you the values with no overflow (confusingly referred to as the ‘sum’). The true sum – addition proper – is 2C+S.

Logically:

A OR B = (A AND B) OR (A AND B) OR (A XOR B)

1 OR 1 = (1 AND 1) OR (1 AND 1) OR (1 XOR 1) = 1 OR 1 OR 0 = 1
1 OR 0 = (1 AND 0) OR (1 AND 0) OR (1 XOR 0) = 0 OR 0 OR 1 = 1
0 OR 1 = (0 AND 1) OR (0 AND 1) OR (0 XOR 1) = 0 OR 0 OR 1 = 1
0 OR 0 = (0 AND 0) OR (0 AND 0) OR (0 XOR 0) = 0 OR 0 OR 0 = 0

Arithmetically it holds:

(XOR = A+B-2AB)

A + B = (A * B) + (A * B) + (A+B-2AB)

1 + 1 = (1 * 1) + (1 * 1) + (1 + 1 – 2(1*1) = 1 + 1 + 0 = 2
1 + 0 = (1 * 0) + (1 * 0) + (1 + 0 – 2(1*0) = 0 + 0 + 1 = 1
0 + 1 = (0 * 1) + (0 * 1) + (0 + 1 – 2(0*1) = 0 + 0 + 1 = 1
0 + 0 = (0 * 0) + (0 * 0) + (0 + 0 – 2(0*0) = 0 + 0 + 0 = 0

-

– Pasted Max Patch, click to expand. –
#267080

You must be logged in to reply to this topic.