I need to write an integer that "circulates" between a minimum and maximum value. If the maximum value is reached and you add 1, it jumps to the minimum value. If you subtract 1 from the minimum value you reach the maxValue.
Example:
minValue = 2;
maxValue = 10;
leads to: ... 2,3,4,...9,10,2,3,...
I figured out the addition algorithm, but I'm stuck with the subtraction. Addition looks like this:
public static circularInt operator +(circularInt a, int b)
{
int sum = a.value + b;
int circularValue = ((sum - a.minValue) % (a.maxValue + 1 - a.minValue)) + a.minValue;
return new circularInt(circularValue, a.minValue, a.maxValue);
}
Basically the algorithm can be broken down to "newValue % range". All the +- minValue is just to eliminate it from the calculation, and add it later on again.
Is there a known algorithm for this? If not, do you have an idea how the subtraction algorithm might look like?