Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #52931
| References | <521828e7$0$29986$c3e8da3$5496439d@news.astraweb.com> <CALwzidm5km=wzMJn4RoR9hcnSOUqyyq-BWo89owMHBGwTyztLA@mail.gmail.com> |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | 2013-08-24 01:50 -0600 |
| Subject | Re: Fast conversion of numbers to numerator/denominator pairs |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.191.1377330651.19984.python-list@python.org> (permalink) |
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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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