Type this into a script:
for (int i = -10; i < 10; i++) {
Debug.Log(i + "-> " + (i % 3));
}
I kid you not this is what you get in the negative range:
-3 → 0
-2 → -2
-1 → -1
Modulus is supposed to give me values from [0…3] but I’m getting negative numbers! Check with Google for proof. Anybody else get this BAD MATH?
Lovrenc
2
Direct copry from SO forum:
Note that C# and C++'s % operator is actually NOT a modulo, it’s remainder. The formula for modulo that you want, in your case, is:
float nfmod(float a,float b)
{
return a - b * floor(a / b)
}
See the modulo operator on wikipedia. Also take a look at the list at the right side. Almost all programming languages return a signed remainder.