标签归档:integer-division

找出数字的除法余数

问题:找出数字的除法余数

如何在Python中找到数字的除法余数?

例如:
如果数字为26,除数为7,则除法余数为5。
(因为7 + 7 + 7 = 21和26-21 = 5。)

How could I go about finding the division remainder of a number in Python?

For example:
If the number is 26 and divided number is 7, then the division remainder is 5.
(since 7+7+7=21 and 26-21=5.)


回答 0

您正在寻找模运算符:

a % b

例如:

26 % 7

当然,也许他们希望您自己实现它,这也不会太困难。

you are looking for the modulo operator:

a % b

for example:

26 % 7

Of course, maybe they wanted you to implement it yourself, which wouldn’t be too difficult either.


回答 1

除法的其余部分可以使用运算符发现%

>>> 26%7
5

如果需要商和模,则有内置divmod函数:

>>> seconds= 137
>>> minutes, seconds= divmod(seconds, 60)

The remainder of a division can be discovered using the operator %:

>>> 26%7
5

In case you need both the quotient and the modulo, there’s the builtin divmod function:

>>> seconds= 137
>>> minutes, seconds= divmod(seconds, 60)

回答 2

26%7(您将得到余数)

26/7(您将获得除数可以是浮点值)

26 // 7(您将只获得除数的整数值))

26 % 7 (you will get remainder)

26 / 7 (you will get divisor can be float value )

26 // 7 (you will get divisor only integer value) )


回答 3

如果要在一行代码中获得商和余数(更一般的用例),请使用:

quotient, remainder = divmod(dividend, divisor)
#or
divmod(26, 7)

If you want to get quotient and remainder in one line of code (more general usecase), use:

quotient, remainder = divmod(dividend, divisor)
#or
divmod(26, 7)

回答 4

如果要避免取模,还可以结合使用四个基本操作:)

26 - (26 // 7 * 7) = 5

If you want to avoid modulo, you can also use a combination of the four basic operations :)

26 - (26 // 7 * 7) = 5

回答 5

从Python 3.7开始,有一个新math.remainder()函数:

from math import remainder
print(remainder(26,7))

输出:

-2.0  # not 5

注意,如上所述,这是不一样%

引用文档

数学。余数(x,y)

返回关于y的x的IEEE 754样式余数。对于有限x和有限非零y,这是x-n * y的差,其中n是最接近商x / y精确值的整数。如果x / y恰好位于两个连续整数之间,则n使用最接近的偶数整数。因此,余数r =余数(x,y)始终满足abs(r)<= 0.5 * abs(y)。

特殊情况遵循IEEE 754:特别是,对于任何有限x,remainder(x,math.inf)为x,而对任何非NaN x而言,remainder(x,0)和restder(math.inf,x)都会引发ValueError。如果余数运算的结果为零,则该零将具有与x相同的符号。

在使用IEEE 754二进制浮点的平台上,此操作的结果始终可以准确表示:不引入舍入错误。

Issue29962描述了创建新功能的原理。

From Python 3.7, there is a new math.remainder() function:

from math import remainder
print(remainder(26,7))

Output:

-2.0  # not 5

Note, as above, it’s not the same as %.

Quoting the documentation:

math.remainder(x, y)

Return the IEEE 754-style remainder of x with respect to y. For finite x and finite nonzero y, this is the difference x – n*y, where n is the closest integer to the exact value of the quotient x / y. If x / y is exactly halfway between two consecutive integers, the nearest even integer is used for n. The remainder r = remainder(x, y) thus always satisfies abs(r) <= 0.5 * abs(y).

Special cases follow IEEE 754: in particular, remainder(x, math.inf) is x for any finite x, and remainder(x, 0) and remainder(math.inf, x) raise ValueError for any non-NaN x. If the result of the remainder operation is zero, that zero will have the same sign as x.

On platforms using IEEE 754 binary floating-point, the result of this operation is always exactly representable: no rounding error is introduced.

Issue29962 describes the rationale for creating the new function.


回答 6

除法时,请使用%而不是/。这将为您退还剩余款项。所以你的情况

26 % 7 = 5

Use the % instead of the / when you divide. This will return the remainder for you. So in your case

26 % 7 = 5

回答 7

模数是正确的答案,但是如果您手动执行此操作,则应该可以。

num = input("Enter a number: ")
div = input("Enter a divisor: ")

while num >= div:
    num -= div
print num

Modulo would be the correct answer, but if you’re doing it manually this should work.

num = input("Enter a number: ")
div = input("Enter a divisor: ")

while num >= div:
    num -= div
print num

回答 8

我们可以通过使用模数运算符(%)来解决此问题

26%7 = 5;

但是26/7 = 3,因为它将给出商,但%运算符将给出余数。

We can solve this by using modulus operator (%)

26 % 7 = 5;

but 26 / 7 = 3 because it will give quotient but % operator will give remainder.


回答 9

您可以使用模运算符找到余数

a=14
b=10
print(a%b)

它将打印4

You can find remainder using modulo operator Example

a=14
b=10
print(a%b)

It will print 4


回答 10

如果您想要除法问题的余数,只需使用实际的余数规则,就像在数学中一样。授予它不会给您十进制输出。

valone = 8
valtwo = 3
x = valone / valtwo
r = valone - (valtwo * x)
print "Answer: %s with a remainder of %s" % (x, r)

如果要使用计算器格式,只需将替换valone = 8 为即可valone = int(input("Value One"))。对进行相同的操作valtwo = 3,但显然不同。

If you want the remainder of your division problem, just use the actual remainder rules, just like in mathematics. Granted this won’t give you a decimal output.

valone = 8
valtwo = 3
x = valone / valtwo
r = valone - (valtwo * x)
print "Answer: %s with a remainder of %s" % (x, r)

If you want to make this in a calculator format, just substitute valone = 8 with valone = int(input("Value One")). Do the same with valtwo = 3, but different vairables obviously.


回答 11

您可以定义一个函数并使用2个值来调用余数,例如rem(number1,number2),该函数返回number1%number2,然后创建一阵子并将其设置为true,然后为函数保留两个输出,分别输入数字1和2,然后打印出(rem (数字1,数字2)

you can define a function and call it remainder with 2 values like rem(number1,number2) that returns number1%number2 then create a while and set it to true then print out two inputs for your function holding number 1 and 2 then print(rem(number1,number2)