Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'numeric': 0.07; 'semantic': 0.07; 'python': 0.08; '>>>>': 0.09; 'float.': 0.09; "it'd": 0.09; 'precision': 0.09; 'am,': 0.14; 'wrote:': 0.14; 'angelico': 0.16; 'computation': 0.16; 'floats': 0.16; 'subject:Fibonacci': 0.16; 'truncated': 0.16; 'long,': 0.16; 'cc:addr:python-list': 0.17; 'continuing': 0.17; 'awesome': 0.19; 'header:In-Reply-To:1': 0.21; 'cc:2**0': 0.22; 'cc:no real name:2**0': 0.23; 'integer': 0.23; 'fri,': 0.23; 'message- id:@mail.gmail.com': 0.28; 'problem': 0.28; '(as': 0.29; 'import': 0.29; 'cc:addr:python.org': 0.30; 'steven': 0.32; 'someone': 0.33; "isn't": 0.33; 'chris': 0.34; 'shows': 0.34; 'there': 0.35; "d'aprano": 0.35; 'fraction': 0.35; 'numbers.': 0.35; 'else': 0.35; 'using': 0.35; 'uses': 0.36; 'difference': 0.37; 'too.': 0.37; 'received:google.com': 0.37; 'something': 0.37; '20,': 0.37; 'received:74.125': 0.38; 'could': 0.38; 'but': 0.38; 'subject:: ': 0.38; 'more': 0.60; 'cost': 0.65; 'square': 0.67; 'promote': 0.78 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=nLh29BtuIQJMLhaZu0eprXrseCw8d/EdbdJ0V/+F5Hc=; b=B1KAYgmDfMVlS1aXfWdJOf8l5msj1+Ty+lReBuqKafWdk2CYtLjBLznhpDfhmgg4St mib888uaX1F4Y9QSahZmunpB0UCbwi7/7mOqixYgq0QrjjuCU1R5P1/8L7LEKv9uz9+f y99F2Yfdg09B0ba1GWIJJCIUZL1HAXWCOojW0= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=JpglcobJUq7eWApPEkiPuAHuzbmYhY9f9koVrRE8lDIsWLp73kkPMTC2XWyZ3qbiHV vTrRB20dZPVDWUZJ6FoyozFmgxZ852S4yPF1rX+4MwN0bvb4M2ZSlRxzuMgZqzdredcW y+2s6jvgCIy4rmI0L9v2Y/9a32sgUrx1dvRpM= MIME-Version: 1.0 In-Reply-To: <4dd69fcd$0$29996$c3e8da3$5496439d@news.astraweb.com> References: <108ce447-10fa-4cf7-84ef-440ee18dbfd4@22g2000prx.googlegroups.com> <9d9c163b-14fd-4131-81bb-105c3b97c432@h36g2000pro.googlegroups.com> <4dd59e1e$0$29996$c3e8da3$5496439d@news.astraweb.com> <4dd602ec$0$29996$c3e8da3$5496439d@news.astraweb.com> <4dd69fcd$0$29996$c3e8da3$5496439d@news.astraweb.com> Date: Fri, 20 May 2011 11:43:43 -0700 Subject: Re: Faster Recursive Fibonacci Numbers From: geremy condra To: "Steven D'Aprano" Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 34 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1305917026 news.xs4all.nl 49181 [::ffff:82.94.164.166]:36687 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5871 On Fri, May 20, 2011 at 10:07 AM, Steven D'Aprano wrote: > On Fri, 20 May 2011 16:54:06 +1000, Chris Angelico wrote: > >> If someone has time to kill (as if!), it'd be awesome to get a new >> numeric type that uses bc's code; any other numeric type (int, long, >> float) could autopromote to it, removing the dilemma of which to promote >> out of long and float. Hmm... Python 4.0, 'bc' is the new default >> integer and everything else is a performance optimization? Heh. > > The problem is, it isn't *just* a performance optimization, there is a > semantic difference too. Consider: > >>>> x = 1e-300 >>>> x*x == 0 > True > > But using something with more precision: > >>>> from fractions import Fraction >>>> x = Fraction(10)**-300 >>>> x*x == 0 > False > > > So you get different behaviour between floats and arbitrary precision > numbers. And this shows up in the above implementation; reimplementing it using Fractions and a truncated continuing fraction approximation of phi and the square root of 5 gets us up to about 500, at the cost of a very long computation time. Geremy Condra