% operator with negatives

Kevin Coombes kevin at math.lsa.umich.edu
Sat Dec 15 02:37:06 AEST 1990


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>



More information about the Comp.lang.c mailing list