Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.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.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'root': 0.05; 'say,': 0.05; 'calculating': 0.09; 'subject:set': 0.09; 'cc:addr:python-list': 0.11; 'accuracy,': 0.16; 'algorithm.': 0.16; 'benjamin': 0.16; 'cc:name:python list': 0.16; "chris'": 0.16; 'comparison.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'loops': 0.16; 'susceptible': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'tests': 0.22; 'cc:addr:python.org': 0.22; 'error': 0.23; 'case.': 0.24; 'cc:2**0': 0.24; 'performing': 0.26; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'absolute': 0.30; 'compared': 0.30; 'relative': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; '"do': 0.31; 'reduced': 0.31; 'subject:numbers': 0.31; "we're": 0.32; 'guess': 0.33; 'subject:the': 0.34; 'could': 0.34; 'subject:with': 0.35; 'basic': 0.35; 'definition': 0.35; 'test': 0.35; 'received:google.com': 0.35; 'useful': 0.36; 'error.': 0.37; 'two': 0.37; 'e.g.': 0.38; 'mine': 0.38; 'pm,': 0.38; 'that,': 0.38; 'even': 0.60; 'numbers': 0.61; 'more': 0.64; 'effectively': 0.66; 'between': 0.67; 'mar': 0.68; '100': 0.79; 'oscar': 0.84; 'trick.': 0.84; 'to:none': 0.92 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=92GCu3OL9n34cJijeKfYNCu0k5v8NgaJ+AcbC7aeZ2c=; b=VAV7QeRBmf0V4tvR4y1BpEDRiPwq86PfhQw9M2UUkSpOEvngLzJioqLpsLBNpEsUFI TXFeBMOgkICQkyefaPTAmGlqYE+wcxGIFeefsxXw56i7CJ6I9QN+M4n2byG5FOkV2mIC iMIyXJRGbjTkZSHqvdoz5m1FE8BHblQA1bv5Mi8JTvoOyXQmzIFCfAasAsLqhOFgiQmI +Am6X9Yf/Z7Aq1dUFcVHedV7d1cNK7amxzAhWG3oZLMyD8m4S8YiA5B9uCQdu5PlFhZY p3sI/mJdcDE3Gqy5Q1+dt6Gps78Ezd9vKzyaisq6YPbdF5kCKY677rERTIMWVH81LF2E JkKA== MIME-Version: 1.0 X-Received: by 10.68.112.164 with SMTP id ir4mr14011457pbb.153.1394111785163; Thu, 06 Mar 2014 05:16:25 -0800 (PST) In-Reply-To: References: <8e4c1ab1-e65d-483f-ad9d-6933ae2052c3@googlegroups.com> <85r478bv99.fsf_-_@benfinney.id.au> <53153e66$0$24931$e4fe514c@dreader36.news.xs4all.nl> <59dd57ad-39b0-4c71-a58e-b4ae6517b385@googlegroups.com> <53156a42$0$2923$c3e8da3$76491128@news.astraweb.com> <87iortoic0.fsf@elektro.pacujo.net> Date: Fri, 7 Mar 2014 00:16:24 +1100 Subject: Re: Working with the set of real numbers From: Chris Angelico Cc: Python List Content-Type: text/plain; charset=UTF-8 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: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394111795 news.xs4all.nl 2847 [2001:888:2000:d::a6]:42655 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67937 On Thu, Mar 6, 2014 at 11:27 PM, Oscar Benjamin wrote: > So my loop > > while x ** 2 - y > x * eps: > x = (x + y/x) / 2 > > and Chris' loop: > > while abs(guess1-guess2) > epsilon: > guess1 = n/guess2 > guess2 = (guess1 + guess2)/2 > > and now your loop > > while err > 1e-10: > err = n - guess * guess > guess += err/(2 * guess) > > are all performing the same basic algorithm. The only significant > difference is that mine tests for a relative error condition where as > the other two test for absolute error. This means that it will still > converge to an answer with the correct precision even when the root is > large e.g. sqrt(1e100). The other two are susceptible to infinite > loops in this case. This is one place where I find the REXX 'numeric fuzz' setting to be a useful trick. The definition is that, whenever two numbers are compared for equality, the 'numeric digits' setting is effectively reduced by the fuzz for that comparison. So let's say we're calculating to a precision of 100 digits ('numeric digits 100'). You could then code the loop as "do until guess1=guess2", with a numeric fuzz of, say, 2. That'll give you 98 digits of accuracy, *regardless of scale*. It's like setting epsilon to "whatever would be 1e-98 if we were working with numbers between 0 and 1", more or less. A sliding epsilon. ChrisA