Skip to content

Commit 98127c3

Browse files
committed
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. ........
1 parent ac256ab commit 98127c3

File tree

4 files changed

+121
-20
lines changed

4 files changed

+121
-20
lines changed

Doc/library/fractions.rst

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,24 @@ another rational number, or from a string.
1515

1616
.. class:: Fraction(numerator=0, denominator=1)
1717
Fraction(other_fraction)
18+
Fraction(float)
19+
Fraction(decimal)
1820
Fraction(string)
1921

20-
The first version requires that *numerator* and *denominator* are
21-
instances of :class:`numbers.Rational` and returns a new
22-
:class:`Fraction` instance with value ``numerator/denominator``. If
23-
*denominator* is :const:`0`, it raises a
24-
:exc:`ZeroDivisionError`. The second version requires that
25-
*other_fraction* is an instance of :class:`numbers.Rational` and
26-
returns an :class:`Fraction` instance with the same value. The
27-
last version of the constructor expects a string instance. The
28-
usual form for this string is::
22+
The first version requires that *numerator* and *denominator* are instances
23+
of :class:`numbers.Rational` and returns a new :class:`Fraction` instance
24+
with value ``numerator/denominator``. If *denominator* is :const:`0`, it
25+
raises a :exc:`ZeroDivisionError`. The second version requires that
26+
*other_fraction* is an instance of :class:`numbers.Rational` and returns a
27+
:class:`Fraction` instance with the same value. The next two versions accept
28+
either a :class:`float` or a :class:`decimal.Decimal` instance, and return a
29+
:class:`Fraction` instance with exactly the same value. Note that due to the
30+
usual issues with binary floating-point (see :ref:`tut-fp-issues`), the
31+
argument to ``Fraction(1.1)`` is not exactly equal to 11/10, and so
32+
``Fraction(1.1)`` does *not* return ``Fraction(11, 10)`` as one might expect.
33+
(But see the documentation for the :meth:`limit_denominator` method below.)
34+
The last version of the constructor expects a string or unicode instance.
35+
The usual form for this instance is::
2936

3037
[sign] numerator ['/' denominator]
3138

@@ -55,6 +62,13 @@ another rational number, or from a string.
5562
Fraction(-1, 8)
5663
>>> Fraction('7e-6')
5764
Fraction(7, 1000000)
65+
>>> Fraction(2.25)
66+
Fraction(9, 4)
67+
>>> Fraction(1.1)
68+
Fraction(2476979795053773, 2251799813685248)
69+
>>> from decimal import Decimal
70+
>>> Fraction(Decimal('1.1'))
71+
Fraction(11, 10)
5872

5973

6074
The :class:`Fraction` class inherits from the abstract base class
@@ -63,19 +77,30 @@ another rational number, or from a string.
6377
and should be treated as immutable. In addition,
6478
:class:`Fraction` has the following methods:
6579

80+
.. versionchanged:: 3.2
81+
The :class:`Fraction` constructor now accepts :class:`float` and
82+
:class:`decimal.Decimal` instances.
83+
6684

6785
.. method:: from_float(flt)
6886

6987
This class method constructs a :class:`Fraction` representing the exact
7088
value of *flt*, which must be a :class:`float`. Beware that
7189
``Fraction.from_float(0.3)`` is not the same value as ``Fraction(3, 10)``
7290

91+
.. note:: From Python 3.2 onwards, you can also construct a
92+
:class:`Fraction` instance directly from a :class:`float`.
93+
7394

7495
.. method:: from_decimal(dec)
7596

7697
This class method constructs a :class:`Fraction` representing the exact
7798
value of *dec*, which must be a :class:`decimal.Decimal` instance.
7899

100+
.. note:: From Python 3.2 onwards, you can also construct a
101+
:class:`Fraction` instance directly from a :class:`decimal.Decimal`
102+
instance.
103+
79104

80105
.. method:: limit_denominator(max_denominator=1000000)
81106

@@ -90,10 +115,12 @@ another rational number, or from a string.
90115
or for recovering a rational number that's represented as a float:
91116

92117
>>> from math import pi, cos
93-
>>> Fraction.from_float(cos(pi/3))
118+
>>> Fraction(cos(pi/3))
94119
Fraction(4503599627370497, 9007199254740992)
95-
>>> Fraction.from_float(cos(pi/3)).limit_denominator()
120+
>>> Fraction(cos(pi/3)).limit_denominator()
96121
Fraction(1, 2)
122+
>>> Fraction(1.1).limit_denominator()
123+
Fraction(11, 10)
97124

98125

99126
.. method:: __floor__()

Lib/fractions.py

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
"""Fraction, infinite-precision, real numbers."""
55

6+
from decimal import Decimal
67
import math
78
import numbers
89
import operator
@@ -41,13 +42,21 @@ def gcd(a, b):
4142
class Fraction(numbers.Rational):
4243
"""This class implements rational numbers.
4344
44-
Fraction(8, 6) will produce a rational number equivalent to
45-
4/3. Both arguments must be Integral. The numerator defaults to 0
46-
and the denominator defaults to 1 so that Fraction(3) == 3 and
47-
Fraction() == 0.
45+
In the two-argument form of the constructor, Fraction(8, 6) will
46+
produce a rational number equivalent to 4/3. Both arguments must
47+
be Rational. The numerator defaults to 0 and the denominator
48+
defaults to 1 so that Fraction(3) == 3 and Fraction() == 0.
4849
49-
Fraction can also be constructed from strings of the form
50-
'[-+]?[0-9]+((/|.)[0-9]+)?', optionally surrounded by spaces.
50+
Fractions can also be constructed from:
51+
52+
- numeric strings similar to those accepted by the
53+
float constructor (for example, '-2.3' or '1e10')
54+
55+
- strings of the form '123/456'
56+
57+
- float and Decimal instances
58+
59+
- other Rational instances (including integers)
5160
5261
"""
5362

@@ -57,8 +66,32 @@ class Fraction(numbers.Rational):
5766
def __new__(cls, numerator=0, denominator=None):
5867
"""Constructs a Rational.
5968
60-
Takes a string like '3/2' or '1.5', another Rational, or a
61-
numerator/denominator pair.
69+
Takes a string like '3/2' or '1.5', another Rational instance, a
70+
numerator/denominator pair, or a float.
71+
72+
Examples
73+
--------
74+
75+
>>> Fraction(10, -8)
76+
Fraction(-5, 4)
77+
>>> Fraction(Fraction(1, 7), 5)
78+
Fraction(1, 35)
79+
>>> Fraction(Fraction(1, 7), Fraction(2, 3))
80+
Fraction(3, 14)
81+
>>> Fraction('314')
82+
Fraction(314, 1)
83+
>>> Fraction('-35/4')
84+
Fraction(-35, 4)
85+
>>> Fraction('3.1415') # conversion from numeric string
86+
Fraction(6283, 2000)
87+
>>> Fraction('-47e-2') # string may include a decimal exponent
88+
Fraction(-47, 100)
89+
>>> Fraction(1.47) # direct construction from float (exact conversion)
90+
Fraction(6620291452234629, 4503599627370496)
91+
>>> Fraction(2.25)
92+
Fraction(9, 4)
93+
>>> Fraction(Decimal('1.47'))
94+
Fraction(147, 100)
6295
6396
"""
6497
self = super(Fraction, cls).__new__(cls)
@@ -69,6 +102,19 @@ def __new__(cls, numerator=0, denominator=None):
69102
self._denominator = numerator.denominator
70103
return self
71104

105+
elif isinstance(numerator, float):
106+
# Exact conversion from float
107+
value = Fraction.from_float(numerator)
108+
self._numerator = value._numerator
109+
self._denominator = value._denominator
110+
return self
111+
112+
elif isinstance(numerator, Decimal):
113+
value = Fraction.from_decimal(numerator)
114+
self._numerator = value._numerator
115+
self._denominator = value._denominator
116+
return self
117+
72118
elif isinstance(numerator, str):
73119
# Handle construction from strings.
74120
m = _RATIONAL_FORMAT.match(numerator)

Lib/test/test_fractions.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
F = fractions.Fraction
1313
gcd = fractions.gcd
1414

15+
# decorator for skipping tests on non-IEEE 754 platforms
16+
requires_IEEE_754 = unittest.skipUnless(
17+
float.__getformat__("double").startswith("IEEE"),
18+
"test requires IEEE 754 doubles")
19+
1520
class DummyFloat(object):
1621
"""Dummy float class for testing comparisons with Fractions"""
1722

@@ -130,13 +135,33 @@ def testInit(self):
130135

131136
self.assertRaisesMessage(ZeroDivisionError, "Fraction(12, 0)",
132137
F, 12, 0)
133-
self.assertRaises(TypeError, F, 1.5)
134138
self.assertRaises(TypeError, F, 1.5 + 3j)
135139

136140
self.assertRaises(TypeError, F, "3/2", 3)
137141
self.assertRaises(TypeError, F, 3, 0j)
138142
self.assertRaises(TypeError, F, 3, 1j)
139143

144+
@requires_IEEE_754
145+
def testInitFromFloat(self):
146+
self.assertEquals((5, 2), _components(F(2.5)))
147+
self.assertEquals((0, 1), _components(F(-0.0)))
148+
self.assertEquals((3602879701896397, 36028797018963968),
149+
_components(F(0.1)))
150+
self.assertRaises(TypeError, F, float('nan'))
151+
self.assertRaises(TypeError, F, float('inf'))
152+
self.assertRaises(TypeError, F, float('-inf'))
153+
154+
def testInitFromDecimal(self):
155+
self.assertEquals((11, 10),
156+
_components(F(Decimal('1.1'))))
157+
self.assertEquals((7, 200),
158+
_components(F(Decimal('3.5e-2'))))
159+
self.assertEquals((0, 1),
160+
_components(F(Decimal('.000e20'))))
161+
self.assertRaises(TypeError, F, Decimal('nan'))
162+
self.assertRaises(TypeError, F, Decimal('snan'))
163+
self.assertRaises(TypeError, F, Decimal('inf'))
164+
self.assertRaises(TypeError, F, Decimal('-inf'))
140165

141166
def testFromString(self):
142167
self.assertEquals((5, 1), _components(F("5")))

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,9 @@ C-API
301301
Library
302302
-------
303303

304+
- Issue #8294: The Fraction constructor now accepts Decimal and float
305+
instances directly.
306+
304307
- Issue #7279: Comparisons involving a Decimal signaling NaN now
305308
signal InvalidOperation instead of returning False. (Comparisons
306309
involving a quiet NaN are unchanged.) Also, Decimal quiet NaNs

0 commit comments

Comments
 (0)