来自What’s New In Python 3.7
我们可以看到新的^{
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 quotientx / y
. Ifx / y
is exactly halfway between two consecutive integers, the nearest even integer is used forn
. The remainderr = remainder(x, y)
thus always satisfiesabs(r) <= 0.5 * abs(y)
.Special cases follow IEEE 754: in particular,
remainder(x, math.inf)
is x for any finite x, andremainder(x, 0)
andremainder(math.inf, x)
raiseValueError
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.
但是我们还记得有一个^{
remainder of
x / y
我们还可以看到,操作员注意:
Not for complex numbers. Instead convert to floats using
abs()
if appropriate.
如果可能的话,我还没有尝试运行Python3.7。在
但我试过了
Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> 100 % math.inf
100.0
>>> math.inf % 100
nan
>>> 100 % 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
所以区别在于,我们将得到nan
和{
所以问题是%
和{math.remainder
是否也可以处理复数(%
缺少它)?主要优势是什么?在
这是来自官方cpythongithub回购的source of ^{
对于模,这是}。在
m = x - n*y
,其中n
是floor(x/y)
,所以剩下的0 <= m < y
而不是{所以
多亏了@MaartenFabré,我没有注意到细节:
我构建了Python 3.7:
不同之处在于:
零作为除数:
^{pr2}$基本数,其中
math.remainder(x, y) < x % y
复数:
无穷大(
math.inf
)相关问题 更多 >
编程相关推荐