Given a value n
, what would be the most efficient way of putting it in the range of [min, max]
such that it will repeat through the range if it goes beyond it?
I don't know the term for this, so it's difficult to phrase the question, but maybe a truth table might help:
Where min = -2
and max = 3
input --> output
-6 0
-5 1
-4 2
-3 3
-2 -2
-1 -1
0 0
1 1
2 2
3 -2
4 -1
5 0
6 1
What I have so far is this:
(value - min) % (max - min) + min;
Which works, but my worry is the modulus operator %
is unreliable for negative inputs, from what I've read (output becomes implementation defined, rendering it useless, or at least unportable)