Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python,': 0.02; 'algorithm': 0.04; 'subsequent': 0.05; 'remaining': 0.07; 'counting': 0.09; 'machines.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'repeated': 0.09; 'since.': 0.09; 'slow.': 0.09; 'subject:set': 0.09; 'def': 0.12; 'assume': 0.14; 'wrote': 0.14; 'benjamin': 0.16; 'curious.': 0.16; 'digits.': 0.16; 'guess.': 0.16; 'inspiration': 0.16; 'integers,': 0.16; 'multiplies': 0.16; 'prec': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'rounding': 0.16; 'worse.': 0.16; "would've": 0.16; 'wrote:': 0.18; 'later': 0.20; 'error': 0.23; 'either.': 0.24; 'math': 0.24; "haven't": 0.24; '(or': 0.24; "i've": 0.25; 'shown': 0.26; 'this:': 0.26; 'header:X-Complaints- To:1': 0.27; 'idea': 0.28; 'fixed': 0.29; 'scale': 0.29; 'needed.': 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; "can't": 0.35; 'done.': 0.35; 'but': 0.35; 'there': 0.35; 'surely': 0.36; 'done': 0.36; 'method': 0.36; "i'll": 0.36; 'should': 0.36; 'too': 0.37; 'two': 0.37; 'machines': 0.38; 'to:addr:python-list': 0.38; 'that,': 0.38; 'little': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; '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; 'stated': 0.69; 'miss': 0.74; 'square': 0.74; 'as:': 0.81; 'glad': 0.83; 'algorithm,': 0.84; '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 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dave Angel Subject: Re: Working with the set of real numbers Date: Wed, 5 Mar 2014 07:57:49 -0500 (EST) Organization: news.gmane.org 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> X-Gmane-NNTP-Posting-Host: dpc6744192186.direcpc.com X-Newsreader: PiaoHong Usenet NewsReaders 1.36 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: 92 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394024033 news.xs4all.nl 2963 [2001:888:2000:d::a6]:38931 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67844 Oscar Benjamin Wrote in message: > 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. No idea how you came up with that, but I see an error in my stated algorithm, which does surely penalize it. The slope isn't 2, but 2x. So the line should have been guess += err/(2*guess) Now if you stop the loop after 3 iterations (or use some other approach to get a low-precision estimate, then you can calculate scale = 1/(2*estimate) and then for remaining iterations, guess += err *scale > 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). Well considering you did not special-case the divide by 2, I'm not surprised it's slower. > >> 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. > A later message, I guess. I can't write that much on the tablet. -- DaveA