Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python remainder operator

Tags:

python

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 ;)?

like image 291
Dejwi Avatar asked Aug 28 '13 22:08

Dejwi


People also ask

How do you use the remainder operator in Python?

The % symbol in Python is called the Modulo Operator. It returns the remainder of dividing the left hand operand by right hand operand. It's used to get the remainder of a division problem. The modulo operator is considered an arithmetic operation, along with + , - , / , * , ** , // .

Which operator in Python gives remainder?

Python supports a wide range of arithmetic operators that you can use when working with numbers in your code. One of these operators is the modulo operator ( % ), which returns the remainder of dividing two numbers.

What is operator for remainder?

Remainder (%) The remainder operator ( % ) returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend.

What is MOD () in Python?

Overview. Modulus is an arithmetic operation that gives the remainder when the first number is divided from the second number. In Python, we achieve this operation using the modulus operator which is represented by the percentage symbol.


2 Answers

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…).

like image 140
abarnert Avatar answered Oct 03 '22 14:10

abarnert


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)
like image 25
shuttle87 Avatar answered Oct 03 '22 13:10

shuttle87