From 2122c255413938098939f9bdbaa72ea4fc5eff04 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 3 Jan 2009 09:27:08 +0000 Subject: [PATCH] Fractions.from_float() no longer loses precision with large integer inputs. --- Lib/fractions.py | 2 +- Lib/test/test_fractions.py | 2 ++ Misc/NEWS | 3 +++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Lib/fractions.py b/Lib/fractions.py index bc065243d418..ed1e9a0c1035 100755 --- a/Lib/fractions.py +++ b/Lib/fractions.py @@ -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__)) diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py index 4fadf6caa496..6851d2d31325 100644 --- a/Lib/test/test_fractions.py +++ b/Lib/test/test_fractions.py @@ -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))) diff --git a/Misc/NEWS b/Misc/NEWS index 87dbfd2994ca..a984ee8b319c 100644 --- 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.) -- 2.47.3