]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Simplify one of the decimal recipes.
authorRaymond Hettinger <python@rcn.com>
Sat, 3 Jan 2009 07:46:36 +0000 (07:46 +0000)
committerRaymond Hettinger <python@rcn.com>
Sat, 3 Jan 2009 07:46:36 +0000 (07:46 +0000)
Doc/library/decimal.rst

index 330f139f673a55baec1c6bfcf298449e74ffee51..c90e9b9317d0891209b4091fae5a991220d7536b 100644 (file)
@@ -1880,13 +1880,13 @@ suggest, so we trap :const:`Inexact` to signal a need for more precision:
     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
+        numerator, denominator = Decimal(n), Decimal(d)
+        ctx = Context(prec=60)
+        result = ctx.divide(numerator, denominator)
+        while ctx.flags[Inexact]:
+            ctx.prec *= 2
+            result = ctx.divide(numerator, denominator)
+        return result
 
 .. doctest::