modulo negative

David Clements's icon

Hi.
Please see attached. When you move the incdec, it cycles around 1-8, but if you go into negative numbers, it breaks the cycle. Any suggestions?
Cheers

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

Source Audio's icon

depends on what you want in minus range.
insert abs at the end and you get
-1 = 1
you can use
expr abs($i1%8)
instead for same effect.
if you want
-1 = 7

then

expr abs($i1%8+8) %8

David Clements's icon

expr abs($i1%8+8) %8
This is great, except for one thing. When it gets to 8, i get a zero. This is musical bars, so i want the range to be 1 -8.
I could fix it with a [sel 0], but that feels kind of ugly. Any way to fix that within the [expr] object?

Thanks

Source Audio's icon

expr abs($i1%8+8) %8 + 1

Roman Thilenius's icon


[pong 1. 8. @mode wrap] ?

or save this as [110.loopi]

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

low end of the range is always 1 (i once made it for the same purpose), high end has to be set as argument for $f2

Roman Thilenius's icon


this will also work, maybe it is a bit shorter and better to control and modify for third parties, compared to the loopi bandworm above.


David Clements's icon

That last one is perfect. Thanks Roman. Thanks all.

Roman Thilenius's icon


when i think about it, you could also use [substitute 0 #1] instead of the lower expr.

i am amazed how some of my tools shrink in size over the years. :D

David Clements's icon

haha, brilliant. Eventually all our patches will just have 2 objects and work perfectly :)

David Clements's icon

actually, that doesn't work. I just get 0

David Clements's icon

doesn't work for me anyway. Here's a bit more context.

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

Roman Thilenius's icon


maybe i did something wrong; i quickly tried that in max 8 yesterday and only connected a numberbox ... this does not make sure that substitute actually sends out message type LONG... which i cant tell offhand now. ;)

but the other alternatives are [select 0] [t 16], which is very unsexy, and [if], which consumes even more CPU than an [expr] in int mode.

(in a scheduler context i usually go by that rule that if you need more than 3 objects to avoid a überlong expression, then the chance is higher that expür is more effective.)

David Clements's icon

Ah, it's fine. The previous solution works perfectly anyway :-)