Bug in floating point multiplication

MRAB python at mrabarnett.plus.com
Thu Jul 2 12:20:27 EDT 2015


On 2015-07-02 15:52, Steven D'Aprano wrote:
> 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.
>
These are all on Windows 8.1 with CPython from www.python.org.

With "x = 1 - 1.0/2**53" (needed for Python 2.x) they all passed.

2.5.4 (r254:67916, Dec 23 2008, 15:19:34) [MSC v.1400 64 bit (AMD64)]
2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)]
2.6.6 (r266:84297, Aug 24 2010, 18:13:38) [MSC v.1500 64 bit (AMD64)]
2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)]
2.7.7 (default, Jun  1 2014, 14:21:57) [MSC v.1500 64 bit (AMD64)]
2.7.7 (default, Jun  1 2014, 14:17:13) [MSC v.1500 32 bit (Intel)]
3.1.4 (default, Jun 12 2011, 14:16:16) [MSC v.1500 64 bit (AMD64)]
3.1.4 (default, Jun 12 2011, 15:05:44) [MSC v.1500 32 bit (Intel)]
3.2.5 (default, May 15 2013, 23:07:10) [MSC v.1500 64 bit (AMD64)]
3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)]
3.3.5 (v3.3.5:62cf4e77f785, Mar  9 2014, 10:35:05) [MSC v.1600 64 bit 
(AMD64)]
3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit 
(Intel)]
3.4.2 (v3.4.2:ab2c023a9432, Oct  6 2014, 22:16:31) [MSC v.1600 64 bit 
(AMD64)]
3.4.2 (v3.4.2:ab2c023a9432, Oct  6 2014, 22:15:05) [MSC v.1600 32 bit 
(Intel)]
3.5.0b2 (v3.5.0b2:7a088af5615b, May 31 2015, 06:22:19) [MSC v.1900 64 
bit (AMD64)]
3.5.0b2 (v3.5.0b2:7a088af5615b, May 31 2015, 06:08:44) [MSC v.1900 32 
bit (Intel)]




More information about the Python-list mailing list