Python built-in functions (17) -- divmod, pythondivmod
English document:
divmod
(A,B)
Take two (non complex) numbers as arguments and return a pair of numbers consisting of their quotient and remainder when using integer division. with mixed operand types, the rules for binary arithmetic operators apply. for integers, the result is the same(a // b, a % b)
. For floating point numbers the result is(q, a % b)
, WhereQIs usuallymath.floor(a / b)
But may be 1 less than that. In any caseq * b + a % b
Is very closeA, Ifa % b
Is non-zero it has the same signB, And0 <= abs(a % b) < abs(b)
.
Note:
1. Accept two values (non-plural values), and return the operator obtained by the division of the two values, and a tuple consisting of the remainder.
2. If the parameters are all integers, the floor division is executed, which is equivalent(a//b,a%b)。
>>> divmod(5,2)(2, 1)>>> 5//22>>> 5%21
3. If the parameter is a floating point, it is equivalent (math.floor(a/b),a%b
).
>> divmod(5.5,2)(2.0, 1.5)>>> math.floor(5.5/2)2>>> 5.5/22.75>>> math.floor(5.5/2.0)2>>> 5.5%21.5