Is anybody coming from Pascal?
Mikael Olofsson
mikael at isy.liu.se
Mon Jul 23 06:12:24 EDT 2001
On 21-Jul-2001 Stephen Horne wrote:
> Also, I'm not happy with the Python definition of integer division,
> and of the remainder...
>
> """
> The modulo operator always yields a result with the same sign as its
> second operand (or zero)
> """
>
> As far as I'm concerned, this is plain wrong. If I do the following...
>
> quotient = -3 / 2
> remainder = -3 % 2
>
> I expect quotient = -1 and remainder = -1
>
> I get quotient = -2 and remainder = 1
>
> Either version maintains the basic remainder law - given...
>
> quotient = a / b
> remainder = a % b
>
> You can still rely on...
>
> (quotient * b) + remainder == a
>
> I just think integer division should round towards zero - not strictly
> downwards.
In my research I toss a lot of finite fields around. These are mathematical
entities where modulo reduction is used all the time. The mathematical
concept of modulo is one of equivalence. If a, b, and c, are integers, you
say that a and b are equal (or equivalent) modulo c if there is an integer
d such that
a == b + c*d
holds. For instance
-5, -2, 1, 4
are all equal modulo 3. Therefore, I expect
>>> a % c == b % c
to evaluate as true if and only if a and b are equal modulo c (as
described above). Now, if
>>> quotient = -3 / 2
>>> remainder = -3 % 2
would result in quotient = -1 and remainder = -1, then I could not rely
on Python in this matter without (to me) unnecessary hacking. For the
mathemetically interrested, quotient = -2 and remainder = 1 should be
the right thing.
(Well... Unless
>>> quotient = 3 / 2
>>> remainder = 3 % 2
would result in quotient = 2 and remainder = -1.)
/Mikael
-----------------------------------------------------------------------
E-Mail: Mikael Olofsson <mikael at isy.liu.se>
WWW: http://www.dtr.isy.liu.se/dtr/staff/mikael
Phone: +46 - (0)13 - 28 1343
Telefax: +46 - (0)13 - 28 1339
Date: 23-Jul-2001
Time: 11:48:30
/"\
\ / ASCII Ribbon Campaign
X Against HTML Mail
/ \
This message was sent by XF-Mail.
-----------------------------------------------------------------------
More information about the Python-list
mailing list