]> 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:27:08 +0000 (09:27 +0000)
committerRaymond Hettinger <python@rcn.com>
Sat, 3 Jan 2009 09:27:08 +0000 (09:27 +0000)
Lib/fractions.py
Lib/test/test_fractions.py
Misc/NEWS

index bc065243d418b56e8bfb32c456f15b48d089da0f..ed1e9a0c1035e570366840efbf0405172a724117 100755 (executable)
@@ -109,7 +109,7 @@ class Fraction(numbers.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 4fadf6caa49656394674a5203de0de0d00a92fca..6851d2d31325970fede57af8f0f068e454e2ef99 100644 (file)
@@ -136,6 +136,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 87dbfd2994cafb32a179aa5291dfc71a170c6d56..a984ee8b319ca237fe9bf0f8090aad0c01ca9ea4 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -48,6 +48,9 @@ Core and Builtins
 Library
 -------
 
+- Fractions.from_float() no longer loses precision for integers too big to
+  cast as floats.
+
 - Issue #4812: add missing underscore prefix to some internal-use-only
   constants in the decimal module.  (Dec_0 becomes _Dec_0, etc.)