Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'binary': 0.07; 'float': 0.07; 'subject:bug': 0.07; '0.02': 0.09; '0.1': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'stored': 0.12; '2.7': 0.14; '0.01': 0.16; '0.03': 0.16; '0.04': 0.16; '0.06': 0.16; '0.07': 0.16; '0.09': 0.16; '0.19': 0.16; 'b.py': 0.16; 'clues': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'repr()': 0.16; 'rounding.': 0.16; 'str()': 0.16; 'url:whatsnew': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'print': 0.22; 'header:User-Agent:1': 0.23; '(or': 0.24; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'correct': 0.29; 'chris': 0.29; 'url:bugs': 0.29; "doesn't": 0.30; 'characters': 0.30; 'code': 0.31; 'decimal': 0.31; 'python2.7': 0.31; 'run': 0.32; 'url:python': 0.33; 'actual': 0.34; 'objects': 0.35; 'but': 0.35; 'charset:us-ascii': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'two': 0.37; 'minimum': 0.38; 'sometimes': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'changed': 0.39; 'received:org': 0.40; 'how': 0.40; "you're": 0.61; 'here': 0.66; 'received:204': 0.75; 'article': 0.77; '2.7.': 0.84; 'actually,': 0.84; 'received:204.14': 0.84; 'info,': 0.91; 'reasons,': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ned Deily Subject: Re: python 3.44 float addition bug? Date: Sat, 21 Jun 2014 12:24:24 -0700 References: Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 204.14.154.233 User-Agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 100 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1403378678 news.xs4all.nl 2962 [2001:888:2000:d::a6]:47532 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:73482 In article , Chris Angelico wrote: > Also, when you're looking at how things print out, consider looking at > two things: the str() and the repr(). Sometimes just "print(p)" > doesn't give you all the info, so you might instead want to write your > loop thus: > > z = 0.01 > p = 0.0 > for i in range(19): > p += z > print(str(p) + " -- " + repr(p)) > Sometimes you can get extra clues that way, although in this instance > I think you won't. Actually, I think this is one case where you would get extra clues (or extra headscratching) if you run the code with various releases of Python. $ python2.6 b.py 0.01 -- 0.01 0.02 -- 0.02 0.03 -- 0.029999999999999999 0.04 -- 0.040000000000000001 0.05 -- 0.050000000000000003 0.06 -- 0.060000000000000005 0.07 -- 0.070000000000000007 0.08 -- 0.080000000000000002 0.09 -- 0.089999999999999997 0.1 -- 0.099999999999999992 0.11 -- 0.10999999999999999 0.12 -- 0.11999999999999998 0.13 -- 0.12999999999999998 0.14 -- 0.13999999999999999 0.15 -- 0.14999999999999999 0.16 -- 0.16 0.17 -- 0.17000000000000001 0.18 -- 0.18000000000000002 0.19 -- 0.19000000000000003 $ python2.7 b.py 0.01 -- 0.01 0.02 -- 0.02 0.03 -- 0.03 0.04 -- 0.04 0.05 -- 0.05 0.06 -- 0.060000000000000005 0.07 -- 0.07 0.08 -- 0.08 0.09 -- 0.09 0.1 -- 0.09999999999999999 0.11 -- 0.10999999999999999 0.12 -- 0.11999999999999998 0.13 -- 0.12999999999999998 0.14 -- 0.13999999999999999 0.15 -- 0.15 0.16 -- 0.16 0.17 -- 0.17 0.18 -- 0.18000000000000002 0.19 -- 0.19000000000000003 $ python3.4 b.py 0.01 -- 0.01 0.02 -- 0.02 0.03 -- 0.03 0.04 -- 0.04 0.05 -- 0.05 0.060000000000000005 -- 0.060000000000000005 0.07 -- 0.07 0.08 -- 0.08 0.09 -- 0.09 0.09999999999999999 -- 0.09999999999999999 0.10999999999999999 -- 0.10999999999999999 0.11999999999999998 -- 0.11999999999999998 0.12999999999999998 -- 0.12999999999999998 0.13999999999999999 -- 0.13999999999999999 0.15 -- 0.15 0.16 -- 0.16 0.17 -- 0.17 0.18000000000000002 -- 0.18000000000000002 0.19000000000000003 -- 0.19000000000000003 What's going on here is that in Python 2.7 the repr() of floats was changed to use the minimum number of digits to accurately roundtrip the number under correct rounding. For compatibility reasons, the str() representation was not changed for 2.7. But in Python 3.2, str() was changed to be identical to repr() for floats. It's important to keep in mind that the actual binary values stored in float objects are the same across all of these releases; only the representation of them as decimal characters varies. https://docs.python.org/2.7/whatsnew/2.7.html#other-language-changes http://bugs.python.org/issue9337 -- Ned Deily, nad@acm.org