diff options
author | Raymond Hettinger <python@rcn.com> | 2009-01-03 07:46:36 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2009-01-03 07:46:36 (GMT) |
commit | b3833ddecd2217e904c90a72f82a13ace3a348c8 (patch) | |
tree | 890cee1a5b23fe20458236f6acb2ae5fa37900e5 /Doc | |
parent | fd6032d4522945805959030b47ab07985a785ffa (diff) | |
download | cpython-b3833ddecd2217e904c90a72f82a13ace3a348c8.zip cpython-b3833ddecd2217e904c90a72f82a13ace3a348c8.tar.gz cpython-b3833ddecd2217e904c90a72f82a13ace3a348c8.tar.bz2 |
Simplify one of the decimal recipes.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/decimal.rst | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/Doc/library/decimal.rst b/Doc/library/decimal.rst index 330f139..c90e9b9 100644 --- a/Doc/library/decimal.rst +++ b/Doc/library/decimal.rst @@ -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:: |