Modulo operator for array indexing

James Harkins's icon

For array indexing, you need integer modulo such that 0 <= (x % n) < n.

In Max, if x is negative, then it's -n < (x % n) <= 0, which will not work for array indexing. (It seems that Max array-like objects (itable etc) clip out-of-range indices, and I didn't see an option to wrap indices -- so a negative result from [%] will not help for array indexing.)

Pd provides [%] for the Max modulo behavior, and [mod] for an always-positive result.

Is there a [mod] in Max? I sorta worked around it by x - (n * floor(x / n)) with gen stuff for the right-hand term, but it seems rather a long way to go around for a standard operator.

Thanks.
hjh

Graham Wakefield's icon

There is in gen~, using [wrap 0 N] .

James Harkins's icon

OK... still a bit amusing to build an abstraction for this.

The use case that set this off was mapping scale degrees to semitones. The table for one octave's worth of semitone values needs to be indexed by `degree % scaleSize` and if you set middle C to degree 0, then negative degrees are a thing. I've done this completely transparently in Pd and SC, but got "gotcha-ed" in front of a student yesterday in Max.

hjh