diff options
author | Mark Dickinson <dickinsm@gmail.com> | 2010-04-03 11:18:52 (GMT) |
---|---|---|
committer | Mark Dickinson <dickinsm@gmail.com> | 2010-04-03 11:18:52 (GMT) |
commit | 98127c37167a6735dba576819514c334c89e5b9c (patch) | |
tree | d481c794b5c67ae901a9f627bc225452c7701284 /Lib | |
parent | ac256ab2843dfb6c28af0227202df67664ed462e (diff) | |
download | cpython-98127c37167a6735dba576819514c334c89e5b9c.zip cpython-98127c37167a6735dba576819514c334c89e5b9c.tar.gz cpython-98127c37167a6735dba576819514c334c89e5b9c.tar.bz2 |
Merged revisions 79629 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r79629 | mark.dickinson | 2010-04-02 23:27:36 +0100 (Fri, 02 Apr 2010) | 2 lines
Issue #8294: Allow float and Decimal arguments in Fraction constructor.
........
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/fractions.py | 62 | ||||
-rw-r--r-- | Lib/test/test_fractions.py | 27 |
2 files changed, 80 insertions, 9 deletions
diff --git a/Lib/fractions.py b/Lib/fractions.py index 9624c90..fc8a12c 100644 --- a/Lib/fractions.py +++ b/Lib/fractions.py @@ -3,6 +3,7 @@ """Fraction, infinite-precision, real numbers.""" +from decimal import Decimal import math import numbers import operator @@ -41,13 +42,21 @@ _RATIONAL_FORMAT = re.compile(r""" class Fraction(numbers.Rational): """This class implements rational numbers. - Fraction(8, 6) will produce a rational number equivalent to - 4/3. Both arguments must be Integral. The numerator defaults to 0 - and the denominator defaults to 1 so that Fraction(3) == 3 and - Fraction() == 0. + In the two-argument form of the constructor, Fraction(8, 6) will + produce a rational number equivalent to 4/3. Both arguments must + be Rational. The numerator defaults to 0 and the denominator + defaults to 1 so that Fraction(3) == 3 and Fraction() == 0. - Fraction can also be constructed from strings of the form - '[-+]?[0-9]+((/|.)[0-9]+)?', optionally surrounded by spaces. + Fractions can also be constructed from: + + - numeric strings similar to those accepted by the + float constructor (for example, '-2.3' or '1e10') + + - strings of the form '123/456' + + - float and Decimal instances + + - other Rational instances (including integers) """ @@ -57,8 +66,32 @@ class Fraction(numbers.Rational): def __new__(cls, numerator=0, denominator=None): """Constructs a Rational. - Takes a string like '3/2' or '1.5', another Rational, or a - numerator/denominator pair. + Takes a string like '3/2' or '1.5', another Rational instance, a + numerator/denominator pair, or a float. + + Examples + -------- + + >>> Fraction(10, -8) + Fraction(-5, 4) + >>> Fraction(Fraction(1, 7), 5) + Fraction(1, 35) + >>> Fraction(Fraction(1, 7), Fraction(2, 3)) + Fraction(3, 14) + >>> Fraction('314') + Fraction(314, 1) + >>> Fraction('-35/4') + Fraction(-35, 4) + >>> Fraction('3.1415') # conversion from numeric string + Fraction(6283, 2000) + >>> Fraction('-47e-2') # string may include a decimal exponent + Fraction(-47, 100) + >>> Fraction(1.47) # direct construction from float (exact conversion) + Fraction(6620291452234629, 4503599627370496) + >>> Fraction(2.25) + Fraction(9, 4) + >>> Fraction(Decimal('1.47')) + Fraction(147, 100) """ self = super(Fraction, cls).__new__(cls) @@ -69,6 +102,19 @@ class Fraction(numbers.Rational): self._denominator = numerator.denominator return self + elif isinstance(numerator, float): + # Exact conversion from float + value = Fraction.from_float(numerator) + self._numerator = value._numerator + self._denominator = value._denominator + return self + + elif isinstance(numerator, Decimal): + value = Fraction.from_decimal(numerator) + self._numerator = value._numerator + self._denominator = value._denominator + return self + elif isinstance(numerator, str): # Handle construction from strings. m = _RATIONAL_FORMAT.match(numerator) diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py index 5ad0742..dd51f9b 100644 --- a/Lib/test/test_fractions.py +++ b/Lib/test/test_fractions.py @@ -12,6 +12,11 @@ from pickle import dumps, loads F = fractions.Fraction gcd = fractions.gcd +# decorator for skipping tests on non-IEEE 754 platforms +requires_IEEE_754 = unittest.skipUnless( + float.__getformat__("double").startswith("IEEE"), + "test requires IEEE 754 doubles") + class DummyFloat(object): """Dummy float class for testing comparisons with Fractions""" @@ -130,13 +135,33 @@ class FractionTest(unittest.TestCase): self.assertRaisesMessage(ZeroDivisionError, "Fraction(12, 0)", F, 12, 0) - self.assertRaises(TypeError, F, 1.5) self.assertRaises(TypeError, F, 1.5 + 3j) self.assertRaises(TypeError, F, "3/2", 3) self.assertRaises(TypeError, F, 3, 0j) self.assertRaises(TypeError, F, 3, 1j) + @requires_IEEE_754 + def testInitFromFloat(self): + self.assertEquals((5, 2), _components(F(2.5))) + self.assertEquals((0, 1), _components(F(-0.0))) + self.assertEquals((3602879701896397, 36028797018963968), + _components(F(0.1))) + self.assertRaises(TypeError, F, float('nan')) + self.assertRaises(TypeError, F, float('inf')) + self.assertRaises(TypeError, F, float('-inf')) + + def testInitFromDecimal(self): + self.assertEquals((11, 10), + _components(F(Decimal('1.1')))) + self.assertEquals((7, 200), + _components(F(Decimal('3.5e-2')))) + self.assertEquals((0, 1), + _components(F(Decimal('.000e20')))) + self.assertRaises(TypeError, F, Decimal('nan')) + self.assertRaises(TypeError, F, Decimal('snan')) + self.assertRaises(TypeError, F, Decimal('inf')) + self.assertRaises(TypeError, F, Decimal('-inf')) def testFromString(self): self.assertEquals((5, 1), _components(F("5"))) |