Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!rt.uk.eu.org!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.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'preferred.': 0.05; 'exp': 0.09; 'function:': 0.09; 'def': 0.12; '(1,': 0.16; '*must*': 0.16; '23,': 0.16; 'decimal.': 0.16; 'enough.': 0.16; 'sign,': 0.16; 'simplest': 0.16; 'str()': 0.16; 'wrote:': 0.18; 'aug': 0.22; "i've": 0.25; 'subject:/': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'function': 0.29; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; "d'aprano": 0.31; 'decimal': 0.31; 'faster,': 0.31; 'steven': 0.31; 'subject:numbers': 0.31; 'fri,': 0.33; 'guess': 0.33; 'skip:d 20': 0.34; 'convert': 0.35; 'form.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'version': 0.36; 'example,': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'numbers': 0.61; '50%': 0.78; 'subject:Fast': 0.84; '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=cpt3iCw52jmChHY8Tp63BlCE7yF7AGochCCSFSfMXTA=; b=uwqO2ihONAQyRwiZK0rqt76ZBKk40KG9LW+9xri817ZecXp5b4PpRwvFKUOloV6Ln7 lIBxawch8jwdp26SV+VSe5r9VCVheC1n27U0xKXBp5mfDQSlWUGd17tt+ox18wL1qoor LnS5BzS2fHAdorTdaLlj0krcyi2pcXSV0LCjUpQf2Ar9XpNgqvHJMgpAbDcWCiF59XbB M1b1CDkizQR6pv7gSMdOhdSah6t4MycGqNJ321MRE5XXe6RYqXUU7YallVQVrnxnQB8X egaIrZT+wUUEsLkNq/+75JXhTk4PxSOvVNAfaMnkb1cs6kUSiUlhsSexnb0htxlgF0H0 YUbQ== X-Received: by 10.68.27.37 with SMTP id q5mr3839228pbg.145.1377329913580; Sat, 24 Aug 2013 00:38:33 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <521828e7$0$29986$c3e8da3$5496439d@news.astraweb.com> References: <521828e7$0$29986$c3e8da3$5496439d@news.astraweb.com> From: Ian Kelly Date: Sat, 24 Aug 2013 01:37:53 -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: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1377329921 news.xs4all.nl 15997 [2001:888:2000:d::a6]:52128 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52930 On Fri, Aug 23, 2013 at 9:30 PM, Steven D'Aprano wrote: > Is there a fast way to convert a Decimal into a pair of numbers numerator/ > denominator? It *must* be exact, but it doesn't have to be simplest form. > For example, Decimal("0.5") => (5, 10) would be okay, although (1, 2) > would be preferred. > > > I've tried this function: > > def convert(d): > sign, digits, exp = d.as_tuple() > num = int(''.join([str(digit) for digit in digits])) > if sign: num = -num > return num, 10**-exp > > > which is faster, but not fast enough. Any suggestions? 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