[M3devel] integer division/mod questions?
Jay K
jay.krell at cornell.edu
Sun Jan 17 11:44:53 CET 2010
-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/065bb5f7/attachment-0001.html>
More information about the M3devel
mailing list