Python 中的模运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12754680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Modulo operator in Python
提问by KodeWarrior
What does modulo in the following piece of code do?
下面这段代码中的模有什么作用?
from math import *
3.14 % 2 * pi
How do we calculate modulo on a floating point number?
我们如何计算浮点数的模数?
回答by Blender
When you have the expression:
当你有表达式时:
a % b = c
It really means there exists an integer nthat makes cas small as possible, but non-negative.
这实际上意味着存在一个正整数n,使c尽可能小,但非负。
a - n*b = c
By hand, you can just subtract 2(or add 2if your number is negative) over and over until the end result is the smallest positive number possible:
手动,您可以一遍又一遍地减去2(2如果您的数字为负,则添加),直到最终结果是可能的最小正数:
3.14 % 2
= 3.14 - 1 * 2
= 1.14
Also, 3.14 % 2 * piis interpreted as (3.14 % 2) * pi. I'm not sure if you meant to write 3.14 % (2 * pi)(in either case, the algorithm is the same. Just subtract/add until the number is as small as possible).
此外,3.14 % 2 * pi被解释为(3.14 % 2) * pi。我不确定您是否打算编写3.14 % (2 * pi)(在任何一种情况下,算法都是相同的。只需减/加,直到数字尽可能小)。
回答by Joran Beasley
same as a normal modulo 3.14 % 6.28 = 3.14, just like 3.14%4 =3.143.14%2 = 1.14(the remainder...)
与普通 modulo 相同 3.14 % 6.28 = 3.14,就像3.14%4 =3.143.14%2 = 1.14(余数...)
回答by Thomas
In addition to the other answers, the fmoddocumentationhas some interesting things to say on the subject:
除了其他答案之外,fmod文档中还有一些关于这个主题的有趣内容:
math.fmod(x, y)Return
fmod(x, y), as defined by the platform C library. Note that the Python expressionx % ymay not return the same result. The intent of the C standard is thatfmod(x, y)be exactly (mathematically; to infinite precision) equal tox - n*yfor some integer n such that the result has the same sign asxand magnitude less thanabs(y). Python'sx % yreturns a result with the sign ofyinstead, and may not be exactly computable for float arguments. For example,fmod(-1e-100, 1e100)is-1e-100, but the result of Python's-1e-100 % 1e100is1e100-1e-100, which cannot be represented exactly as a float, and rounds to the surprising1e100. For this reason, functionfmod()is generally preferred when working with floats, while Python'sx % yis preferred when working with integers.
math.fmod(x, y)返回
fmod(x, y),由平台 C 库定义。请注意,Python 表达式x % y可能不会返回相同的结果。C 标准的意图是对于某个整数 nfmod(x, y)精确地(数学上;无限精度)等于x - n*y,使得结果具有相同的符号x和小于 的幅度abs(y)。Pythonx % y返回的结果带有y相反的符号,并且对于浮点参数可能无法完全计算。例如,fmod(-1e-100, 1e100)is-1e-100,但 Python 的-1e-100 % 1e100is的结果1e100-1e-100不能完全表示为浮点数,而是四舍五入到令人惊讶的1e100。出于这个原因,函数fmod()在使用浮点数时通常是首选,而 Python 的x % y处理整数时首选。
回答by Xorlev
Same thing you'd expect from normal modulo .. e.g. 7 % 4 = 3, 7.3 % 4.0 = 3.3
您对正常模数的期望相同.. 例如7 % 4 = 3,7.3 % 4.0 = 3.3
Beware of floating point accuracy issues.
注意浮点精度问题。
回答by praveen kansara
you should use fmod(a,b)
你应该使用 fmod(a,b)
While abs(x%y) < abs(y) is truemathematically, for floatsit may not be true numerically due to roundoff.
While abs(x%y) < abs(y) is true在数学上,由于 ,floats它在数值上可能不是真的roundoff。
For example, and assuming a platform on which a Python floatis an IEEE 754double-precision number, in order that -1e-100 % 1e100have the same sign as 1e100, the computed result is -1e-100 + 1e100, which is numerically exactly equal to 1e100.
例如,假设一个平台上的 aPython float是一个IEEE 754双精度数,为了与-1e-100 % 1e100具有相同的符号1e100,计算结果是-1e-100 + 1e100,在数值上完全等于1e100。
Function fmod()in the math module returns a result whose sign matches the sign of the first argument instead, and so returns -1e-100in this case. Which approach is more appropriate depends on the application.
fmod()math 模块中的函数返回一个结果,其符号与第一个参数的符号匹配,因此-1e-100在这种情况下返回。哪种方法更合适取决于应用程序。
where x = a%bis used for integer modulo
where x = a%b用于整数模

