Bug in floating point multiplication

Vincent Vande Vyvre vincent.vande.vyvre at telenet.be
Thu Jul 2 11:15:04 EDT 2015


Le 02/07/2015 16:52, Steven D'Aprano a écrit :
> Despite the title, this is not one of the usual "Why can't Python do
> maths?" "bug" reports.
>
> Can anyone reproduce this behaviour? If so, please reply with the version of
> Python and your operating system. Printing sys.version will probably do.
>
>
> x = 1 - 1/2**53
> assert x == 0.9999999999999999
> for i in range(1, 1000000):
>      if int(i*x) == i:
>          print(i); break
>
>
> Using Jython and IronPython, the loop runs to completion. That is the
> correct behaviour, or so I am lead to believe. Using Python 2.6, 2.7 and
> 3.3 on Centos and Debian, it prints 2049 and breaks. That should not
> happen. If you can reproduce that (for any value of i, not necessarily
> 2049), please reply.
>
> See also http://bugs.python.org/issue24546 for more details.
>
>
>

Hi,

vincent at tiemoko:~$ python3
Python 3.2.3 (default, Jun 18 2015, 21:46:42)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> x = 1 - 1/2**53
 >>> assert x == 0.9999999999999999
 >>> for i in range(1, 1000000):
...     if int(i*x) == i:
...             print(i)
...             break
...
2049
 >>>

-------------------------------------------------------------------------------
vincent at djoliba:~$ python3
Python 3.4.0 (default, Jun 19 2015, 14:20:21)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
 >>> x = 1 - 1/2**53
 >>> assert x == 0.9999999999999999
 >>> for i in range(1, 1000000):
...     if int(i*x) == i:
...         print(i)
...         break
...
 >>>

Both on Ubuntu.

Vincent



More information about the Python-list mailing list