Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'that?': 0.05; 'pypy': 0.07; '__future__': 0.09; 'creighton': 0.09; 'implies': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'reports.': 0.09; 'subject:skip:m 10': 0.09; 'python': 0.11; '2.7': 0.13; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'reproduce': 0.16; 'wrote:': 0.16; 'laura': 0.18; 'runs': 0.18; '"",': 0.22; 'gcc': 0.22; 'title,': 0.22; '2015': 0.23; 'forgot': 0.23; 'import': 0.24; '(most': 0.24; 'url:bugs': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'division': 0.29; 'prints': 0.29; 'environment': 0.29; 'fri,': 0.31; 'mention': 0.31; 'error.': 0.31; "can't": 0.32; 'anyone': 0.32; 'probably': 0.32; 'url:python': 0.33; 'traceback': 0.33; '(for': 0.34; 'file': 0.34; 'to:addr:python-list': 0.35; 'reply.': 0.35; 'but': 0.36; 'url:org': 0.36; 'there': 0.36; 'so,': 0.37; 'should': 0.37; 'operating': 0.37; 'subject:: ': 0.37; 'version': 0.38; 'received:org': 0.38; 'to:addr:python.org': 0.39; 'system.': 0.39; 'received:de': 0.40; 'your': 0.60; 'necessarily': 0.61; 'more': 0.62; 'mar': 0.65; '>>>>>': 0.66; 'reply': 0.67; 'jul': 0.72; '+1000,': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Bug in floating point multiplication Date: Sun, 05 Jul 2015 09:38:24 +0200 Organization: None References: <55955048$0$1662$c3e8da3$5496439d@news.astraweb.com> <201507042254.t64Ms3qW031538@theraft.openend.se> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd9947.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 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: 67 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1436081928 news.xs4all.nl 2822 [2001:888:2000:d::a6]:47389 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!news.stben.net!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:93504 Laura Creighton wrote: > In a message of Fri, 03 Jul 2015 00:52:55 +1000, "Steven D'Aprano" writes: >>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. >> >> >> >>-- >>Steven > > PyPy says: > Python 2.7.9 (2.5.1+dfsg-1, Mar 27 2015, 19:45:43) > [PyPy 2.5.1 with 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 "", line 1, in > AssertionError >>>>> for i in range(1, 1000000): > .... if int(i*x) == i: > .... print(i); break > .... > .... > 1 >>>>> > > So the loop terminates, but there is an Assertion Error. Did you want > that? No. Steven's environment implies from __future__ import division which he forgot to mention in the original post: >>>> x = 1-1/2**53 >>>> x 1 >>>> from __future__ import division >>>> x = 1-1/2**53 >>>> x 0.9999999999999999