f you know what types you're working with and you're worried about what happens with negatives, you can always use the following function, which is guaranteed to return an answer between 0 and p-1. int safemod(int a, int p) { if (a < 0) return p - ((-a) % p); else return a % p; } Best, Kevin <krc at mayme.umd.edu>