Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed2.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'algorithm': 0.04; 'represents': 0.05; 'root': 0.05; 'float': 0.07; 'versions.': 0.07; 'subject:set': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '"standard': 0.16; "'from": 0.16; "(i'm": 0.16; '(when': 0.16; 'benjamin': 0.16; 'big-o': 0.16; 'digits.': 0.16; 'division.': 0.16; 'form"': 0.16; 'iteration': 0.16; 'massively': 0.16; 'sec': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'module': 0.19; 'basically': 0.19; '>>>': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'replace': 0.24; 'cc:2**0': 0.24; 'compare': 0.26; 'first,': 0.26; 'second': 0.26; 'certain': 0.27; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'chris': 0.29; 'am,': 0.29; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:wiki': 0.31; 'decimal': 0.31; 'division': 0.31; 'subject:numbers': 0.31; 'time;': 0.31; 'url:wikipedia': 0.31; 'probably': 0.32; 'figure': 0.32; "we're": 0.32; 'subject:the': 0.34; "i'd": 0.34; 'subject:with': 0.35; 'beyond': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'version': 0.36; 'earth': 0.36; 'in:': 0.36; 'done': 0.36; 'method': 0.36; 'url:org': 0.36; 'two': 0.37; 'performance': 0.37; 'version,': 0.38; 'anything': 0.39; 'sure': 0.39; 'how': 0.40; 'is.': 0.60; 'number,': 0.60; 'hardware': 0.61; 'march': 0.61; 'numbers': 0.61; 'here:': 0.62; 'such': 0.63; 'talking': 0.65; 'to:addr:gmail.com': 0.65; 'mar': 0.68; '10000': 0.68; 'answer.': 0.68; '9:02': 0.84; 'oscar': 0.84; 'way)': 0.84 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=En6nVA9omyioeDv6GV9DwJnW/Aqg6gdWYU1xVQwuN9g=; b=U/trYyEkijog4yE8UhcpVxJJ1Vvj8PkxoLgjOSF3rdvVjlFPUDT2GgD5im887TkzVv eIgDHcDn8uNmQ07Bi7x1M+rIYupc33aUgRV5bOT9KmowYCavC5YjZWdoanRPhxY6QLbz qbjubSFSkyhsZvFctJzxV4zKQCuANsefcyubCo++LrPYKAbzMncy+IDBWspTSwL07PbZ rsQjAcgl9Zc6xXPFYlyF7seRaF0rEH3uAND1XdT5Qdq2KcQrMjdjze8mEGrlhtfuStDO fcjRJsDabyWiO/iofdNfeAvtLDK1ANwN4nkhxDFQEeTqiA11+EnBiPx76bYGP/RzCiJ1 F21g== X-Received: by 10.58.238.35 with SMTP id vh3mr1772979vec.16.1393973662723; Tue, 04 Mar 2014 14:54:22 -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: Tue, 4 Mar 2014 22:54:01 +0000 Subject: Re: Working with the set of real numbers To: Chris Angelico Content-Type: text/plain; charset=ISO-8859-1 Cc: "python-list@python.org" 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: 1393973669 news.xs4all.nl 2850 [2001:888:2000:d::a6]:41335 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67746 On 4 March 2014 22:18, Chris Angelico wrote: > On Wed, Mar 5, 2014 at 9:02 AM, Oscar Benjamin > wrote: >> On 4 March 2014 21:18, Chris Angelico wrote: >>> On Wed, Mar 5, 2014 at 7:55 AM, Oscar Benjamin >>> wrote: >>> >>> epsilon = 0.0001 >>> def sqrt(n): >>> guess1, guess2 = 1, n >>> while abs(guess1-guess2) > epsilon: >>> guess1 = n/guess2 >>> guess2 = (guess1 + guess2)/2 >>> return guess1 >> >> That's the exact same algorithm I showed! How on earth would you call >> that brute force? > > It uses a lot of division. There are various refinements that can be > done that replace some of that division with multiplication, but I'd > have to go do some research to figure it out. There's a description of such a method here: http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Iterative_methods_for_reciprocal_square_roots I don't know whether that would work out faster (when using decimal - for float it would probably be slower). > Let's compare two > versions. In the first, you set the precision (I'm talking in terms of > REXX's "NUMERIC DIGITS" statement I have no idea what that is. >- anything beyond this many digits > will be rounded (and represented exponentially, if necessary); I'm not > sure if decimal.Decimal precision works this way) such that you get 10 > digits. With the decimal module if you set the precision to 5 digits then it basically represents the number in "standard form" with 5 digits .e.g: 1.2345 x 10**21. > Each iteration requires division by a 10-digit number, which > is an operation that takes a certain amount of time; and it's going to > take some number of iterations to get to the final answer. > > Second version, you set the precision so you get 20 digits. If we're talking about 10-20 digits then the decimal module is overkill: just use float. The speed up from hardware arithmetic will massively out-weigh any other performance considerations. My version was intended to produce large numbers of digits which is when the big-O comes in: $ python3.3 -m timeit -s 'from root import sqrt' 'sqrt(2, 10)' 10000 loops, best of 3: 22.4 usec per loop $ python3.3 -m timeit -s 'from root import sqrt' 'sqrt(2, 100)' 10000 loops, best of 3: 59.1 usec per loop $ python3.3 -m timeit -s 'from root import sqrt' 'sqrt(2, 1000)' 1000 loops, best of 3: 1.15 msec per loop $ python3.3 -m timeit -s 'from root import sqrt' 'sqrt(2, 10000)' 10 loops, best of 3: 85.9 msec per loop $ python3.3 -m timeit -s 'from root import sqrt' 'sqrt(2, 100000)' 10 loops, best of 3: 1.59 sec per loop Oscar