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?
That works well. I came up with this:
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
(Necroing thread as I found a good solution)
I love these zombie threads! I see your maths+mod and raise you an [expr] to get it in one go!
[expr (($f1
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!
i can make it longer if required. :D
*cough* That's not something I thought I'd ever see written on the Max forum!
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:
to my excuse, mine allows offsets, i.e. a center another than 0