how to loop a range of values

Morgan Souren's icon

Hello,
I hope someone can help me with this simple question.
I would like to assign an endless encoder to a dial for visualisation. So I use it as a 360.
So how can i make my values go from 0 to 360 and when reaching 361 go back to 0, and when reaching 720 go back to 0 and so on. I know its some sort of mathematical function but I can't quite get it. Thanks for your help!

M

Roman Thilenius's icon


if you dont need/have negative numbers (as input), you can use [% 360.]

otherwise - i dont know of any better method - you might want to use the wonderful 110.loopf ^^ https://cycling74.com/forums/modulo

there is also [piwrap] and [pong 0. 360. @mode wrap], depending on the application.

Morgan Souren's icon

Hey Roman, that % is what I ws trying to remember! Thank you very much!a

Roman Thilenius's icon


and while you probably know that, you wrote it wrong: 360 is 0, not 361. ;)

Morgan Souren's icon

You're right!

Bradley Korth's icon

Ooh, just for the record, if you DID want to loop numbers from 0 to 359 and have it include negative for an input, you could do % 360, then + 360, then % 360 again.

Roman Thilenius's icon


no matter how you want to display something, often when the range is 0-360 or 0-pi the place where you send it will treat the high & max values always the same no matter what is coming in. (it is not exactly min and max, it is always the same value when you think about it)

so whether you display 0 or 360 is mostly cosmetics, and you should adapt the math to your liking. but dont attempt to use offsets ("+ 1"?) that is too confusing.

i am often uncertain what looks better, 0 or 360, but if you want to display 360 you could "correct" the 0 to become 360 by using [route 0] - [t 360]

(or by using expr, but i am not)

(and there is that moment where you realize that eventually you should have converted everything to 0. - 1. before doing the math)


(most people dont know that the legal weeks first day is the sunday, not the monday!)

Morgan Souren's icon

Hello,
Thanks for your replies. Finally I decided to use pong in wrap mode. (thanks for the hint roman!).
Application is for use with an endless encoder. The idea now is to assign those values to a nodes object to control a surround installation.
Here is the the pong part

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

Have a nice evening.

M