Limiting numbers to maximum values?

Peter Ostry's icon

How do you usually limit integers to a certain maximum? For example everything in an incoming stream which is above 120 should give 120. No intelligence needed, just set it to the allowed maximum if it is higher.

[scale] cannot do it, it delivers numbers outside of its given output range. The number box does it and I could use the [if ...] object but I think there is a more common way.

MIB's icon

[clip 0 120] ?

Chris Muir's icon

[clip] is good, [split] can sort of do it, [zmap] can do it, but is overkill.

Peter Ostry's icon

Ah, thanks.

[zmap] is actually good in this case. I [scale] already but it lets the data shoot over the range. With [zmap] this doesn't even occur and I don't need to [clip].

Gregory Taylor's icon

Another (possibly even simpler) approach is to set the "maximum" attribute of a number box - possibly even the one you're using in your UI.

Chris Muir's icon

He alludes to that by saying "The number box does it..."

I assumed he was looking for another method, possibly to reduce cpu cycles from the changing graphics.

Peter Ostry's icon

Yes Chris, this is correct.
The application is going to have many objects and I spare computer energy wherever I can – which takes some energy from me. Mister Newton was right ;-)

Roman Thilenius's icon

zmap?? split?? :D

there is a [maximum 120] object for this task, or for that matter, [expr max($f1,120.)]

Chris Muir's icon

As always, when talking about efficiency, measurements are good. Somewhat surprisingly, zmap wins here.

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

In my experience, [expr] is often the worst candidate, at least as far as efficiency is concerned, but in this case, it's in the running. (Some people may find [expr] more readable for complex operations than a bunch of Max code, though.)

Roman Thilenius's icon

... but how can you do maximum with zmap or with split? both cut off values outside the range.

Chris Muir's icon
Max Patch
Copy patch and select New From Clipboard in Max.

If I'm understanding you, zmap does the right thing. [split] would need a little trickery with the right outlet which would negate any advantage to using it in this case.

Roman Thilenius's icon

well i thought his question made quite clear that he was looking for maximum or clip, but i could be wrong.

Mathieu Chamagne's icon

if you want to clip to max 120, you must use [minimum 120], not [maximum] ...

Roman Thilenius's icon

also true.