Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def float_to_decimal(f):
- "Convert a floating point number to a Decimal with no loss of information"
- n, d = f.as_integer_ratio()
- numerator, denominator = Decimal(n), Decimal(d)
- ctx = Context(prec=60)
- result = ctx.divide(numerator, denominator)
- while ctx.flags[Inexact]:
- ctx.flags[Inexact] = False
- ctx.prec *= 2
- result = ctx.divide(numerator, denominator)
- return result
Add Comment
Please, Sign In to add comment