@@ -17,25 +17,30 @@ The :mod:`fractions` module provides support for rational number arithmetic.
17
17
A Fraction instance can be constructed from a pair of integers, from
18
18
another rational number, or from a string.
19
19
20
+ .. index :: single: as_integer_ratio()
21
+
20
22
.. class :: Fraction(numerator=0, denominator=1)
21
- Fraction(other_fraction)
22
- Fraction(float)
23
- Fraction(decimal)
23
+ Fraction(number)
24
24
Fraction(string)
25
25
26
26
The first version requires that *numerator * and *denominator * are instances
27
27
of :class: `numbers.Rational ` and returns a new :class: `Fraction ` instance
28
28
with value ``numerator/denominator ``. If *denominator * is ``0 ``, it
29
- raises a :exc: `ZeroDivisionError `. The second version requires that
30
- *other_fraction * is an instance of :class: `numbers.Rational ` and returns a
31
- :class: `Fraction ` instance with the same value. The next two versions accept
32
- either a :class: `float ` or a :class: `decimal.Decimal ` instance, and return a
33
- :class: `Fraction ` instance with exactly the same value. Note that due to the
29
+ raises a :exc: `ZeroDivisionError `.
30
+
31
+ The second version requires that *number * is an instance of
32
+ :class: `numbers.Rational ` or has the :meth: `!as_integer_ratio ` method
33
+ (this includes :class: `float ` and :class: `decimal.Decimal `).
34
+ It returns a :class: `Fraction ` instance with exactly the same value.
35
+ Assumed, that the :meth: `!as_integer_ratio ` method returns a pair
36
+ of coprime integers and last one is positive.
37
+ Note that due to the
34
38
usual issues with binary floating-point (see :ref: `tut-fp-issues `), the
35
39
argument to ``Fraction(1.1) `` is not exactly equal to 11/10, and so
36
40
``Fraction(1.1) `` does *not * return ``Fraction(11, 10) `` as one might expect.
37
41
(But see the documentation for the :meth: `limit_denominator ` method below.)
38
- The last version of the constructor expects a string or unicode instance.
42
+
43
+ The last version of the constructor expects a string.
39
44
The usual form for this instance is::
40
45
41
46
[sign] numerator ['/' denominator]
@@ -110,6 +115,10 @@ another rational number, or from a string.
110
115
Formatting of :class: `Fraction ` instances without a presentation type
111
116
now supports fill, alignment, sign handling, minimum width and grouping.
112
117
118
+ .. versionchanged :: 3.14
119
+ The :class: `Fraction ` constructor now accepts any objects with the
120
+ :meth: `!as_integer_ratio ` method.
121
+
113
122
.. attribute :: numerator
114
123
115
124
Numerator of the Fraction in lowest term.
0 commit comments