Raymond Hettinger <[email protected]> added the comment:
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
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue4796>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com