
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 = b*q + 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>
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:
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: ================================== ==> RELEASE 0.9.6 (6 Apr 1992) <== ================================== [...] New features in 0.9.6: [...] - Division and modulo for long and plain integers with negative operands have changed; a/b is now floor(float(a)/float(b)) and a%b is defined as a-(a/b)*b. So now the outcome of divmod(a,b) is the same as (a/b, a%b) for integers. For floats, % is also changed, but of course / is unchanged, and divmod(x,y) does not yield (x/y, x%y)... [...] 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 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:
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: ================================== ==> RELEASE 0.9.6 (6 Apr 1992) <== ================================== [...] New features in 0.9.6: [...] - Division and modulo for long and plain integers with negative operands have changed; a/b is now floor(float(a)/float(b)) and a%b is defined as a-(a/b)*b. So now the outcome of divmod(a,b) is the same as (a/b, a%b) for integers. For floats, % is also changed, but of course / is unchanged, and divmod(x,y) does not yield (x/y, x%y)... [...] 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
participants (5)
-
Jason Orendorff
-
Mark Dickinson
-
Rob Cliffe
-
spir ☣
-
Xavier Ho