Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'cpython': 0.05; 'subject:Python': 0.06; 'strict': 0.07; 'overflow': 0.09; 'subject:language': 0.09; 'python': 0.11; '3.3,': 0.16; 'both,': 0.16; 'correctness': 0.16; 'entirely.': 0.16; 'exponent': 0.16; 'finite,': 0.16; 'formula': 0.16; 'inconvenient': 0.16; 'inf': 0.16; 'subject: \n ': 0.16; 'applies': 0.16; 'wrote:': 0.18; 'version.': 0.19; '(the': 0.22; '>>>': 0.22; 'import': 0.22; 'integer': 0.24; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'point': 0.28; 'am,': 0.29; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; 'calculated': 0.31; "d'aprano": 0.31; 'decimal': 0.31; 'steven': 0.31; 'regular': 0.32; 'fri,': 0.33; 'skip:d 20': 0.34; 'could': 0.34; "can't": 0.35; 'beyond': 0.35; 'one,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'version': 0.36; 'accuracy': 0.36; 'event,': 0.36; 'version,': 0.38; 'to:addr :python-list': 0.38; 'to:addr:python.org': 0.39; 'ian': 0.60; 'temporarily': 0.60; "you'll": 0.62; 'within': 0.65; 'due': 0.66; 'here': 0.66; 'default': 0.69; 'gotten': 0.74; 'subject:this': 0.83; 'actually,': 0.84; 'loses': 0.84; 'system:': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=vem1YXEzQmwh+eGkxWfvNVFHUCRVWDgtuYLfj7gM8Rk=; b=Cv8SoMG7zfFqcvOPVnEZxUCPq+0vXaGIpJ92Zdr0qFz0ctepQoy6M2TkT9+mY58U+U C0QYgZ/uidpke4FfErFDjOQOejkcyZ6MHbLID80tUJ2w1Igk80HrQ027eia6HmKrmQiD b8NWHX6JLEh3x1uSfJOehEK779xqCsyZ+V7YNhzhOvOpQU7DNhpkaRwH4GhxYD3ygZTM 4wrDQCVVWXQdYOseJpKDPEfYLthB+5QrTLDzRWqEM8Mkj0xEmZAV7E7M06QKxyjdvrY2 jQMQM9R5Bq7eytzWjemWlSYuRuMzstMpzzXcUhQH3fNtdz/9YYNN/phBtahu7s6r01o5 DPyQ== X-Received: by 10.68.235.6 with SMTP id ui6mr16011793pbc.45.1396630949364; Fri, 04 Apr 2014 10:02:29 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <533e84ad$0$29993$c3e8da3$5496439d@news.astraweb.com> References: <9daf0806-02de-4447-964c-c8f8953c23e5@googlegroups.com> <5334c38e$0$29994$c3e8da3$5496439d@news.astraweb.com> <53364327$0$29994$c3e8da3$5496439d@news.astraweb.com> <53365F55.2040302@gmail.com> <533836c4$0$29994$c3e8da3$5496439d@news.astraweb.com> <533e64ce$0$29993$c3e8da3$5496439d@news.astraweb.com> <533e84ad$0$29993$c3e8da3$5496439d@news.astraweb.com> From: Ian Kelly Date: Fri, 4 Apr 2014 11:01:48 -0600 Subject: Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list) To: Python Content-Type: text/plain; charset=ISO-8859-1 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: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1396630958 news.xs4all.nl 2905 [2001:888:2000:d::a6]:55292 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69674 On Fri, Apr 4, 2014 at 4:08 AM, Steven D'Aprano wrote: > On Fri, 04 Apr 2014 02:13:13 -0600, Ian Kelly wrote: > >> On Fri, Apr 4, 2014 at 1:52 AM, Steven D'Aprano >> wrote: >>> py> from decimal import * >>> py> getcontext().prec = 16 >>> py> x = Decimal("0.7777777777787516") py> y = >>> Decimal("0.7777777777787518") py> (x + y) / 2 >>> Decimal('0.7777777777787515') >>> >>> "Guido, why can't Python do maths???" >> >> Well, you need to work within the system: >> >>>>> (5*x + 5*y) / 10 >> Decimal('0.7777777777787517') >> >> Actually, I have no idea whether that formula can be relied upon or the >> correctness of the above was just luck. > > > And what happens when x+y would have been calculated correctly, but one, > or both, of 5*x or 5*y loses catastrophically loses accuracy due to > overflow? > > py> x = 3.1e307 > py> y = 3.3e307 > py> (x+y)/2 > 3.2e+307 > py> (5*x+5*y)/10 > inf > > (I've used regular floats here out of laziness, the same principle > applies to Decimals -- there will be *some* number x which is finite, but > 5*x overflows to infinity.) I thought that Decimals had arbitrary-precision exponents, at least in the pure Python version. Turns out that's wrong; although the context.Emax can be set to any int in the pure Python version, it can't be removed entirely. One could just temporarily upgrade the Emax for the above calculation, but the pure Python version was made inconvenient to use voluntarily in CPython 3.3, and the C version has strict limits. In any event, the exponent limits for decimals are much higher than for floats (the default Emax is 999999, and it can be set roughly within the limits of C integer precision), so any case where you'll get overflow with a Decimal is already far beyond the point where you'd have gotten overflow with a float.