[Tutor] The remainder %
Bob Gailer
bgailer@alum.rpi.edu
Tue Jun 3 13:37:01 2003
OOPS I did not do my homework. So it turns out that Python % and divmod DO
modulo, and the documentation is in error when it says "remainder". It sort
of redeems itself later "The integer division and modulo operators are
connected by the following identity: x == (x/y)*y + (x%y)."
From the interpreter window:
>>> for i in range(3, -4, -1):i, i % 3
...
(3, 0)
(2, 2)
(1, 1)
(0, 0)
(-1, 2)
(-2, 1)
(-3, 0)
Other languages that I know just say "modulo" and deliver remainder.
At 01:17 PM 6/3/2003 -0400, Lloyd Kvam wrote:
>PythonWin 2.2.1 (#34, Apr 15 2002, 09:51:39) [MSC 32 bit (Intel)] on win32.
>Portions Copyright 1994-2001 Mark Hammond (mhammond@skippinet.com.au) -
>see 'Help/About PythonWin' for further copyright information.
> >>> -1 % 3
>2
>
>Bob Gailer wrote:
>>At 02:09 PM 6/3/2003 +0200, Guillaume wrote:
>>
>>>Hello,
>>>I'm not very good in maths and I don't understand why
>>>14 % 3 = 2
>>>Could someone explain me this mystery?
>>>Thanks in advance :)
>>
>>What's really frustrating here is that the language reference says "%
>>(modulo) operator yields the remainder..."
>>In math modulo is NOT the same as remainder. At least the explanation
>>says "remainder". Modulo and remainder have the same values when the left
>>argument is >= 0, and different values for < 0. Example:
>>n n modulo 3 n % 3
>>3 0 0
>>2 2 2
>>1 1 1
>>0 0 0
>>-1 2 1
>>-2 1 2
>>-3 0 0
>>The only programming language I know that "got is right" is APL. There
>>the function is named modulo and it DOES modulo. Most other languages
>>call it modulo and it does remainder.
>>Bob Gailer
>>bgailer@alum.rpi.edu
>>303 442 2625
>
>
>--
>Lloyd Kvam
>Venix Corp.
>1 Court Street, Suite 378
>Lebanon, NH 03766-1358
>
>voice: 603-443-6155
>fax: 801-459-9582
>
>
>
>
>
>---
>Incoming mail is certified Virus Free.
>Checked by AVG anti-virus system (http://www.grisoft.com).
>Version: 6.0.483 / Virus Database: 279 - Release Date: 5/19/2003
Bob Gailer
bgailer@alum.rpi.edu
303 442 2625