8
votes

Is there any remainder operator in Python? I do not ask for modulo operator, but remainder. For example:

-5 mod 2 = 1

but

-5 rem 2 = -1 # where "rem" is a remainder operator.

Do I have to implement it by myself ;)?

3
Yes, you have to implement it yourself. - Katriel
@katrielalex but programming is hard :( - blakev
Do you really just want a truncated-division remainder, without having truncated division as well? - abarnert
"I do not ask for modulo operator, but remainder." You do understand those are the same things? See Wikipedia: "In computing, the modulo (sometimes called modulus) operation finds the remainder of division of one number by another." (Or do you just want different rules about negatives?) - Thanatos

3 Answers

21
votes

There are actually three different definitions of "modulo" or "remainder", not two:

  • Truncated division remainder: sign is the same as the dividend.
  • Floored division remainder: sign is the same as the divisor.
  • Euclidean division remainder: sign is always positive.

Calling one of them "modulo" and another "remainder" is very confusing; all three of them are useful definitions for both terms.

Almost every language only provides one of the three (Fortran being a notable exception).* Most languages provide the one that matches the language's division operator.** Because Python uses floored division (following Knuth's argument in The Art of Computer Programming), it uses the matching remainder operator.

If you want either of the other, you have to write it manually. It's not very hard; this Wikipedia article shows how to implement all three.

For example:

def trunc_divmod(a, b):
    q = a / b
    q = -int(-q) if q<0 else int(q)
    r = a - b * q
    return q, r

Now, for your example:

>>> q, r = trunc_divmod(-5, 2)
>>> print(q, r)
-2 -1

* Often languages that provide both call truncated remainder some variation on mod, and floored some variation on rem… but that definitely isn't something to rely on. For example, Fortran calls floored remainder modulo, while Scheme calls Euclidean remainder mod.

** Two notable exceptions are C90 and C++03, which leave the choice up to the implementation. While many implementations use truncated division and remainder, some do not (a few even use truncated division and floored remainder, which means a = b * (a/b) + a%b does not even work…).

2
votes

Edit: it's not entirely clear what you meant when you were asking for a remainder operation, the way to do this will depend on what requirements there are on the sign of the output.

If the sign is to be always positive divmod can do what you want, it's in the standard library

http://docs.python.org/2/library/functions.html#divmod

Also you might want to look at the built-in binary arithmetic operators:

http://docs.python.org/2/reference/expressions.html

If the remainder has to have the same sign as the the argument passed then you'd have to roll your own such as this:

import math
def rem(x,y):
    res = x % y
    return math.copysign(res,x)
0
votes

Does math.fmod do what you're looking for?