blob: 58e7126b0bf212fddadfc125cee7dae05251752f [file] [log] [blame]
Christian Heimes3feef612008-02-11 06:19:17 +00001:mod:`fractions` --- Rational numbers
Christian Heimes0bd4e112008-02-12 22:59:25 +00002=====================================
Guido van Rossum7736b5b2008-01-15 21:44:53 +00003
Christian Heimes3feef612008-02-11 06:19:17 +00004.. module:: fractions
Guido van Rossum7736b5b2008-01-15 21:44:53 +00005 :synopsis: Rational numbers.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Guido van Rossum7736b5b2008-01-15 21:44:53 +00007.. moduleauthor:: Jeffrey Yasskin <jyasskin at gmail.com>
8.. sectionauthor:: Jeffrey Yasskin <jyasskin at gmail.com>
Guido van Rossum7736b5b2008-01-15 21:44:53 +00009
Raymond Hettinger469271d2011-01-27 20:38:46 +000010**Source code:** :source:`Lib/fractions.py`
11
12--------------
Guido van Rossum7736b5b2008-01-15 21:44:53 +000013
Mark Dickinsonce279a72008-06-27 17:01:17 +000014The :mod:`fractions` module provides support for rational number arithmetic.
Guido van Rossum7736b5b2008-01-15 21:44:53 +000015
16
Mark Dickinsonce279a72008-06-27 17:01:17 +000017A Fraction instance can be constructed from a pair of integers, from
18another rational number, or from a string.
19
Christian Heimes3feef612008-02-11 06:19:17 +000020.. class:: Fraction(numerator=0, denominator=1)
21 Fraction(other_fraction)
Mark Dickinson98127c32010-04-03 11:18:52 +000022 Fraction(float)
23 Fraction(decimal)
Christian Heimes3feef612008-02-11 06:19:17 +000024 Fraction(string)
Guido van Rossum7736b5b2008-01-15 21:44:53 +000025
Mark Dickinson98127c32010-04-03 11:18:52 +000026 The first version requires that *numerator* and *denominator* are instances
27 of :class:`numbers.Rational` and returns a new :class:`Fraction` instance
28 with value ``numerator/denominator``. If *denominator* is :const:`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
34 usual issues with binary floating-point (see :ref:`tut-fp-issues`), the
35 argument to ``Fraction(1.1)`` is not exactly equal to 11/10, and so
36 ``Fraction(1.1)`` does *not* return ``Fraction(11, 10)`` as one might expect.
37 (But see the documentation for the :meth:`limit_denominator` method below.)
38 The last version of the constructor expects a string or unicode instance.
39 The usual form for this instance is::
Guido van Rossum7736b5b2008-01-15 21:44:53 +000040
Mark Dickinsonce279a72008-06-27 17:01:17 +000041 [sign] numerator ['/' denominator]
42
43 where the optional ``sign`` may be either '+' or '-' and
44 ``numerator`` and ``denominator`` (if present) are strings of
Mark Dickinsoncf63f2f2009-04-22 17:50:21 +000045 decimal digits. In addition, any string that represents a finite
46 value and is accepted by the :class:`float` constructor is also
47 accepted by the :class:`Fraction` constructor. In either form the
48 input string may also have leading and/or trailing whitespace.
49 Here are some examples::
Mark Dickinsonce279a72008-06-27 17:01:17 +000050
51 >>> from fractions import Fraction
52 >>> Fraction(16, -10)
53 Fraction(-8, 5)
54 >>> Fraction(123)
55 Fraction(123, 1)
56 >>> Fraction()
57 Fraction(0, 1)
58 >>> Fraction('3/7')
59 Fraction(3, 7)
Mark Dickinsonce279a72008-06-27 17:01:17 +000060 >>> Fraction(' -3/7 ')
61 Fraction(-3, 7)
62 >>> Fraction('1.414213 \t\n')
63 Fraction(1414213, 1000000)
64 >>> Fraction('-.125')
65 Fraction(-1, 8)
Mark Dickinsoncf63f2f2009-04-22 17:50:21 +000066 >>> Fraction('7e-6')
67 Fraction(7, 1000000)
Mark Dickinson98127c32010-04-03 11:18:52 +000068 >>> Fraction(2.25)
69 Fraction(9, 4)
70 >>> Fraction(1.1)
71 Fraction(2476979795053773, 2251799813685248)
72 >>> from decimal import Decimal
73 >>> Fraction(Decimal('1.1'))
74 Fraction(11, 10)
Mark Dickinsonce279a72008-06-27 17:01:17 +000075
76
77 The :class:`Fraction` class inherits from the abstract base class
78 :class:`numbers.Rational`, and implements all of the methods and
79 operations from that class. :class:`Fraction` instances are hashable,
80 and should be treated as immutable. In addition,
Senthil Kumaranb505a6a2013-09-09 19:57:37 -070081 :class:`Fraction` has the following properties and methods:
Guido van Rossum7736b5b2008-01-15 21:44:53 +000082
Mark Dickinson98127c32010-04-03 11:18:52 +000083 .. versionchanged:: 3.2
84 The :class:`Fraction` constructor now accepts :class:`float` and
85 :class:`decimal.Decimal` instances.
86
Guido van Rossum7736b5b2008-01-15 21:44:53 +000087
Senthil Kumaranb505a6a2013-09-09 19:57:37 -070088 .. attribute:: numerator
89
90 Numerator of the Fraction in lowest term.
91
92 .. attribute:: denominator
93
94 Denominator of the Fraction in lowest term.
95
96
Raymond Hettingerf03b4c82019-08-11 14:40:59 -070097 .. method:: as_integer_ratio()
98
99 Return a tuple of two integers, whose ratio is equal
100 to the Fraction and with a positive denominator.
101
102 .. versionadded:: 3.8
103
Benjamin Petersone41251e2008-04-25 01:59:09 +0000104 .. method:: from_float(flt)
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000105
Mark Dickinsonce279a72008-06-27 17:01:17 +0000106 This class method constructs a :class:`Fraction` representing the exact
Benjamin Petersone41251e2008-04-25 01:59:09 +0000107 value of *flt*, which must be a :class:`float`. Beware that
Martin Panterd21e0b52015-10-10 10:36:22 +0000108 ``Fraction.from_float(0.3)`` is not the same value as ``Fraction(3, 10)``.
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000109
Éric Araujofa5e6e42014-03-12 19:51:00 -0400110 .. note::
111
112 From Python 3.2 onwards, you can also construct a
Mark Dickinson98127c32010-04-03 11:18:52 +0000113 :class:`Fraction` instance directly from a :class:`float`.
114
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000115
Benjamin Petersone41251e2008-04-25 01:59:09 +0000116 .. method:: from_decimal(dec)
Christian Heimes587c2bf2008-01-19 16:21:02 +0000117
Mark Dickinsonce279a72008-06-27 17:01:17 +0000118 This class method constructs a :class:`Fraction` representing the exact
Mark Dickinson268bf4a2008-06-24 15:32:27 +0000119 value of *dec*, which must be a :class:`decimal.Decimal` instance.
Christian Heimes587c2bf2008-01-19 16:21:02 +0000120
Éric Araujofa5e6e42014-03-12 19:51:00 -0400121 .. note::
122
123 From Python 3.2 onwards, you can also construct a
Mark Dickinson98127c32010-04-03 11:18:52 +0000124 :class:`Fraction` instance directly from a :class:`decimal.Decimal`
125 instance.
126
Christian Heimes587c2bf2008-01-19 16:21:02 +0000127
Benjamin Petersone41251e2008-04-25 01:59:09 +0000128 .. method:: limit_denominator(max_denominator=1000000)
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000129
Benjamin Petersone41251e2008-04-25 01:59:09 +0000130 Finds and returns the closest :class:`Fraction` to ``self`` that has
131 denominator at most max_denominator. This method is useful for finding
132 rational approximations to a given floating-point number:
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000133
Benjamin Petersone41251e2008-04-25 01:59:09 +0000134 >>> from fractions import Fraction
135 >>> Fraction('3.1415926535897932').limit_denominator(1000)
Mark Dickinson79edbd52008-06-24 14:26:24 +0000136 Fraction(355, 113)
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000137
Benjamin Petersone41251e2008-04-25 01:59:09 +0000138 or for recovering a rational number that's represented as a float:
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000139
Benjamin Petersone41251e2008-04-25 01:59:09 +0000140 >>> from math import pi, cos
Mark Dickinson98127c32010-04-03 11:18:52 +0000141 >>> Fraction(cos(pi/3))
Mark Dickinson79edbd52008-06-24 14:26:24 +0000142 Fraction(4503599627370497, 9007199254740992)
Mark Dickinson98127c32010-04-03 11:18:52 +0000143 >>> Fraction(cos(pi/3)).limit_denominator()
Mark Dickinson79edbd52008-06-24 14:26:24 +0000144 Fraction(1, 2)
Mark Dickinson98127c32010-04-03 11:18:52 +0000145 >>> Fraction(1.1).limit_denominator()
146 Fraction(11, 10)
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000147
148
Benjamin Petersone41251e2008-04-25 01:59:09 +0000149 .. method:: __floor__()
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000150
Mark Dickinson268bf4a2008-06-24 15:32:27 +0000151 Returns the greatest :class:`int` ``<= self``. This method can
152 also be accessed through the :func:`math.floor` function:
153
154 >>> from math import floor
155 >>> floor(Fraction(355, 113))
156 3
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000157
158
Benjamin Petersone41251e2008-04-25 01:59:09 +0000159 .. method:: __ceil__()
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000160
Mark Dickinson268bf4a2008-06-24 15:32:27 +0000161 Returns the least :class:`int` ``>= self``. This method can
162 also be accessed through the :func:`math.ceil` function.
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000163
164
Benjamin Petersone41251e2008-04-25 01:59:09 +0000165 .. method:: __round__()
166 __round__(ndigits)
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000167
Mark Dickinson268bf4a2008-06-24 15:32:27 +0000168 The first version returns the nearest :class:`int` to ``self``,
169 rounding half to even. The second version rounds ``self`` to the
170 nearest multiple of ``Fraction(1, 10**ndigits)`` (logically, if
171 ``ndigits`` is negative), again rounding half toward even. This
172 method can also be accessed through the :func:`round` function.
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000173
174
Mark Dickinsonce279a72008-06-27 17:01:17 +0000175.. function:: gcd(a, b)
176
Georg Brandl36ab1ef2009-01-03 21:17:04 +0000177 Return the greatest common divisor of the integers *a* and *b*. If either
178 *a* or *b* is nonzero, then the absolute value of ``gcd(a, b)`` is the
179 largest integer that divides both *a* and *b*. ``gcd(a,b)`` has the same
180 sign as *b* if *b* is nonzero; otherwise it takes the sign of *a*. ``gcd(0,
181 0)`` returns ``0``.
Mark Dickinsonce279a72008-06-27 17:01:17 +0000182
Serhiy Storchaka48e47aa2015-05-13 00:19:51 +0300183 .. deprecated:: 3.5
184 Use :func:`math.gcd` instead.
185
Mark Dickinsonce279a72008-06-27 17:01:17 +0000186
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000187.. seealso::
188
189 Module :mod:`numbers`
190 The abstract base classes making up the numeric tower.