Manual
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 as (a // b, a % b). For floating point numbers the result is (q, a % b), where q is usually math.floor(a / b) but may be 1 less than that. In any case q * b + a % b is very close to a, if a % b is non-zero it has the same sign as b, and 0 <= abs(a % b) < abs(b).
直译
取得两个数字(非复数)做参数,并返回一对数,其中包含它们的商和余数(当使用整数除法时)。混合运算类型下,支持二进制算数运算符。对于整数,结果等同于(a // b, a % b)。对于浮点数,结果等同于(q, a % b),其中q通常是math.floor(a / b),但可能比它小1。任何情况下,q * b + a % b都非常接近a,若a % b非零,它与b符号相同,且0 <= abs(a % b) < abs(b)。
实例
>>> divmod(2,4)
(0, 2)
>>> divmod(5, 2)
(2, 1)
>>> a = 5.3
>>> b = 3.14
>>> a_div_b = divmod(a, b)
>>> q = a_div_b[0]
>>> q
1.0
>>> x = q * b + a % b
>>> x == a
True
本文详细介绍了Python中divmod函数的功能及用法。该函数接受两个非复数数字作为输入,返回一个包含整数除法得到的商和余数的元组。文章通过实例展示了整数和浮点数情况下divmod的不同表现。
866

被折叠的 条评论
为什么被折叠?



