Hello again,
I searched the def of int division in R. I could not find it in the english wikipedia, but in the french one, there was http://fr.wikipedia.org/wiki/Division_enti%C3%A8re#Division_euclidienne_dans...: for each pair (a,b) in ZxZ, there exists (q,r) in ZxZ | a = bq + r | |r| < |b| This implies that if a xor b is negative, thare are 2 of solutions: the one where (q,r) is equal in absolute value to the solution in N, and (q-1,r+b). So, in the case of -4/3, we have the choice between (-1,-1) and (-2,2).
The following discussion in the article does not tell whether one solution is the _official_ one. But: that -4/3 != -(4/3) looks simply wrong for me.
Denis
vit esse estrany ☣
spir.wikidot.com
2010/5/7 spir ☣ denis.spir@gmail.com
The following discussion in the article does not tell whether one solution is the _official_ one. But: that -4/3 != -(4/3) looks simply wrong for me.
Denis
I believe the official decision relates to PEP 238:
http://www.python.org/dev/peps/pep-0238/
And Guido was one of the authors of the PEP, so the chances are it won't be changed for a long time.
Cheers, Xav
(PS: Yes, I prefer C's integer division, where it's always rounded towards 0 (truncation), not just a straight floor.)
On Fri, May 7, 2010 at 9:19 AM, Xavier Ho contact@xavierho.com wrote:
2010/5/7 spir ☣ denis.spir@gmail.com >
The following discussion in the article does not tell whether one solution is the _official_ one. But: that -4/3 != -(4/3) looks simply wrong for me.
Denis
I believe the official decision relates to PEP 238:
No: PEP 238 is (partly) about how integer division is expressed in Python; not about its semantics. Python's choice for integer division with negative arguments goes back much further. From Misc/HISTORY:
==================================
[...]
New features in 0.9.6:
[...]
Personally, I've always liked Python's behaviour in this regard: for the few times that I've needed an 'x % y' operation that works with both positive and negative x, more often than not x-y*floor(x/y) turns out to be what I need. I've lost count of the number times I've had to write something awkward like:
/* adjust for the exponent; first reduce it modulo _PyHASH_BITS */
e = e >= 0 ? e % _PyHASH_BITS : _PyHASH_BITS-1-((-1-e) % _PyHASH_BITS);
in C.
-- Mark
2010/5/7 spir ☣ denis.spir@gmail.com:
I searched the def of int division in R. I could not find it in the english wikipedia,
On page 4 of Gallian, "Contemporary Abstract Algebra", we have:
Division Algorithm
Let a and b be integers with b > 0. Then there exist unique
integers q and r with the property that a = bq + r where
0 <= r < b.
This is, of course, the definition Python uses. I think this is pretty standard. What might mathematicians like about this definition? Well, I think the fundamentally important thing about integer division (or any mathematical object really) is the patterns it makes:
>>> [a % 5 for a in range(20)]
[0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4]
>>> [a // 5 for a in range(20)]
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3]
Those patterns show up in both C and Python. Do the patterns continue as you go into the negative numbers? In Python they do:
>>> [a % 5 for a in range(-10, 10)]
[0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4]
>>> [a // 5 for a in range(-10, 10)]
[-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
In C, the patterns change as you pass 0.
That is, the Python definition satisfies these mathematical properties, and the C definition doesn't:
(a + b) // b == a // b + 1
(a + b) % b == a % b
The Python definition agrees with modulo arithmetic:
-3 ≡ 2 (mod 5)
http://en.wikipedia.org/wiki/Modular_arithmetic
In Python, -3 % 5 == 2 % 5 is true. In C it is false.
But: that -4/3 != -(4/3) looks simply wrong for me.
You can either have the mirror symmetry about 0 that you want, or you can have the translational symmetry shown above. I think translational symmetry is the defining thing about integer division and therefore more important.
Of course for a programming language the question of which definition to use is a practical one: which is more useful? Ultimately practicality beats purity. But as far as purity goes (and it goes along with practicality a good long way) I think Python's integer division wins by a wide margin.
Cheers, -j
If I can diffidently put myself forward as a mathematician of sorts (not a professional one):
I agree with everything that Jason says here.
Mathematics is the study of abstract patterns. (No doubt, this is not an original observation.)
Rob Cliffe
----- Original Message ----- From: "Jason Orendorff" jason.orendorff@gmail.com To: "spir ☣" denis.spir@gmail.com Cc: "python ideas" python-ideas@python.org Sent: Friday, May 07, 2010 3:20 PM Subject: Re: [Python-ideas] integer dividion in R -- PS
2010/5/7 spir ☣ denis.spir@gmail.com:
I searched the def of int division in R. I could not find it in the english wikipedia,
On page 4 of Gallian, "Contemporary Abstract Algebra", we have:
Division Algorithm
Let a and b be integers with b > 0. Then there exist unique integers q and r with the property that a = bq + r where 0 <= r < b.
This is, of course, the definition Python uses. I think this is pretty standard. What might mathematicians like about this definition? Well, I think the fundamentally important thing about integer division (or any mathematical object really) is the patterns it makes:
[a % 5 for a in range(20)] [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4] [a // 5 for a in range(20)] [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3]
Those patterns show up in both C and Python. Do the patterns continue as you go into the negative numbers? In Python they do:
[a % 5 for a in range(-10, 10)] [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4] [a // 5 for a in range(-10, 10)] [-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
In C, the patterns change as you pass 0.
That is, the Python definition satisfies these mathematical properties, and the C definition doesn't:
(a + b) // b == a // b + 1 (a + b) % b == a % b
The Python definition agrees with modulo arithmetic:
-3 ≡ 2 (mod 5) http://en.wikipedia.org/wiki/Modular_arithmetic
In Python, -3 % 5 == 2 % 5 is true. In C it is false.
But: that -4/3 != -(4/3) looks simply wrong for me.
You can either have the mirror symmetry about 0 that you want, or you can have the translational symmetry shown above. I think translational symmetry is the defining thing about integer division and therefore more important.
Of course for a programming language the question of which definition to use is a practical one: which is more useful? Ultimately practicality beats purity. But as far as purity goes (and it goes along with practicality a good long way) I think Python's integer division wins by a wide margin.
Cheers, -j
Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas