Q: Feature Wish: "%" Extension

Marcin 'Qrczak' Kowalczyk qrczak at knm.org.pl
Sun Nov 4 11:50:17 CET 2001


Sun, 4 Nov 2001 02:14:45 -0500, Tim Peters <tim.one at home.com> pisze:

>> Back when dinasaurs walked the earth, I had a long argument with the
>> Pascal standards committe about the correct definition of the modulus
>> operator.  Believe it or not, they had it so that (-3)%2 was a negative
>> number!
> 
> Most languages do, alas.

Among these types in OCaml:
  1. int: builtin, machine word size,
  2. big_int: unbounded,
  3. num: union of int, big_int and ratio,
all have different meanings of division and modulus! I wasn't aware
that there are more than two variants in use.

In each case separately division is consistent with modulus.
The following conditions hold in the respective cases:
  1. (-x) div y = -(x div y)
  2. 0 <= x mod y < |y|
  3. x div y = floor (x/y)

Intel processors and C99 do the first variant. Python does the third
variant. Knuth says the third variant is the true one. In SML and
Haskell the first variant is called quot/rem and the third is called
div/mod.

I recently needed to express quot/rem and div/mod in terms of the
first and the second variant. I think I haven't seen the second
variant before.

-- 
 __("<  Marcin Kowalczyk * qrczak at knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^
QRCZAK



More information about the Python-list mailing list