Path: utzoo!attcan!uunet!zaphod.mps.ohio-state.edu!sdd.hp.com!caen!math.lsa.umich.edu!math.lsa.umich.edu!kevin From: kevin@math.lsa.umich.edu (Kevin Coombes) Newsgroups: comp.lang.c Subject: Re: % operator with negatives Message-ID: <1990Dec14.153706.14688@math.lsa.umich.edu> Date: 14 Dec 90 15:37:06 GMT References: <7461:Dec1310:04:0990@kramden.acf.nyu.edu> <18140@neptune.inf.ethz.ch> <1990Dec14.064305.4491@dirtydog.ima.isc.com> Sender: usenet@math.lsa.umich.edu Organization: University of Michigan Math Dept., Ann Arbor Lines: 15 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