Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.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.023 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'python,': 0.02; 'algorithm': 0.04; 'subsequent': 0.05; 'counting': 0.09; 'machines.': 0.09; 'repeated': 0.09; 'since.': 0.09; 'slow.': 0.09; 'subject:set': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'assume': 0.14; 'cc:name:python list': 0.16; 'curious.': 0.16; 'digits.': 0.16; 'inspiration': 0.16; 'integers,': 0.16; 'multiplies': 0.16; 'prec': 0.16; 'rounding': 0.16; 'worse.': 0.16; "would've": 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'either.': 0.24; 'math': 0.24; "haven't": 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'shown': 0.26; 'this:': 0.26; 'header:In- Reply-To:1': 0.27; 'fixed': 0.29; 'needed.': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'included': 0.31; 'decimal': 0.31; 'division': 0.31; 'subject:numbers': 0.31; 'anyone': 0.31; 'agreed': 0.32; 'ago': 0.33; 'guess': 0.33; 'subject:the': 0.34; "i'd": 0.34; 'could': 0.34; 'problem': 0.35; 'subject:with': 0.35; 'done.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'done': 0.36; 'method': 0.36; "i'll": 0.36; 'too': 0.37; 'two': 0.37; 'machines': 0.38; 'little': 0.38; 'even': 0.60; 'easy': 0.60; 'contributing': 0.60; 'dave': 0.60; 'hardware': 0.61; 'march': 0.61; 'took': 0.61; "you're": 0.61; 'skip:n 10': 0.64; 'between': 0.67; '10000': 0.68; 'miss': 0.74; 'square': 0.74; 'as:': 0.81; 'glad': 0.83; 'complexity': 0.84; 'divide': 0.84; 'eps': 0.84; 'fast,': 0.84; 'father': 0.84; 'oscar': 0.84; 'penalty': 0.84; 'slope': 0.84; 'trial)': 0.84; 'angel': 0.91; 'story.': 0.93 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 :cc:content-type; bh=03PAQRph+DgfgasB6Q6wv1fxHyumU02p/6yb1HmtbII=; b=eMcuxdiVrGfvSugSdO1pTHl/OhX9IRsJrnZ7EcbLjwoF3idJC37sD8yZbfLbYAA0C1 4Hgd5Zr3qTudNYJyTt3kkfEyjMBSiEn+7YluTj3zKkK7dN6NCVv19P8/F0pBi//UaPEy veAPz5Ixi9boCSXYUgsMEpDJZMJ/HqUoxIjWLKTVHvffK7xi0EZmRCnPpDzOtPz87tN0 1HoYuNAtag/iUYX3bWuGOgJKb9QSaaeF5Rbg7+CEJM9dPloTHHxC1DVTnqBWACgltQ2o GXK88hDy8vVaFXuihftlgw3vGosgG+sf8MtDzLvwdJJtlhzoexjhB7Wf2a8Us7lJkm+b XO9g== X-Received: by 10.58.69.111 with SMTP id d15mr60810veu.3.1394020807676; Wed, 05 Mar 2014 04:00:07 -0800 (PST) MIME-Version: 1.0 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> From: Oscar Benjamin Date: Wed, 5 Mar 2014 11:59:47 +0000 Subject: Re: Working with the set of real numbers To: Dave Angel Content-Type: text/plain; charset=ISO-8859-1 Cc: Python List 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: 68 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394020816 news.xs4all.nl 2936 [2001:888:2000:d::a6]:59308 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67833 On 4 March 2014 23:20, Dave Angel wrote: > > One problem with complexity claims is that it's easy to miss some > contributing time eaters. I haven't done any measuring on modern > machines nor in python, but I'd assume that multiplies take > *much* longer for large integers, and that divides are much > worse. So counting iterations isn't the whole story. Agreed but there's a big difference between log(N) iterations and N iterations! > On the assumption that division by 2 is very fast, and that a > general multiply isn't too bad, you could improve on Newton by > observing that the slope is 2. > > err = n - guess * guess > guess += err/2 I gues you mean like this: def sqrt(n): err = guess = 1 while err > 1e-10: err = n - guess * guess guess += err/2 return guess This requires log2(10)*N iterations to get N digits. So the penalty for using division would have to be extreme in order for this to better. Using Decimal to get many digits we can write that as: def sqrt2(n, prec=1000): '''Solve x**2 = y''' eps = D(10) ** -(prec + 5) err = guess = D(1) with localcontext() as ctx: ctx.prec = prec + 10 while abs(err) > eps: err = n - guess*guess guess += err/2 return guess This method works out much slower than Newton with division at 10000 digits: 40s (based on a single trial) vs 80ms (timeit result). > Some 37 years ago I microcoded a math package which included > square root. All the math was decimal, and there was no hardware > multiply or divide. The algorithm I came up with generated the > answer one digit at a time, with no subsequent rounding needed. > And it took just a little less time than two divides. For that > architecture, Newton's method would've been too > slow. If you're working with a fixed small precision then it might be. > Incidentally, the algorithm did no divides, not even by 2. No > multiplies either. Just repeated subtraction, sorta like divide > was done. > > If anyone is curious, I'll be glad to describe the algorithm; > I've never seen it published, before or since. I got my > inspiration from a method used in mechanical, non-motorized, > adding machines. My father had shown me that approach in the > 50's. I'm curious. Oscar