Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!news2.euro.net!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.133 X-Spam-Level: * X-Spam-Evidence: '*H*': 0.74; '*S*': 0.01; 'float': 0.07; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'zero,': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'header:In-Reply- To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; '"",': 0.31; "d'aprano": 0.31; 'division': 0.31; 'steven': 0.31; 'file': 0.32; "we're": 0.32; '(most': 0.33; 'skip:s 30': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'false': 0.36; 'done': 0.36; 'should': 0.36; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'little': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'even': 0.60; 'therefore': 0.72; 'further,': 0.74; 'numbers:': 0.91; '2013': 0.98 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:to :content-type; bh=u3WfCrfeYPag4PYSvGF0PjuUOJvtLQCciPHR3vwSBQw=; b=T36SkcxiDqPrhBz6juVpZz85YHBQv3eAurZNVF+R21qB6zhwwBLWkeXSboD0vN0SLM D/BXIbLrhTt5nyIo1zIT+QM2jyBV14JGuSw8EcNmD1wPNG1a3ntH7YcswXx6wflrYuoA cTErVljXmcSQO2ItW166j/RFYpiZtUVqYd3lBvxxPGnCufdK0A/x/QR7jknalEQOBzy6 VMyvhYyRnsajkp9rqiUh60T40RgLSkJ7XJZBCiFBYBpQCmNLxsdyWX0ZX5hTGNOlMV2a +KOAAFrVOMLpjMyIKb0GgICcOwE2pv6rCN2q7IudAkmcQilwux+WwMjY8FxuhJTmANog Lfjg== MIME-Version: 1.0 X-Received: by 10.52.155.67 with SMTP id vu3mr8824752vdb.94.1369749661298; Tue, 28 May 2013 07:01:01 -0700 (PDT) In-Reply-To: <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> Date: Wed, 29 May 2013 00:01:01 +1000 Subject: Re: Short-circuit Logic From: Chris Angelico To: python-list@python.org 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: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1369749670 news.xs4all.nl 15990 [2001:888:2000:d::a6]:54690 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:46290 On Tue, May 28, 2013 at 11:48 PM, Steven D'Aprano wrote: > py> y = 1e17 + x # x is not zero, so y should be > 1e17 > py> 1/(1e17 - y) > Traceback (most recent call last): > File "", line 1, in > ZeroDivisionError: float division by zero You don't even need to go for 1e17. By definition: >>> sys.float_info.epsilon+1.0==1.0 False >>> sys.float_info.epsilon+2.0==2.0 True Therefore the same can be done with 2 as you did with 1e17. >>> y = 2 + sys.float_info.epsilon >>> 1/(2-y) Traceback (most recent call last): File "", line 1, in 1/(2-y) ZeroDivisionError: float division by zero Of course, since we're working with a number greater than epsilon, we need to go a little further, but we can still work with small numbers: >>> x = sys.float_info.epsilon * 2 # Definitely greater than epsilon >>> y = 4 + x >>> 1/(4-y) Traceback (most recent call last): File "", line 1, in 1/(4-y) ZeroDivisionError: float division by zero ChrisA