Bug in floating point multiplication

Chris Angelico rosuav at gmail.com
Thu Jul 2 11:21:10 EDT 2015


On Fri, Jul 3, 2015 at 12:52 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> 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
>

I suspect for Py2 you need to say 1/2.0, or insist on a future
directive. Otherwise the assertion fails, x is simply 1, and the loop
instantly halts, none of which is buggy behaviour.

Testing on a 64-bit Debian Jessie, these all run to completion:
Python 3.6.0a0 (default:5c901b39c6b7, Jun 28 2015, 09:13:39)
Python 3.5.0b1+ (default:7255af1a1c50+, May 26 2015, 00:39:06)
Python 3.4.2 (default, Oct  8 2014, 10:45:20)
Python 2.7.9 (default, Mar  1 2015, 12:57:24)

Testing on a 32-bit Debian Jessie, these all print 2049 and stop:
Python 3.4.2 (default, Oct  8 2014, 13:14:40)
Python 3.3.3 (default, Dec 31 2013, 19:11:08)
Python 2.7.9 (default, Mar  1 2015, 18:22:53)
Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40)

(2.6.6 isn't actually supported on Jessie, it just happens to be laying around.)

On 32-bit AntiX, these both print 2049 and stop:
Python 3.4.3+ (default, Jun  2 2015, 14:09:35)
Python 2.7.10 (default, Jun  1 2015, 16:21:46)

Looks like it's an issue with 32-bit builds only. And not on Windows -
I spun up a Win 7 VM and tested it to completion:

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600
32 bit (Intel)] on win32
Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit
(Intel)] on win32

Over-eager optimization somewhere?

ChrisA



More information about the Python-list mailing list