This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rhettinger
Recipients rhettinger, steven.daprano
Date 2009-01-01.00:43:08
SpamBayes Score 0.0017800307
Marked as misclassified No
Message-id <1230770589.56.0.572003566636.issue4796@psf.upfronthosting.co.za>
In-reply-to
Content
FYI, there is already a lossless implementation in the docs:

def float_to_decimal(f):
    "Convert a floating point number to a Decimal with no loss of
information"
    n, d = f.as_integer_ratio()
    with localcontext() as ctx:
        ctx.traps[Inexact] = True
        while True:
            try:
               return Decimal(n) / Decimal(d)
            except Inexact:
                ctx.prec += 1
History
Date User Action Args
2009-01-01 00:43:09rhettingersetrecipients: + rhettinger, steven.daprano
2009-01-01 00:43:09rhettingersetmessageid: <1230770589.56.0.572003566636.issue4796@psf.upfronthosting.co.za>
2009-01-01 00:43:08rhettingerlinkissue4796 messages
2009-01-01 00:43:08rhettingercreate