find odd or float

Dg's icon

Hello,

I need to discard foat value and odd number.

How to know if a value is floating value (vs int) or if it is odd number (vs even).
Is there an object somewhere for that?

I searched th archive without result.

Thank you.

Emmanuel Jourdan's icon

route can separate int and float. Looking at the odd versus even number can be done by looking at the first bit, or with a modulo.

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

Dg's icon

thx e.j
ok for the odd versus even number.

But however I need to be able to detect if a value has decimal numbers or not (not if it is an int or a float, sorry).

jvkr's icon

When [t f i] to [- 0.] results in zero (or something very close to), there's no decimals.

_
johan

Bas van der Graaff's icon

Like this?

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

Dg's icon

I think to something simplier like this.
Anyway thx.

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

Dg's icon

odd or even

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

Roman Thilenius's icon

route odd or even:

[if ((abs($i1%2))==0) then $i1 else out2 $i1]

route int or float:

[route float int]

true/false for odd or even:

[expr ((abs($i1%2))==0)]

true/false for has decimals:

[expr (($f1-int($f1))!=0)]

-110

.

Peter Castine's icon

Roman Thilenius wrote on Tue, 22 September 2009 22:11route odd or even:

[if ((abs($i1%2))==0) then $i1 else out2 $i1]

You can simplify that to: [if %i1 % 2 then $i1 else out2 $i1]

The abs() and ==0 are superfluous.

(Note that this switches the odd/even sense of the outlets, but OP didn't specify a particular order)

Or, better still: [if %i1 & 1 then $i1 else out2 $i1]

Quote:true/false for odd or even:

[expr ((abs($i1%2))==0)]

Simply use a [& 1] object. (Again, switches true/false sense, but that wasn't specified. Again, abs() is superfluous)

Why make things more complex than they need to be?

Also, if you want finished abstractions for this sort of stuff, I'm pretty sure Karlheinz Essl's collection has all of these functions.

Roman Thilenius's icon

Peter CastineRoman Thilenius
route odd or even:

[if ((abs($i1%2))==0) then $i1 else out2 $i1]

The abs() and ==0 are superfluous.

[if %i1 & 1 then $i1 else out2 $i1]

the abs() was a careless mistake, i am used to float stuff.

but the if n % m then is very interesting. i will have to look into it.

Quote:Simply use a [& 1] object.

...!

-110

.

Szrp's icon

Peter Castine wrote on Wed, 23 September 2009 12:40
The abs() and ==0 are superfluous.

They certainly are, but many programmers find it bad form to shorten precisely spelt out comparisons like that, because it makes the code slightly less clearly readable on first sight. The same goes to some degree for using [& 1]. It's probably the most effective form, but less intuitively understandable for the average human. As long as you aren't desperately fighting for every last bit of processor power I don't see a need for that.

But I guess it really doesn't matter much either way. Personally I tend to shorten stuff a lot too (sometimes making said mistake of making my code less easily readable in the process).

Peter Castine's icon

I don't see that the abs() does much for code readability;-)

About == ... I've been a very, very long holdout against the C convention of implicitly typecasting any ints (of whatever size) to a Boolean. I grew up, after all, with Pascal, which was much stricter about typecasting. But recently I've started (finally? -- it took >20 years) to accept that C encourages implicit typecasts of ints and pointers to what, in Pascal parlance, would be a Boolean expression. Using this is part of the C idiom.

Also, I've noticed that the GCC compiler generates different code for

if (x != 0)

and

if (x)

with the latter generating more efficient object code. (Metrowerks, for all Unixfolk dissed it, was at least intelligent enough to generate the same object code for both source idioms.)

Likewise, bit-masking is an intrinsic and idiomatic part of C-coding.

I'll grant that most people patching Max probably have little or no experience with C, so for them the more verbose idioms may be more easily readable. Still, I think it's worth being aware that the more compact idioms are available.

Roman Thilenius's icon

and i believe that i am not the only one who did not know
that if (be it in a C compiler or in our max object)
already contains a comparison operator, which is by defaulttrue or !=0.

that a bitwise & 1 is more effective than a % 2 (which consists
of at least 3 operations) is not a wonder, but i am surprised
about the if (x)

if it is even more effective CPU-wise i will gladly adaptif (x) into a free region of my biological harddrive.

-110