summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2009-01-03 22:03:11 (GMT)
committerGeorg Brandl <georg@python.org>2009-01-03 22:03:11 (GMT)
commitfe427895b58769840f1f61a82ea0cdfe55150347 (patch)
treeba6fc9493239a122199292caada1e9345d26e8f6 /Doc
parentc63785db867804de8b676ddddcc90c3fcbf2c111 (diff)
downloadcpython-fe427895b58769840f1f61a82ea0cdfe55150347.zip
cpython-fe427895b58769840f1f61a82ea0cdfe55150347.tar.gz
cpython-fe427895b58769840f1f61a82ea0cdfe55150347.tar.bz2
Manually merge r68095,68186,68187,68188,68190 from 2.6 branch.
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/decimal.rst15
1 files changed, 8 insertions, 7 deletions
diff --git a/Doc/library/decimal.rst b/Doc/library/decimal.rst
index 73de358..e793a45 100644
--- a/Doc/library/decimal.rst
+++ b/Doc/library/decimal.rst
@@ -1923,13 +1923,14 @@ 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.flags[Inexact] = False
+ ctx.prec *= 2
+ result = ctx.divide(numerator, denominator)
+ return result
.. doctest::