On 4/10/2011 4:27 AM, Tim Rentsch wrote:
> Eric Sosman<> writes:
>
>> On 4/9/2011 10:53 AM, dr.oktopus wrote:
>>> [...]
>>> What's the right (and smart) way?
>>
>> I don't know if this is "the" right or smart way, but:
>>
>> unsigned my_mod(int m, unsigned n) {
>> if (m< 0) {
>> unsigned um;
>> m += (int)n; /* now m> INT_MIN */
>> um = -m;
>> return n - um % n;
>> }
>> return m % n;
>> }
>
> Not quite right. Consider m == -n, for example.
Good catch; thanks. (That's what I get for considering only
the question asked and not thinking about the rest of the code...)
--
Eric Sosman
d