3 if statements in max/msp

Linea Janson's icon

Hej!
Im working with an accelerometer and Im trying to work with it around 3 if statements. How can I do the setup for the if statements properly.
Right now it won't work...
This is how the code look like.

Linnea

Skjermbilde-2015-11-03-kl.-11.08.51.png
png
Andro's icon

Not behind the computer.
Lot of mistakes there
So its hard to help.
Variable 2 (or 1 ?) is $f. Not possible it has to be $f1 or $f2.
Look at the help file.
Or is ||.
And is &&
To check two conditions in one statement you need ()
If ($f1 > 0.35 ) && ($f1 < 0.75) then 1. Else out2.

metamax's icon

Linnea, it looks like you are trying to output a value based on a range. For the sake of illustration, let's assume the following.

If the input is greater than 0.35 then output 2.
If the input is less than 0.35 but greater than or equal to 0.3 then output 1.
If the input is less than 0.3 then output -1.

You can use: [expr ($f1>=0.35)*2+(($f1>=0.3)&&($f1

That's because logical operators in [expr] are solved as 1 or 0. For example, [expr $f1>=0.35] is 0 or 1 depending on the input. The useful thing about this is you can MULTIPLY the expression by whatever you want for an output (because 0*whatever is 0 and 1*whatever is whatever). Hence, [expr ($f1>= 0.35)*2.] --> 0 or 2.

In other words, everything to the left of the * is the 'if' part of your statement and everything after is the 'then' part of your statement.

Even better, you can connect multiple conditional expressions together in a single [expr] object by ADDING each sub-expression (because the conditions that fail are 0 and 0 + 0... = 0). You just have to make sure that no more than one conditional sub-expression is true for a given input, otherwise you will be adding multiple values greater than 0. Check out the [expr] help file for logical operators.

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

Linea Janson's icon

thank you Metamax!

Luke Woodbury's icon
Max Patch
Copy patch and select New From Clipboard in Max.

If you don't like icky expressions ; ) you can do it with the help of [split]

meeble's icon

very useful information, METAMAX!

Roman Thilenius's icon

there are some more mistakes in the original picture than the synthax things.

it is simply not possible to get two different results at the same time from >= 0.3 and >= 0.35

so: what do you want to do? :)

and of course i second the suggestion to use expr the way meta discribed it. once you got the hang on it, it will be quite fun to extend it with further calculations, all in one object - in a way which is not possible using the boring if object.

metamax's icon

it is simply not possible to get two different results at the same time from >= 0.3 and >= 0.35

Yes, but with Max, all things are possible! :)

metamax's icon
Roman Thilenius's icon

u cheating me u use 2 objekt!

Peter Castine's icon

Cute example, Metamax. One of the objects is presumably comparing 32-bit representations while the other compares 64-bit representations (and in one of the two cases, converting the text representation to the one binary representation and then further converting to the other binary representation).

Floating point values still have gotchas, although compared to the implementations we worked with four or five decades ago they're very well behaved.

PS: You might want to check out http://www.h-schmidt.net/FloatConverter/IEEE754.html and type 0.35 into the "Decimal Representation" box.

metamax's icon

Peter Castine.. in the house!

I actually built a patch (after one of our conversations no less) that casts accurate double precision floats. Nothing more than a novelty.. but it's accurate. :) Not sure why Max doesn't have some way to access the info directly.

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