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 ;)?
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 + , - , / , * , ** , // .
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.
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.
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.
There are actually three different definitions of "modulo" or "remainder", not two:
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…).
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With