%在计算中是什么?我不知道它是做什么的。
它能算出计算的百分比吗例如4% 2显然等于0。如何?
%在计算中是什么?我不知道它是做什么的。
它能算出计算的百分比吗例如4% 2显然等于0。如何?
当前回答
像x % y这样的表达式的计算结果是x ÷ y的余数——嗯,从技术上讲,它是“模量”而不是“提醒”,所以如果您与%是余数运算符的其他语言进行比较,结果可能会有所不同。有一些微妙的区别(如果你对实际结果感兴趣,请参阅下面的“为什么Python的整数除法层”)。
优先级与运算符/(除法)和*(乘法)相同。
>>> 9 / 2
4
>>> 9 % 2
1
9除以2等于4。 4乘以2等于8 9 - 8 = 1 -余数。
Python漏洞:取决于你使用的Python版本,%也是(已弃用的)字符串插值运算符,所以如果你来自具有自动类型转换的语言(如PHP或JS),其中'12' % 2 + 3这样的表达式是合法的:在Python中,它将导致TypeError:在字符串格式化期间不是所有参数都转换,这可能会让你非常困惑。
[Python 3更新]
用户n00p评论:
9/2在python中是4.5。你必须像这样做整数除法:9//2如果你想让python告诉你除法(4)后剩下多少个整对象。
准确地说,整数除法曾经是Python 2中的默认值(请注意,这个答案比我的儿子还大,他在2岁时已经在上学了。X是主流):
$ python2.7
Python 2.7.10 (default, Oct 6 2017, 22:29:07)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 9 / 2
4
>>> 9 // 2
4
>>> 9 % 2
1
在现代Python 9 / 2中,结果确实是4.5:
$ python3.6
Python 3.6.1 (default, Apr 27 2017, 00:15:59)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 9 / 2
4.5
>>> 9 // 2
4
>>> 9 % 2
1
(更新)
用户dahiya_boy在评论区问道:
问:你能解释一下为什么- 11% 5 = 4 - dahiya_boy吗
这很奇怪,对吧?如果你在JavaScript中尝试这个:
> -11 % 5
-1
这是因为在JavaScript中%是“余数”运算符,而在Python中它是“模数”(时钟数学)运算符。
你可以直接从GvR得到解释:
Edit - dahiya_boy
在Java和iOS中- 11% 5 = -1,而在python和ruby中- 11% 5 = 4。
保罗·斯卡丁解释了一半的原因,剩下的解释在下面
在Java和iOS中,%给出的是余数,这意味着如果你除以11% 5,则商数= 2,余数= 1,而- 11% 5则商数= -2,余数= -1。
swift iOS中的示例代码。
但在python中,它给出了时钟模量。用下面的公式计算
mod(a,n) = a - {n * Floor(a/n)}
这意味着,
模(5)= 11号-{5 #楼(11/5)11 - {5}= > * 2}
会赢吗
And
mod(-11.5) = -11 - 5 * 地板(-11/5) => -11 - {5 * (-3)}
会赢吗
python 3.0中的示例代码。
为什么Python使用整数除法层 今天有人(又)问我为什么在Python中整数除法返回结果的下限,而不是像C语言那样截断到零。 对于正数,这并不奇怪:
>>> 5//2
2
但如果其中一个操作数是负的,结果会被取整,即从零四舍五入(直到负无穷):
>>> -5//2
-3
>>> 5//-2
-3
这让一些人感到困惑,但有一个很好的数学理由。整数除法运算(//)和它的兄弟,模数运算(%)一起使用,并满足一个很好的数学关系(所有变量都是整数):
a/b = q with remainder r
这样
b*q + r = a and 0 <= r < b
(assuming a and b are >= 0). If you want the relationship to extend for negative a (keeping b positive), you have two choices: if you truncate q towards zero, r will become negative, so that the invariant changes to 0 <= abs(r) < otherwise, you can floor q towards negative infinity, and the invariant remains 0 <= r < b. [update: fixed this para] In mathematical number theory, mathematicians always prefer the latter choice (see e.g. Wikipedia). For Python, I made the same choice because there are some interesting applications of the modulo operation where the sign of a is uninteresting. Consider taking a POSIX timestamp (seconds since the start of 1970) and turning it into the time of day. Since there are 24*3600 = 86400 seconds in a day, this calculation is simply t % 86400. But if we were to express times before 1970 using negative numbers, the "truncate towards zero" rule would give a meaningless result! Using the floor rule it all works out fine. Other applications I've thought of are computations of pixel positions in computer graphics. I'm sure there are more. For negative b, by the way, everything just flips, and the invariant becomes:
0 >= r > b.
So why doesn't C do it this way? Probably the hardware didn't do this at the time C was designed. And the hardware probably didn't do it this way because in the oldest hardware, negative numbers were represented as "sign + magnitude" rather than the two's complement representation used these days (at least for integers). My first computer was a Control Data mainframe and it used one's complement for integers as well as floats. A pattern of 60 ones meant negative zero! Tim Peters, who knows where all Python's floating point skeletons are buried, has expressed some worry about my desire to extend these rules to floating point modulo. He's probably right; the truncate-towards-negative-infinity rule can cause precision loss for x%1.0 when x is a very small negative number. But that's not enough for me to break integer modulo, and // is tightly coupled to that. PS. Note that I am using // instead of / -- this is Python 3 syntax, and also allowed in Python 2 to emphasize that you know you are invoking integer division. The / operator in Python 2 is ambiguous, since it returns a different result for two integer operands than for an int and a float or two floats. But that's a totally separate story; see PEP 238. Posted by Guido van Rossum at 9:49 AM
其他回答
The % (modulo) operator yields the remainder from the division of the first argument by the second. The numeric arguments are first converted to a common type. A zero right argument raises the ZeroDivisionError exception. The arguments may be floating point numbers, e.g., 3.14%0.7 equals 0.34 (since 3.14 equals 4*0.7 + 0.34.) The modulo operator always yields a result with the same sign as its second operand (or zero); the absolute value of the result is strictly smaller than the absolute value of the second operand [2].
摘自http://docs.python.org/reference/expressions.html
示例1: 6%2的结果为0,因为如果6除以2,没有余数(3倍)。
例2:7%2的结果是1,因为当7除以2(3次)时,余数是1。
总结一下,它返回除法运算的余数,如果没有余数,则返回0。6%2意味着6的余数除以2。
Python -基本操作符 http://www.tutorialspoint.com/python/python_basic_operators.htm
模数-左手操作数除以右手操作数并返回余数
A = 10, b = 20
B % a = 0
我很难轻易地找到% online使用的具体用例,例如。为什么分数模除法或负模除法会得到这样的结果。希望这有助于澄清以下问题:
一般情况下:
模除法返回数学除法运算的余数。它是这样做的:
假设我们有5的被除数和2的除数,下面的除法运算将是(等于x):
dividend = 5
divisor = 2
x = 5/2
模量计算的第一步是进行整数除法: X_int = 5 // 2 (python中的整数除法使用双斜杠) X_int = 2 接下来,将x_int的输出乘以除数: X_mult = x_int *除数 X_mult = 4 最后,从x_mult中减去红利 红利- x_mult = 1 因此,模运算返回1: 5% 2 = 1
应用程序将模数应用于分数
Example: 2 % 5
应用于分数时的模量计算与上述相同;但是,需要注意的是,当除数大于被除数时,整型除法的结果为零:
dividend = 2
divisor = 5
整数除法结果为0,而;因此,当执行上述步骤3时,红利的值将结转(从零减去):
dividend - 0 = 2 —> 2 % 5 = 2
应用程序将模量应用到负值
整除法发生时,整除法的值舍入到最小整数值:
import math
x = -1.1
math.floor(-1.1) = -2
y = 1.1
math.floor = 1
因此,当你做整数除法时,你可能会得到一个不同于你预期的结果!
将上述步骤应用于下面的除数和被除数,说明了模数的概念:
dividend: -5
divisor: 2
步骤1:应用整数除法
x_int = -5 // 2 = -3
第二步:用整数除数的结果乘以除数
x_mult = x_int * 2 = -6
第三步:从相乘的变量中减去红利,注意双重否定。
dividend - x_mult = -5 -(-6) = 1
因此:
-5 % 2 = 1
模运算符,通常用于整数的余数除法,但在Python中可用于浮点数。
http://docs.python.org/reference/expressions.html
The % (modulo) operator yields the remainder from the division of the first argument by the second. The numeric arguments are first converted to a common type. A zero right argument raises the ZeroDivisionError exception. The arguments may be floating point numbers, e.g., 3.14%0.7 equals 0.34 (since 3.14 equals 4*0.7 + 0.34.) The modulo operator always yields a result with the same sign as its second operand (or zero); the absolute value of the result is strictly smaller than the absolute value of the second operand [2].
模数-左手操作数除以右手操作数并返回余数。
如果有帮助的话:
1:0> 2%6
=> 2
2:0> 8%6
=> 2
3:0> 2%6 == 8%6
=> true
... 等等。