繁体   English   中英

python中整数除法的潜在含义是什么?

[英]What's the underlying implimentation of integer division in python?

我正在运行 python 3.7.3

关于整数除法运算符:“//”(双除法、双正斜杠、双除法运算符?我不确定确切的名称。)

它似乎没有给出一致的结果,而且我发现的许多解释并没有完全解释它的结果。

这里[ 在Python中有'//'的原因是什么? (和其他地方)据说“//”运算符给出没有余数的商。 好像a // bfloor(a / b) (如果a / b为负,则向上舍入)。

然而,有时它并没有给出那个答案。 例如, 1 // 0.2计算结果为 4。但是1 / 0.2返回 5,并且math.floor(1 / 2)也返回 5。它给出的数字比整数除法应该小一。 如果将 10 除以 2,则//运算符返回 5,但 1 除以 0.2 无法正常工作。

这个问题出现在我使用//运算符来划分浮点数的其他时候。 5 // 0.2100 // 0.2 我不知道这是否是浮点运算的一些怪癖,但是如果您输入math.floor(5 / 0.2) (或任何其他给出问题的数字集math.floor(5 / 0.2) ,这些问题似乎就会消失。 除非你除以负数,在这种情况下你必须使用math.ceil()而不是math.floor()

我目前的解决方案是这样的:

import math
def integerDivision(a,b):
    tmp = a / b 
    if tmp > 1:
        return(math.floor(tmp))
    else:
        return(math.ceil(tmp))

//运算符的实现是什么使它在某些浮点情况下不能给出正确的结果? 除了上面的代码之外,还有没有更好的方法来解决//运算符的这个问题?

这与实施无关。 这与操作符的语义有关。 无论实现如何, //运算符都需要为您提供应用于浮点数时看到的结果,并且这些结果确实是正确的(对于浮点数)。 如果你不想要这些结果,浮动可能是你正在做的事情的错误工具。

1 // 0.2给出表示其参数商的确切值的下限的浮点数。 但是,右侧参数并不完全具有您键入的值。右侧参数的值是 64 位 IEEE 二进制浮点中可表示的最接近 0.2 的值,该值略高于 0.2:

>>> import decimal
>>> decimal.Decimal(0.2)
Decimal('0.200000000000000011102230246251565404236316680908203125')

因此,商的确切值略小于 5,因此1 // 0.2为您提供4.0

1 / 0.2为您提供5.0因为商的确切值不能表示为浮点数。 结果需要四舍五入,并四舍五入为5.0 //不执行此舍入; 它计算精确值的下限,而不是圆形浮点数的下限。 //的结果可能需要四舍五入,但这是不同的四舍五入。)

尽管如此,实现需要比floor(x / y)更复杂,因为这会产生错误的结果。 CPython 的//浮点数实现基于fmod 您可以在 CPython 源代码库中的Objects/floatobject.c中查看实现。

static PyObject *
float_divmod(PyObject *v, PyObject *w)
{
    double vx, wx;
    double div, mod, floordiv;
    CONVERT_TO_DOUBLE(v, vx);
    CONVERT_TO_DOUBLE(w, wx);
    if (wx == 0.0) {
        PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
        return NULL;
    }
    PyFPE_START_PROTECT("divmod", return 0)
    mod = fmod(vx, wx);
    /* fmod is typically exact, so vx-mod is *mathematically* an
       exact multiple of wx.  But this is fp arithmetic, and fp
       vx - mod is an approximation; the result is that div may
       not be an exact integral value after the division, although
       it will always be very close to one.
    */
    div = (vx - mod) / wx;
    if (mod) {
        /* ensure the remainder has the same sign as the denominator */
        if ((wx < 0) != (mod < 0)) {
            mod += wx;
            div -= 1.0;
        }
    }
    else {
        /* the remainder is zero, and in the presence of signed zeroes
           fmod returns different results across platforms; ensure
           it has the same sign as the denominator. */
        mod = copysign(0.0, wx);
    }
    /* snap quotient to nearest integral value */
    if (div) {
        floordiv = floor(div);
        if (div - floordiv > 0.5)
            floordiv += 1.0;
    }
    else {
        /* div is zero - get the same sign as the true quotient */
        floordiv = copysign(0.0, vx / wx); /* zero w/ sign of vx/wx */
    }
    PyFPE_END_PROTECT(floordiv)
    return Py_BuildValue("(dd)", floordiv, mod);
}

static PyObject *
float_floor_div(PyObject *v, PyObject *w)
{
    PyObject *t, *r;

    t = float_divmod(v, w);
    if (t == NULL || t == Py_NotImplemented)
        return t;
    assert(PyTuple_CheckExact(t));
    r = PyTuple_GET_ITEM(t, 0);
    Py_INCREF(r);
    Py_DECREF(t);
    return r;
}

其他参数类型将使用其他实现,具体取决于类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM