Forums > MaxMSP

rollover when incrementing and decrementing a value

Jan 05 2011 | 9:26 pm

I need to variably increment and decrement a value, and have it return to the minimum value when a threshold is exceeded, and similarly, have it return to the MAXIMUM value when the mininum is exceeded.

So say my range is 1 to 16, and the current value is 16, if I increment then it should become 1. Conversely, if the value is 1 and I DECREMENT, then the value should go to 16.

The modulo operator (% ) seems to work for the incrementing case, but not for the decrementing case. Is there a good way to do this?

Jan 05 2011 | 9:52 pm
``````
-- Pasted Max  Patch, click to expand. --

```
```
Jan 05 2011 | 10:05 pm

That works well. I came up with this:

``````
-- Pasted Max  Patch, click to expand. --

```
```
Jan 05 2011 | 11:41 pm

the modulo would work for decrementing too – but it does not work for negative numbers.

try this for endless dials:

expr (((\$i1%\$i2)+(\$i2)*((\$i1%\$i2)==0))*(\$i1>=0))+((((\$i1%\$i2)+\$i2)+(-\$i2)*(((\$i1%\$i2)+\$i2)==0))*(\$i1<0))

where \$i2 is the range of the dial.

and likewise for float.

if it is sure that you are using numbers only within a smaller/limited range then

[+ 10000]
[% 16]
[- 10000]

should also work.

-110

Jan 18 2012 | 9:00 pm

(Necroing thread as I found a good solution)

— Pasted Max Patch, click to expand. —
Jan 18 2012 | 10:59 pm

I love these zombie threads! I see your maths+mod and raise you an [expr] to get it in one go!

[expr ((\$f1<0)*\$f2)+\$f1-(\$f2*int(\$f1/\$f2))]

Jan 18 2012 | 11:11 pm

That definitely is more elegant than that monster expr above. I do a ton of research on the forum so might as well leave ‘breadcrumbs’ for the next guy!

Jan 19 2012 | 1:12 am

i can make it longer if required. :D

Jan 19 2012 | 1:18 am

*cough* That’s not something I thought I’d ever see written on the Max forum!

Jan 19 2012 | 3:08 pm

Luke’s formula can be made a bit more compact for ints:

`[expr (\$i1 % \$i2) + (\$i1<0) * \$i2]`

A similar technique would work for floats if the expr object supported fmod(). On Max5 fmod isn't supported, but perhaps in Max6 (not installed on this machine, so I can't check).

The C-programmer in me is delighted by abusing the Boolean comparison as an implicit typecast to int (either 0 or 1). The nicely brought up, well-behaved PASCAL programmer in me is appalled by it. If you find multiplication with a comparison expression a little weird, it's easy enough to whip up a little abstraction that does the same thing. Something like the following:

``````
----------begin_max5_patcher----------
459.3ocwT80SBCCD+4sOEMM5Shj1x1XyDMwOGFhYrUjZ15HrRDkv2cWuR0oY
CFLDef0bWK2u+b85FWG7zh07RL5NzSHGmMtNNPJcBmcwN3730IYwkvwvEqTY
bEdfYqEwpj4B4KOujmnLkIhLjL.QCFqWX91unI69KhTScl95sThsNxU4BYUc
ALnemzfFjkrKaRQdNWBfgw5Tacc0eFzQAH4uUgsEXEesoT2XyXfT89BtQOXg
FrIsKXZHETLET7H1P+JESZVwgVTlUHUx3b.C7iKEwY02oT7ArCkMjzjCwZzg
nmO2PLCckfhd3dDAolykPDOqjipvioiZ0tvCp9sW+JR6SggfoY7t1rqwmG6p
4KTrSvtfZ1tz2mrC8.A6c3whfFmJHM2yOSSE8PXl9I0+vBy+ePXsbA+5SebG
jqe.bIdz9F1YWpgc.bblP96mxA5qy+SCprX0xDqd28rD5aIjxKUBYrRTHqcF
8i00NzbQZJWVu+kKRWTT4dkVehnMGpWndIJ5q.zjFaickqi+C3JKzxNRcpR5
IUC5.U0VOs6LMBtw4YdEg.K5fyfmR6.QOBKsmLh0Aqa7EkQcoYxNtlYOYjeG
Yzo5QUAac+T23fD0
-----------end_max5_patcher-----------
``````
Jan 19 2012 | 8:11 pm

to my excuse, mine allows offsets, i.e. a center another than 0

Viewing 11 posts - 1 through 11 (of 11 total)

Forums > MaxMSP