Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52931

Re: Fast conversion of numbers to numerator/denominator pairs

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 <ian.g.kelly@gmail.com>
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 <CALwzidm5km=wzMJn4RoR9hcnSOUqyyq-BWo89owMHBGwTyztLA@mail.gmail.com>
References <521828e7$0$29986$c3e8da3$5496439d@news.astraweb.com> <CALwzidm5km=wzMJn4RoR9hcnSOUqyyq-BWo89owMHBGwTyztLA@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Sat, 24 Aug 2013 01:50:08 -0600
Subject Re: Fast conversion of numbers to numerator/denominator pairs
To Python <python-list@python.org>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.191.1377330651.19984.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Sat, Aug 24, 2013 at 1:37 AM, Ian Kelly <ian.g.kelly@gmail.com> 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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Fast conversion of numbers to numerator/denominator pairs Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-24 03:30 +0000
  Re: Fast conversion of numbers to numerator/denominator pairs Ian Kelly <ian.g.kelly@gmail.com> - 2013-08-24 01:37 -0600
  Re: Fast conversion of numbers to numerator/denominator pairs Ian Kelly <ian.g.kelly@gmail.com> - 2013-08-24 01:50 -0600
  Re: Fast conversion of numbers to numerator/denominator pairs Peter Otten <__peter__@web.de> - 2013-08-24 14:52 +0200
  Re: Fast conversion of numbers to numerator/denominator pairs Tim Delaney <timothy.c.delaney@gmail.com> - 2013-08-25 07:59 +1000
  Re: Fast conversion of numbers to numerator/denominator pairs Tim Delaney <timothy.c.delaney@gmail.com> - 2013-08-25 08:05 +1000

csiph-web