Is there an efficient way to get the least non-negative residue modulo n, where n is positive, in C?
This is quite easy if the number is non-negative, then it\'s jus
You could simply check if the result is negative and then act accordingly:
int mod(int n, int m) { int r = n % m; if (r < 0) return r + m; else return r; }
Or, without if-then-else and as a single expression:
r = ((n % m) + m) % m;