(Numeric) should -7 % 5 = -2 ?

Erik Max Francis max at alcyone.com
Tue Jul 1 21:41:26 EDT 2003


"Louis M. Pecora" wrote:

> We write  a=b mod m if m divides (a-b) (i.e. no remeinder).
> 
> The defintion does not say how to compute the mod, rather it is an
> expression of a relationship between a and b.  Hence, writing -2=-7
> mod
> 5 appears to be OK.

Right.  Equivalences modulo m are really alternate numerical spaces in
which arithmetic is done; in mathematics, the modulo is not strictly an
operator.  In those cases, you don't really have to pick a unique
residue when doing arithmetic (mod m), since it's all equivalence
relation anyway.

In computer science, where modulo is an operator that must return a
unique value, it's not really specified whether (-n % m) (m, n positive)
should be negative or not.  Some languages/systems chose negative, some
don't.  The choice is never "wrong" unless it's done inconsistently
within a particular language/system.

-- 
   Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ People are taught to be racists.
\__/  Jose Abad




More information about the Python-list mailing list