Integer division

Spencer W. Thomas thomas at utah-gr.UUCP
Sat Feb 1 04:23:41 AEST 1986


There is of course, always another way to look at it.  The '%' operator
is not defined to be MOD, it is defined such that
	(a/b)*b + a%b = a
In other words, it is the REMAINDER upon dividing a by b.  Now, if you
want a%b to always be positive, you must then have
	(-a)/b != -(a/b)
which, I think you will agree, is much worse.  If you really want MOD,
here it is:
mod(a,b)
{
	return (( a % b ) + b) % b;
}

-- 
=Spencer   ({ihnp4,decvax}!utah-cs!thomas, thomas at utah-cs.ARPA)



More information about the Comp.lang.c mailing list