[M3devel] integer division/mod questions?
Tony Hosking
hosking at cs.purdue.edu
Sun Jan 17 17:08:15 CET 2010
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?
>
> - Jay
>
> From: jay.krell at cornell.edu
> To: m3devel at elegosoft.com
> Subject: integer division/mod questions?
> Date: Sun, 17 Jan 2010 10:44:53 +0000
>
> -2147483648 div 2147483647 ?
> -2147483648 mod 2147483647 ?
>
> quotient = -1, remainer = -1 seems reasonable.
> 2147483647 * -1 + -1 == -2147483648
>
>
> However, Modula-3 specifies div as rounding down, so
>
> -2147483648 div 2147483647 == -2
>
> and then
>
> http://www.modula3.com/cm3/doc/reference/arithmetic.html
>
> The value x DIV y is the floor of the quotient of x and y; that is, the maximum integer not exceeding the real number z such that z * y = x. For integers x and y, the value of x MOD y is defined to be x - y * (x DIV y). This means that for positive y, the value of x MOD y lies in the interval [0 .. y-1], regardless of the sign of x. For negative y, the value of x MOD y lies in the interval [y+1 .. 0], regardless of the sign of x.
>
> -2147483648 - 2147483647 * -2
> -2147483648 +2 (due to overflow)
> -2147483646
>
> which contradicts the second part.
>
> Maybe I'm confused? I should work this all through again?
>
> - Jay
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20100117/e8249050/attachment-0002.html>
More information about the M3devel
mailing list