Kaydet (Commit) 40188782 authored tarafından Raymond Hettinger's avatar Raymond Hettinger

Fractions.from_float() no longer loses precision with large integer inputs.

üst c921dacf
...@@ -111,7 +111,7 @@ class Fraction(Rational): ...@@ -111,7 +111,7 @@ class Fraction(Rational):
""" """
if isinstance(f, numbers.Integral): if isinstance(f, numbers.Integral):
f = float(f) return cls(f)
elif not isinstance(f, float): elif not isinstance(f, float):
raise TypeError("%s.from_float() only takes floats, not %r (%s)" % raise TypeError("%s.from_float() only takes floats, not %r (%s)" %
(cls.__name__, f, type(f).__name__)) (cls.__name__, f, type(f).__name__))
......
...@@ -139,6 +139,8 @@ class FractionTest(unittest.TestCase): ...@@ -139,6 +139,8 @@ class FractionTest(unittest.TestCase):
def testFromFloat(self): def testFromFloat(self):
self.assertRaises(TypeError, F.from_float, 3+4j) self.assertRaises(TypeError, F.from_float, 3+4j)
self.assertEquals((10, 1), _components(F.from_float(10))) 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((0, 1), _components(F.from_float(-0.0)))
self.assertEquals((10, 1), _components(F.from_float(10.0))) self.assertEquals((10, 1), _components(F.from_float(10.0)))
self.assertEquals((-5, 2), _components(F.from_float(-2.5))) self.assertEquals((-5, 2), _components(F.from_float(-2.5)))
......
...@@ -56,6 +56,9 @@ Core and Builtins ...@@ -56,6 +56,9 @@ Core and Builtins
Library 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 - Issue 4790: The nsmallest() and nlargest() functions in the heapq module
did unnecessary work in the common case where no key function was specified. did unnecessary work in the common case where no key function was specified.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment