Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.020 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'win32': 0.03; 'directive.': 0.09; 'subject:skip:m 10': 0.09; 'cc:addr:python- list': 0.10; 'python': 0.11; 'behaviour.': 0.16; 'buggy': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'reproduce': 0.16; 'wrote:': 0.16; 'windows': 0.20; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; 'do.': 0.22; '64-bit': 0.22; 'am,': 0.23; 'bit': 0.23; '2015': 0.23; 'feb': 0.23; 'dec': 0.23; 'header:In-Reply-To:1': 0.24; 'testing': 0.25; 'supported': 0.27; 'message-id:@mail.gmail.com': 0.28; 'skip:( 20': 0.28; 'looks': 0.29; '32-bit': 0.29; 'fri,': 0.31; 'print': 0.31; 'run': 0.32; 'anyone': 0.32; 'probably': 0.32; 'builds': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'received:google.com': 0.34; "isn't": 0.35; 'so,': 0.37; 'operating': 0.37; 'subject:: ': 0.37; 'optimization': 0.37; 'version': 0.38; 'say': 0.38; '2010,': 0.39; 'system.': 0.39; 'your': 0.60; 'future': 0.61; 'mar': 0.65; 'reply': 0.67; 'jul': 0.72; 'chrisa': 0.84; 'to:none': 0.90; '2013,': 0.91; '2014,': 0.91; 'instantly': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=AUWjZwqsGuipWgs1dmgT/eUTjpnl4qDDHi1V8nTxHOY=; b=RecgCE218QwcendZotSAQJ6xHD1ZgAEQErizP2DxyA/1/G+iLoJ56FjxphuOppy8fF hy0mYHnFcJNxhc1SIAVqPwfpcqkMvluEGa36PJFlb+UgoLbWlglu91ZqGFCJXdR5qdvF pbco9HLIQ4pdFoOahUH8jGXevzGVzrX3z/hND8YXj3moELc4jjHPKmwbqXPHOqD+g5DQ EPHZpLJ/A72hOKRST97undXVEsM+UynEPn0kRY2fn4fnaLeDVsUgXrskhQOLZv2RXd6e Fac9qqAOwyYmXbT468NlmWyYnTEzC/nlSAUIXQ3gc3fXleAgkT9IMna1LDFJESMF4cWa icrw== MIME-Version: 1.0 X-Received: by 10.107.10.83 with SMTP id u80mr3134209ioi.53.1435850470636; Thu, 02 Jul 2015 08:21:10 -0700 (PDT) In-Reply-To: <55955048$0$1662$c3e8da3$5496439d@news.astraweb.com> References: <55955048$0$1662$c3e8da3$5496439d@news.astraweb.com> Date: Fri, 3 Jul 2015 01:21:10 +1000 Subject: Re: Bug in floating point multiplication From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 45 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1435850477 news.xs4all.nl 2896 [2001:888:2000:d::a6]:40434 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:93429 On Fri, Jul 3, 2015 at 12:52 AM, Steven D'Aprano 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