Efficient way to get the least non-negative residue modulo n in C?

Posted by nn on Stack Overflow See other posts from Stack Overflow or by nn
Published on 2010-04-25T16:05:39Z Indexed on 2010/04/25 16:13 UTC
Read the original article Hit count: 383

Filed under:
|

Hi,

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 just a % n (where a is the non-negative integer).

However when a is negative, it appears the behaviour, in C89, is undefined. I.e. -2 % 11 = -2 or 9.

Thanks.

© Stack Overflow or respective owner

Related posts about c

    Related posts about math