]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fractions.from_float() no longer loses precision with large integer inputs.
authorRaymond Hettinger <python@rcn.com>
Sat, 3 Jan 2009 09:24:18 +0000 (09:24 +0000)
committerRaymond Hettinger <python@rcn.com>
Sat, 3 Jan 2009 09:24:18 +0000 (09:24 +0000)
Lib/fractions.py
Lib/test/test_fractions.py
Misc/NEWS

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 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 02e5085ca383d4e9edb21d368a455ede8fcf0e0f..2606ac1cef5232e8d5f57182bbe52bf3172c8d6b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -56,6 +56,9 @@ Core and Builtins
 Library
 -------
 
+- Fractions.from_float() no longer loses precision for integers to 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.