Integer Division

COTTRELL, JAMES cottrell at NBS-VMS.ARPA
Wed Mar 5 07:32:54 AEST 1986


/*
> > Is there someone out there who *wants* a/b to round towards 0 (for reasons
> > that say that is the desired result)?  I asked that before and have not seen
> > any affirmatives.
> > 
> > ucbvax!brahms!weemba	Matthew P Wiener/UCB Math Dept/Berkeley CA 94720
> 
> I do!  Mainly because I want the absolute value of (-a)/b to equal the absolute
> value of a/b.
> -- 
> -Pete Zakel (..!{hplabs,amd,pyramid,ihnp4}!pesnta!valid!pete)

Me too. Conceptually, `a/b' is `how many times does b go into a'? What
is left over has the same sign as the dividend. It seems that most
previously built computers truncate towards zero. Ironically, I also
prefer that `a%b' be positive, so that the `%' operator is actually the
`modulus' rather than the remainder. Thus we have the contradiction that
`a != (a/b)*b + (a%b)' for all possible a & b. But, as previously noted,
mostly we do `%' on positive integers. Of course, if b is a 
power of two, you can just `&' with `b - 1' to get the real modulus. 

	jim		cottrell at nbs
*/
------



More information about the Comp.lang.c mailing list