Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.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.044 X-Spam-Evidence: '*H*': 0.92; '*S*': 0.01; 'context': 0.07; 'exp': 0.09; 'def': 0.12; '24,': 0.16; 'although,': 0.16; 'decimal.': 0.16; 'str()': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'aug': 0.22; 'handling': 0.26; 'subject:/': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'decimal': 0.31; 'subject:numbers': 0.31; 'guess': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.35; 'version': 0.36; 'to:addr :python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'ian': 0.60; '50%': 0.78; 'subject:Fast': 0.84; 'careful': 0.91; '2013': 0.98 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 :content-type; bh=icpTMIRMN6vOD2uIWDuYSAEIRHPTc4bQZcqF45gPDGw=; b=vkxbnmPzKXn/+pcoEoWTtA7jNYzN51HjFQHmhE9IQT8wx1hyT7+GztJbh0Qwlixw9n vmCooWG7skCoKbQW4vgYe6fKkErrqoxj+yd2MtU2fhDnGI8j9OnzylaPlHuKq140MBui FQ8rclGs1fKmLFZWODoXzYVQYKW72clU4jZ9GitfeWL5f2e3yqlSE1ihKkadvOkr+Y3r p1QErdRYxD0A5z5Fc2ivUTFWHOv/MJuX/D7XTOd6QoePn1wVcHFHR+4X02wHd1UzqG73 NRq0Ab4eq+En1dDtVN06JcxpStv3Wrvn+KuayrzvDxbWyggtwWazNnPGGa0MuQrq4SD/ AI2g== X-Received: by 10.68.178.35 with SMTP id cv3mr3830594pbc.160.1377330648700; Sat, 24 Aug 2013 00:50:48 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <521828e7$0$29986$c3e8da3$5496439d@news.astraweb.com> From: Ian Kelly Date: Sat, 24 Aug 2013 01:50:08 -0600 Subject: Re: Fast conversion of numbers to numerator/denominator pairs To: Python Content-Type: text/plain; charset=ISO-8859-1 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: 14 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1377330651 news.xs4all.nl 15869 [2001:888:2000:d::a6]:55070 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52931 On Sat, Aug 24, 2013 at 1:37 AM, Ian Kelly wrote: > > I time this function at about 33% faster than your version for a > six-digit decimal, and almost 50% faster for a 12-digit decimal. My > guess would be because it's not calling str() on every individual > digit. > > def convert(d): > exp = d.as_tuple().exponent > num = int(d.scaleb(-exp)) > return num, 10**-exp Although, you would need to be careful with handling the decimal context for the scaleb operation to make sure the result is exact.