code: def float_to_decimal(f):
      
      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
docs: Convert a floating point number to a Decimal with
      no loss of information. Intended for Python 2.6 where
      casting float to Decimal does not work.