Fast conversion of numbers to numerator/denominator pairs
Ian Kelly
ian.g.kelly at gmail.com
Sat Aug 24 03:50:08 EDT 2013
On Sat, Aug 24, 2013 at 1:37 AM, Ian Kelly <ian.g.kelly at 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.
More information about the Python-list
mailing list