I need to make a function f(a,b)
that will give the following results:
f(0, 3) = 0
f(1, 3) = 1
f(2, 3) = 2
f(3, 3) = 0
f(4, 3) = 1... (this works exactly like a%b)
However it should also follow this pattern for negative numbers:
f(-4, 3) = 2
f(-3, 3) = 0
f(-2, 3) = 1
f(-1, 3) = 2
f( 0, 3) = 0
I currently found this solution:
x = a % b
return a < 0 && mod != 0 ? mod + b : mod;
However this feels way too complicated and slow for what I'm trying to achieve. Isn't there a simpler way to generate a sequence similar to modulo that continues for negative values?