Bug in floating point multiplication

Tim Chase python.list at tim.thechases.com
Thu Jul 2 12:59:49 EDT 2015


On 2015-07-03 00:52, Steven D'Aprano wrote:
> x = 1 - 1/2**53
> assert x == 0.9999999999999999
> for i in range(1, 1000000):
>     if int(i*x) == i:
>         print(i); break

tkc at debian:~$ python
Python 2.7.9 (default, Mar  1 2015, 12:57:24) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 1 - 1/2**53
>>> assert x == 0.9999999999999999
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError
>>> for i in range(1, 1000000):
...     if int(i*x) == i:
...         print(i); break
... 
1
>>> x
1
>>> ^D
tkc at debian:~$ python3
Python 3.4.2 (default, Oct  8 2014, 10:45:20) 
[GCC 4.9.1] 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
... 
>>> ^D

tkc at debian:~$ uname -a
Linux bigbox 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt9-3~deb8u1
(2015-04-24) x86_64 GNU/Linux

Weird that on 2.7, the assert fails.

-tkc



More information about the Python-list mailing list