summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2009-01-03 09:24:18 (GMT)
committerRaymond Hettinger <python@rcn.com>2009-01-03 09:24:18 (GMT)
commit40188782dd9cf3495293d8fe07070d4c642389fd (patch)
treeb227405566bfdf87272045a2c27575a50fc1920a
parentc921dacf7842a3a89395c68affa25793ee45d3f3 (diff)
downloadcpython-40188782dd9cf3495293d8fe07070d4c642389fd.zip
cpython-40188782dd9cf3495293d8fe07070d4c642389fd.tar.gz
cpython-40188782dd9cf3495293d8fe07070d4c642389fd.tar.bz2
Fractions.from_float() no longer loses precision with large integer inputs.
-rwxr-xr-xLib/fractions.py2
-rw-r--r--Lib/test/test_fractions.py2
-rw-r--r--Misc/NEWS3
3 files changed, 6 insertions, 1 deletions
diff --git a/Lib/fractions.py b/Lib/fractions.py
index 4adb184..446ad8e 100755
--- a/Lib/fractions.py
+++ b/Lib/fractions.py
@@ -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__))
diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py
index 979fef7..a6c3c32 100644
--- a/Lib/test/test_fractions.py
+++ b/Lib/test/test_fractions.py
@@ -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)))
diff --git a/Misc/NEWS b/Misc/NEWS
index 02e5085..2606ac1 100644
--- 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.