python2中的python3舍入行为

2024-06-13 01:42:48 发布

您现在位置:Python中文网/ 问答频道 /正文

在Python 2.x中,内置的^{}具有以下行为:

if two multiples are equally close, rounding is done away from 0 (so. for example, round(0.5) is 1.0 and round(-0.5) is -1.0)

在python3.x中,this has changed到更常见的:

if two multiples are equally close, rounding is done toward the even choice (so, for example, both round(0.5) and round(-0.5) are 0, and round(1.5) is 2).

在Python2.x中有没有一种简单的方法来实现这种行为?不幸的是,^{}模块没有包含此内容。也许还有一个类似的模块我还没找到?或者,另一种将python3.x函数引入python2.x的方法?在

显然,我可以编写一个新函数来产生所需的行为,但我更好奇的是,是否存在使用实际python3.x函数的解决方案,以避免增加不必要的复杂性和维护代码。在


Tags: and函数forcloseifsoisare
3条回答

这个回答已经阅读了最初的问题,答案是“不,我不能想出一些使用Py3原始代码的东西。”

但是对于想知道在Py2中复制Py3行为的代码是什么的人来说(包括int与float行为),下面是上面的User代码的改编,其中包括ndigits和{}的int与float的区别。在

import sys

def py3round(number, ndigits=None):
    '''
    Simulates Python3 rounding behavior in Python 2

    >>> py3round(2.3)
    2
    >>> py3round(2.7)
    3
    >>> py3round(2.7, 0)
    3.0
    >>> py3round(1.5, 0)
    2.0
    >>> py3round(2.5, 0)
    2.0
    >>> py3round(-1.5)
    -2
    >>> py3round(-2.5)
    -2
    '''
    if sys.version_info[0] >= 3:
        if ndigits is not None:
            return round(number, ndigits)
        else:
            return round(number)        

    intIt = True if ndigits is None else False
    ndigits = ndigits if ndigits is not None else 0

    f = number
    if abs(round(f) - f) == 0.5:
        retAmount = 2.0 * round(f / 2.0, ndigits);
    else:
        retAmount = round(f, ndigits)

    if intIt:
        return int(retAmount)
    else:
        return retAmount

除非您介意numpy依赖,^{}可能会这样做:

>>> from numpy import around
>>> around(0.5)
0
>>> around(-0.5)
-0
>>> around(1.5)
2.0

Python 2中的Python 3回合

函数可以如下所示:

def py3round(f):
    if abs(round(f)-f) == 0.5:
        return 2.0*round(f/2.0);
    return round(f)

# Python 3            apply round to ... -.1 -.75 -.5 -.25 0 .25 .5 .75 ...
>>> ' '.join(map(str, map(int, [round(i * 0.25) for i in range(-20, 20)])))
'-5 -5 -4 -4 -4 -4 -4 -3 -3 -3 -2 -2 -2 -2 -2 -1 -1 -1 0 0 0 0 0 1 1 1 2 2 2 2 2 3 3 3 4 4 4 4 4 5'
# Python 2            apply round to ... -.1 -.75 -.5 -.25 0 .25 .5 .75 ...
>>> ' '.join(map(str, map(int, [py3round(i * 0.25) for i in range(-20, 20)])))
'-5 -5 -4 -4 -4 -4 -4 -3 -3 -3 -2 -2 -2 -2 -2 -1 -1 -1 0 0 0 0 0 1 1 1 2 2 2 2 2 3 3 3 4 4 4 4 4 5'

让我澄清一下bltinmodule.c中的圆是什么

^{pr2}$

所以圆实际上几乎什么也做不了。它取决于传递给它的对象。 这将导致floatobject.c函数static PyObject *double_round(double x, int ndigits)

z = round(y);
if (fabs(y-z) == 0.5)
    /* halfway between two integers; use round-half-even */
    z = 2.0*round(y/2.0);

我在上面的函数中使用了这些行的知识。在

Python 3中的Python 2 round

我想你需要写一个新函数。在

def python2round(f):
    if round(f + 1) - round(f) != 1:
        return f + abs(f) / f * 0.5
    return round(f)

if语句处理i + 0.5i + 1.5被舍入到不同方向=偶数和减半的情况。在这种情况下,舍入是从零开始的。在

# in Python 2          apply round to ... -.1 -.75 -.5 -.25 0 .25 .5 .75 ...
>>> ' '.join(map(str, map(int, [round(i * 0.25) for i in range(-20, 20)])))
'-5 -5 -5 -4 -4 -4 -4 -3 -3 -3 -3 -2 -2 -2 -2 -1 -1 -1 -1 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5'
# in Python 3          apply round to ... -.1 -.75 -.5 -.25 0 .25 .5 .75 ...
>>> ' '.join(map(str, map(int, [python2round(i * 0.25) for i in range(-20, 20)])))
'-5 -5 -5 -4 -4 -4 -4 -3 -3 -3 -3 -2 -2 -2 -2 -1 -1 -1 -1 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5'

您是否需要第二个参数取整ndigits的解决方案?在

相关问题 更多 >