## 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
• Jan 05 2011 | 10:05 pm
That works well. I came up with this:
• 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
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)
• 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
• 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
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:
• Jan 19 2012 | 8:11 pm
to my excuse, mine allows offsets, i.e. a center another than 0