## rollover when incrementing and decrementing a value

Jan 5, 2011 at 9:26pm

# rollover when incrementing and decrementing a value

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?

#54270
Jan 5, 2011 at 9:52pm
– Pasted Max Patch, click to expand. –
#195308
Jan 5, 2011 at 10:05pm

That works well. I came up with this:

– Pasted Max Patch, click to expand. –
#195309
Jan 5, 2011 at 11:41pm

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

#195310
Jan 18, 2012 at 9:00pm

(Necroing thread as I found a good solution)

– Pasted Max Patch, click to expand. –

#195311
Jan 18, 2012 at 10:59pm

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))]

#195312
Jan 18, 2012 at 11:11pm

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!

#195313
Jan 19, 2012 at 1:12am

i can make it longer if required. :D

#195314
Jan 19, 2012 at 1:18am

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

#195315
Jan 19, 2012 at 3:08pm

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-----------
``````
#195316
Jan 19, 2012 at 8:11pm

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

#195317

You must be logged in to reply to this topic.