%在计算中是什么?我不知道它是做什么的。
它能算出计算的百分比吗例如4% 2显然等于0。如何?
%在计算中是什么?我不知道它是做什么的。
它能算出计算的百分比吗例如4% 2显然等于0。如何?
当前回答
模量是一种数学运算,有时被描述为“时钟算术”。我发现把它简单地描述为一个余数是会误导和令人困惑的,因为它掩盖了它在计算机科学中被如此广泛使用的真正原因。它实际上是用来环绕循环的。
想象一个时钟:假设你看一个“军事”时间的时钟,时间范围从0点到23点59分。现在,如果你想在每天的午夜发生一些事情,你会希望当前时间mod 24为零:
如果(小时% 24 == 0):
你可以想象历史上所有的小时都围绕着24小时循环而今天的小时就是这个无限长的数字对24取余。它是一个比余数更深刻的概念,它是一种处理循环的数学方法,在计算机科学中非常重要。它还用于环绕数组,允许您增加索引,并在到达数组的末尾后使用模量返回到开头。
其他回答
有点偏离主题,%也用于字符串格式化操作,如%=,将值替换为字符串:
>>> x = 'abc_%(key)s_'
>>> x %= {'key':'value'}
>>> x
'abc_value_'
同样,跑题了,但它似乎是一个小小的文档特性,我花了一段时间才找到,我认为它与python模数计算有关,这个SO页面在这方面排名很高。
请注意
(3 +2 + 1 - 5) + (4 % 2) - (1/4) + 6
即使使用括号,在Python 3.4中计算的结果也是6.75,而不是7。
'/'操作符也不是那么容易理解(python2.7): try…
- 1/4
1 - 1/4
这有点离题,但在计算上面的表达式时应该考虑:)
在大多数语言中%表示模数。Python也不例外。
像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
我发现掌握模运算符(%)最简单的方法是通过长除法。它是余数,在确定一个数是偶数还是奇数时很有用:
4%2 = 0
2
2|4
-4
0
11%3 = 2
3
3|11
-9
2