]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Manually merge r68095,68186,68187,68188,68190 from 2.6 branch.
authorGeorg Brandl <georg@python.org>
Sat, 3 Jan 2009 22:03:11 +0000 (22:03 +0000)
committerGeorg Brandl <georg@python.org>
Sat, 3 Jan 2009 22:03:11 +0000 (22:03 +0000)
Doc/library/decimal.rst
Lib/fractions.py
Lib/heapq.py
Lib/test/test_fractions.py
Misc/NEWS

index 73de35854bfbf5e135ca9f7b2a91d07e6e485d55..e793a4587cf79b6ebfba162cdffca0b87db9eebf 100644 (file)
@@ -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::
 
index 4adb184a9eeaace3c7bb642468b7e20f9cd4b91e..446ad8ea8279a1c754c1055e0b2c411731861fcb 100755 (executable)
@@ -111,7 +111,7 @@ class Fraction(Rational):
 
         """
         if isinstance(f, numbers.Integral):
-            f = float(f)
+            return cls(f)
         elif not isinstance(f, float):
             raise TypeError("%s.from_float() only takes floats, not %r (%s)" %
                             (cls.__name__, f, type(f).__name__))
index 0a83b76b464d1db250ea8172fc4ed17ab9ef54e2..c13d6500b6e44d8759f186f64c7540ebc096a9c5 100644 (file)
@@ -354,6 +354,10 @@ def nsmallest(n, iterable, key=None):
 
     Equivalent to:  sorted(iterable, key=key)[:n]
     """
+    if key is None:
+        it = izip(iterable, count())                        # decorate
+        result = _nsmallest(n, it)
+        return map(itemgetter(0), result)                   # undecorate
     in1, in2 = tee(iterable)
     it = izip(imap(key, in1), count(), in2)                 # decorate
     result = _nsmallest(n, it)
@@ -365,6 +369,10 @@ def nlargest(n, iterable, key=None):
 
     Equivalent to:  sorted(iterable, key=key, reverse=True)[:n]
     """
+    if key is None:
+        it = izip(iterable, imap(neg, count()))             # decorate
+        result = _nlargest(n, it)
+        return map(itemgetter(0), result)                   # undecorate
     in1, in2 = tee(iterable)
     it = izip(imap(key, in1), imap(neg, count()), in2)      # decorate
     result = _nlargest(n, it)
index 979fef75cdf725e9306ed164013ce1a463472fb3..a6c3c32cb09e271dd83b8655b33740950f9fab55 100644 (file)
@@ -139,6 +139,8 @@ class FractionTest(unittest.TestCase):
     def testFromFloat(self):
         self.assertRaises(TypeError, F.from_float, 3+4j)
         self.assertEquals((10, 1), _components(F.from_float(10)))
+        bigint = 1234567890123456789
+        self.assertEquals((bigint, 1), _components(F.from_float(bigint)))
         self.assertEquals((0, 1), _components(F.from_float(-0.0)))
         self.assertEquals((10, 1), _components(F.from_float(10.0)))
         self.assertEquals((-5, 2), _components(F.from_float(-2.5)))
index 02a7df0e3bd9718f079632cc41d5bb53d2ff14fa..078824e916006dede921bf579b18ad940b8f3245 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -127,6 +127,12 @@ Library
 - Issue #4646: distutils was choking on empty options arg in the setup 
   function. Original patch by Thomas Heller.
 
+- Fractions.from_float() no longer loses precision for integers too big to
+  cast as floats.
+
+- Issue 4790: The nsmallest() and nlargest() functions in the heapq module
+  did unnecessary work in the common case where no key function was specified.
+
 - Issue #3767: Convert Tk object to string in tkColorChooser.
 
 - Issue #3248: Allow placing ScrolledText in a PanedWindow.