[issue5473] round(float, ndigits<0) sometimes rounds to odd

Christian Taylor report at bugs.python.org
Wed Mar 11 00:18:54 CET 2009


New submission from Christian Taylor <dingo at chello.at>:

round(x, n) may unexpectedly round floats upwards to odd multiples of
10**(-n) if n is negative, depending on the system Python 3 is running
on. I think this is distinct from issue 1869.

Example:
>>> round(25.0, -1)
30.0

I used the following function to check 1000 cases for a given exponent
and yield the values where rounding to odd occurs:

def check(exponent):
    factor = 10**exponent
    for x in range(5, 5+20000, 20):
        if not round(float(x*factor), -1-exponent) < x*factor:
            yield float(x*factor)

On a Core2 Duo running Arch Linux (32bit):
Python 3.1a1+ (py3k:70302, Mar 10 2009, 21:43:09)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> [len(list(check(exponent))) for exponent in range(10)]
[1000, 1000, 1000, 1000, 1000, 0, 0, 1000, 1000, 1000]

On an Athlon XP running Slackware (32bit):
Python 3.1a1+ (py3k:70302, Mar 11 2009, 01:01:18)
[GCC 4.1.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> [len(list(check(exponent))) for exponent in range(10)]
[1000, 1000, 1000, 1000, 1000, 0, 0, 1000, 1000, 1000]

On an Athlon 64 running Debian (32bit):
Python 3.1a1+ (py3k:70302, Mar 10 2009, 22:45:59)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> [len(list(check(exponent))) for exponent in range(10)]
[0, 0, 0, 0, 630, 0, 0, 0, 195, 0]
>>> next(check(4))
650000.0
>>> next(check(8))
14500000000.0

----------
components: Interpreter Core
messages: 83450
nosy: dingo
severity: normal
status: open
title: round(float, ndigits<0) sometimes rounds to odd
type: behavior
versions: Python 3.0, Python 3.1

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue5473>
_______________________________________


More information about the Python-bugs-list mailing list