[M3devel] integer division/mod questions?

hendrik at topoi.pooq.com hendrik at topoi.pooq.com
Sun Jan 17 21:39:42 CET 2010


On Sun, Jan 17, 2010 at 11:08:15AM -0500, Tony Hosking wrote:
> On 17 Jan 2010, at 05:50, Jay K wrote:
> 
> > I think I missed a sign.
> > 
> > -2147483648 - 2147483647  * -2
> >  actually -2147483648 + 2 * 2147483647
> >   actually 2147483646
> > 
> > which agrees.
> > 
> > so 
> > 
> > -2147483648 div 2147483647 = -2 
> > -2147483648 mod 2147483647 = 2147483646 
> > 
> > -2 * 2147483647 + 2147483646  = -2147483648
> > 
> > I'll make sure m3_mod works this way if it doesn't already.
> > Presumably we are stuck with these rules.
> 
> Yep.  I seem to remember reading some rationale somewhere sometime somehow, but I forget where when how.  Anyone?

It gives results that satisfy important mathematical identities, such as

     (a + m) MOD m = a MOD m

which makes it easier to reason about the correctness of algorithms and 
program  transformations.

-- hendrik



More information about the M3devel mailing list