summaryrefslogtreecommitdiffstats
path: root/Doc/library/fractions.rst
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2024-07-19 05:06:53 (GMT)
committerGitHub <noreply@github.com>2024-07-19 05:06:53 (GMT)
commitc8d2630995fc234f8276e35643a4a43e62224510 (patch)
tree359d25600fe9da975fdc9ee45dbba022bb7c5978 /Doc/library/fractions.rst
parenteaf094c09b5b1c33435c60ef49b1cec78c32573c (diff)
downloadcpython-c8d2630995fc234f8276e35643a4a43e62224510.zip
cpython-c8d2630995fc234f8276e35643a4a43e62224510.tar.gz
cpython-c8d2630995fc234f8276e35643a4a43e62224510.tar.bz2
gh-82017: Support as_integer_ratio() in the Fraction constructor (GH-120271)
Any objects that have the as_integer_ratio() method (e.g. numpy.float128) can now be converted to a fraction.
Diffstat (limited to 'Doc/library/fractions.rst')
-rw-r--r--Doc/library/fractions.rst27
1 files changed, 18 insertions, 9 deletions
diff --git a/Doc/library/fractions.rst b/Doc/library/fractions.rst
index 552d603..410b176 100644
--- a/Doc/library/fractions.rst
+++ b/Doc/library/fractions.rst
@@ -17,25 +17,30 @@ The :mod:`fractions` module provides support for rational number arithmetic.
A Fraction instance can be constructed from a pair of integers, from
another rational number, or from a string.
+.. index:: single: as_integer_ratio()
+
.. class:: Fraction(numerator=0, denominator=1)
- Fraction(other_fraction)
- Fraction(float)
- Fraction(decimal)
+ Fraction(number)
Fraction(string)
The first version requires that *numerator* and *denominator* are instances
of :class:`numbers.Rational` and returns a new :class:`Fraction` instance
with value ``numerator/denominator``. If *denominator* is ``0``, it
- raises a :exc:`ZeroDivisionError`. The second version requires that
- *other_fraction* is an instance of :class:`numbers.Rational` and returns a
- :class:`Fraction` instance with the same value. The next two versions accept
- either a :class:`float` or a :class:`decimal.Decimal` instance, and return a
- :class:`Fraction` instance with exactly the same value. Note that due to the
+ raises a :exc:`ZeroDivisionError`.
+
+ The second version requires that *number* is an instance of
+ :class:`numbers.Rational` or has the :meth:`!as_integer_ratio` method
+ (this includes :class:`float` and :class:`decimal.Decimal`).
+ It returns a :class:`Fraction` instance with exactly the same value.
+ Assumed, that the :meth:`!as_integer_ratio` method returns a pair
+ of coprime integers and last one is positive.
+ Note that due to the
usual issues with binary floating-point (see :ref:`tut-fp-issues`), the
argument to ``Fraction(1.1)`` is not exactly equal to 11/10, and so
``Fraction(1.1)`` does *not* return ``Fraction(11, 10)`` as one might expect.
(But see the documentation for the :meth:`limit_denominator` method below.)
- The last version of the constructor expects a string or unicode instance.
+
+ The last version of the constructor expects a string.
The usual form for this instance is::
[sign] numerator ['/' denominator]
@@ -110,6 +115,10 @@ another rational number, or from a string.
Formatting of :class:`Fraction` instances without a presentation type
now supports fill, alignment, sign handling, minimum width and grouping.
+ .. versionchanged:: 3.14
+ The :class:`Fraction` constructor now accepts any objects with the
+ :meth:`!as_integer_ratio` method.
+
.. attribute:: numerator
Numerator of the Fraction in lowest term.