summaryrefslogtreecommitdiffstats
path: root/Lib/fractions.py
diff options
context:
space:
mode:
authorMark Dickinson <dickinsm@gmail.com>2010-04-02 22:27:36 (GMT)
committerMark Dickinson <dickinsm@gmail.com>2010-04-02 22:27:36 (GMT)
commit7c63eee4854ef4227ce7a79c4b153e75af6aab46 (patch)
treeae8c5d0c4b85fe0834ccac7eadbf06e23aefc699 /Lib/fractions.py
parent58c1e788067a79e89d6f5c34a5da312525bf322b (diff)
downloadcpython-7c63eee4854ef4227ce7a79c4b153e75af6aab46.zip
cpython-7c63eee4854ef4227ce7a79c4b153e75af6aab46.tar.gz
cpython-7c63eee4854ef4227ce7a79c4b153e75af6aab46.tar.bz2
Issue #8294: Allow float and Decimal arguments in Fraction constructor.
Diffstat (limited to 'Lib/fractions.py')
-rw-r--r--Lib/fractions.py62
1 files changed, 54 insertions, 8 deletions
diff --git a/Lib/fractions.py b/Lib/fractions.py
index 7a3e555..a0d86a4 100644
--- a/Lib/fractions.py
+++ b/Lib/fractions.py
@@ -4,6 +4,7 @@
"""Rational, infinite-precision, real numbers."""
from __future__ import division
+from decimal import Decimal
import math
import numbers
import operator
@@ -43,13 +44,21 @@ _RATIONAL_FORMAT = re.compile(r"""
class Fraction(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.
- Fractions 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)
"""
@@ -59,8 +68,32 @@ class Fraction(Rational):
def __new__(cls, numerator=0, denominator=None):
"""Constructs a Fraction.
- Takes a string like '3/2' or '1.5', another Fraction, 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)
@@ -71,6 +104,19 @@ class Fraction(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, basestring):
# Handle construction from strings.
m = _RATIONAL_FORMAT.match(numerator)