a Python bug report

Ian Kelly ian.g.kelly at gmail.com
Tue May 29 22:11:06 EDT 2018


On Tue, May 29, 2018 at 7:07 PM, Ruifeng Guo <Ruifeng.Guo at synopsys.com> wrote:
> Hello,
> We encountered a bug in Python recently, we checked the behavior for Python version 2.7.12, and 3.1.1, both version show the same behavior. Please see below the unexpected behavior in "red text".
>
> Thanks,
> Ruifeng Guo
>
> From: Brian Archer
> Sent: Tuesday, May 29, 2018 5:57 PM
> To: Ruifeng Guo <rfguo at synopsys.com>
> Subject: Python Bug
>
> Python 3.1.1 (r311:74480, Nov 20 2012, 09:11:57)
> [GCC 4.2.2] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> a=1017.0
>>>> print(int(a))
> 1017
>>>> b=1000*1.017
>>>> print(b)
> 1017.0
>>>> int(b)
> 1016
>>>> c=1017.0
>>>> int(c)
> 1017

Try this, and you'll see what the problem is:

>>> repr(b)
'1016.9999999999999'

The value of b is not really 1017, but fractionally less as a result
of floating point rounding error, because 1.017 cannot be exactly
represented as a float.

In Python 3.2, the str() of the float type was changed to match the
repr(), so that when you use print() as above you will also get this
result:

>>> print(b)
1016.9999999999999

By the way, Python 3.1.1 is really old (six years!). I recommend
upgrading if possible.



More information about the Python-list mailing list