@@ -15,17 +15,24 @@ another rational number, or from a string.
15
15
16
16
.. class :: Fraction(numerator=0, denominator=1)
17
17
Fraction(other_fraction)
18
+ Fraction(float)
19
+ Fraction(decimal)
18
20
Fraction(string)
19
21
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::
29
36
30
37
[sign] numerator ['/' denominator]
31
38
@@ -55,6 +62,13 @@ another rational number, or from a string.
55
62
Fraction(-1, 8)
56
63
>>> Fraction('7e-6')
57
64
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)
58
72
59
73
60
74
The :class: `Fraction ` class inherits from the abstract base class
@@ -63,19 +77,30 @@ another rational number, or from a string.
63
77
and should be treated as immutable. In addition,
64
78
:class: `Fraction ` has the following methods:
65
79
80
+ .. versionchanged :: 3.2
81
+ The :class: `Fraction ` constructor now accepts :class: `float ` and
82
+ :class: `decimal.Decimal ` instances.
83
+
66
84
67
85
.. method :: from_float(flt)
68
86
69
87
This class method constructs a :class: `Fraction ` representing the exact
70
88
value of *flt *, which must be a :class: `float `. Beware that
71
89
``Fraction.from_float(0.3) `` is not the same value as ``Fraction(3, 10) ``
72
90
91
+ .. note :: From Python 3.2 onwards, you can also construct a
92
+ :class: `Fraction ` instance directly from a :class: `float `.
93
+
73
94
74
95
.. method :: from_decimal(dec)
75
96
76
97
This class method constructs a :class: `Fraction ` representing the exact
77
98
value of *dec *, which must be a :class: `decimal.Decimal ` instance.
78
99
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
+
79
104
80
105
.. method :: limit_denominator(max_denominator=1000000)
81
106
@@ -90,10 +115,12 @@ another rational number, or from a string.
90
115
or for recovering a rational number that's represented as a float:
91
116
92
117
>>> from math import pi, cos
93
- >>> Fraction.from_float (cos(pi/ 3 ))
118
+ >>> Fraction(cos(pi/ 3 ))
94
119
Fraction(4503599627370497, 9007199254740992)
95
- >>> Fraction.from_float (cos(pi/ 3 )).limit_denominator()
120
+ >>> Fraction(cos(pi/ 3 )).limit_denominator()
96
121
Fraction(1, 2)
122
+ >>> Fraction(1.1 ).limit_denominator()
123
+ Fraction(11, 10)
97
124
98
125
99
126
.. method :: __floor__()
0 commit comments