signed int modulo unsigned int produces nonsense results
问题 I need to perform a real mathematical modulo in C. It makes sense for me to allow negative numbers for the moduled argument, since my modular calculations can produce negative intermediate results, which must be put back into the least residue system. But it makes no sense to allow negative module, therefore i wrote unsigned int mod( int x, unsigned int m ) { int r = x % m; return r >= 0 ? r : r + m; } However calling such function with negative number and positive module printf("%u\n", mod(