blob: fba199bf787731f38b9549815ad97850d4f2ecd6 [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.
6.. moduleauthor:: Jeffrey Yasskin <jyasskin at gmail.com>
7.. sectionauthor:: Jeffrey Yasskin <jyasskin at gmail.com>
Guido van Rossum7736b5b2008-01-15 21:44:53 +00008
Raymond Hettinger469271d2011-01-27 20:38:46 +00009**Source code:** :source:`Lib/fractions.py`
10
11--------------
Guido van Rossum7736b5b2008-01-15 21:44:53 +000012
Mark Dickinsonce279a72008-06-27 17:01:17 +000013The :mod:`fractions` module provides support for rational number arithmetic.
Guido van Rossum7736b5b2008-01-15 21:44:53 +000014
15
Mark Dickinsonce279a72008-06-27 17:01:17 +000016A Fraction instance can be constructed from a pair of integers, from
17another rational number, or from a string.
18
Christian Heimes3feef612008-02-11 06:19:17 +000019.. class:: Fraction(numerator=0, denominator=1)
20 Fraction(other_fraction)
Mark Dickinson98127c32010-04-03 11:18:52 +000021 Fraction(float)
22 Fraction(decimal)
Christian Heimes3feef612008-02-11 06:19:17 +000023 Fraction(string)
Guido van Rossum7736b5b2008-01-15 21:44:53 +000024
Mark Dickinson98127c32010-04-03 11:18:52 +000025 The first version requires that *numerator* and *denominator* are instances
26 of :class:`numbers.Rational` and returns a new :class:`Fraction` instance
27 with value ``numerator/denominator``. If *denominator* is :const:`0`, it
28 raises a :exc:`ZeroDivisionError`. The second version requires that
29 *other_fraction* is an instance of :class:`numbers.Rational` and returns a
30 :class:`Fraction` instance with the same value. The next two versions accept
31 either a :class:`float` or a :class:`decimal.Decimal` instance, and return a
32 :class:`Fraction` instance with exactly the same value. Note that due to the
33 usual issues with binary floating-point (see :ref:`tut-fp-issues`), the
34 argument to ``Fraction(1.1)`` is not exactly equal to 11/10, and so
35 ``Fraction(1.1)`` does *not* return ``Fraction(11, 10)`` as one might expect.
36 (But see the documentation for the :meth:`limit_denominator` method below.)
37 The last version of the constructor expects a string or unicode instance.
38 The usual form for this instance is::
Guido van Rossum7736b5b2008-01-15 21:44:53 +000039
Mark Dickinsonce279a72008-06-27 17:01:17 +000040 [sign] numerator ['/' denominator]
41
42 where the optional ``sign`` may be either '+' or '-' and
43 ``numerator`` and ``denominator`` (if present) are strings of
Mark Dickinsoncf63f2f2009-04-22 17:50:21 +000044 decimal digits. In addition, any string that represents a finite
45 value and is accepted by the :class:`float` constructor is also
46 accepted by the :class:`Fraction` constructor. In either form the
47 input string may also have leading and/or trailing whitespace.
48 Here are some examples::
Mark Dickinsonce279a72008-06-27 17:01:17 +000049
50 >>> from fractions import Fraction
51 >>> Fraction(16, -10)
52 Fraction(-8, 5)
53 >>> Fraction(123)
54 Fraction(123, 1)
55 >>> Fraction()
56 Fraction(0, 1)
57 >>> Fraction('3/7')
58 Fraction(3, 7)
Mark Dickinsonce279a72008-06-27 17:01:17 +000059 >>> Fraction(' -3/7 ')
60 Fraction(-3, 7)
61 >>> Fraction('1.414213 \t\n')
62 Fraction(1414213, 1000000)
63 >>> Fraction('-.125')
64 Fraction(-1, 8)
Mark Dickinsoncf63f2f2009-04-22 17:50:21 +000065 >>> Fraction('7e-6')
66 Fraction(7, 1000000)
Mark Dickinson98127c32010-04-03 11:18:52 +000067 >>> Fraction(2.25)
68 Fraction(9, 4)
69 >>> Fraction(1.1)
70 Fraction(2476979795053773, 2251799813685248)
71 >>> from decimal import Decimal
72 >>> Fraction(Decimal('1.1'))
73 Fraction(11, 10)
Mark Dickinsonce279a72008-06-27 17:01:17 +000074
75
76 The :class:`Fraction` class inherits from the abstract base class
77 :class:`numbers.Rational`, and implements all of the methods and
78 operations from that class. :class:`Fraction` instances are hashable,
79 and should be treated as immutable. In addition,
Senthil Kumaranb505a6a2013-09-09 19:57:37 -070080 :class:`Fraction` has the following properties and methods:
Guido van Rossum7736b5b2008-01-15 21:44:53 +000081
Mark Dickinson98127c32010-04-03 11:18:52 +000082 .. versionchanged:: 3.2
83 The :class:`Fraction` constructor now accepts :class:`float` and
84 :class:`decimal.Decimal` instances.
85
Guido van Rossum7736b5b2008-01-15 21:44:53 +000086
Senthil Kumaranb505a6a2013-09-09 19:57:37 -070087 .. attribute:: numerator
88
89 Numerator of the Fraction in lowest term.
90
91 .. attribute:: denominator
92
93 Denominator of the Fraction in lowest term.
94
95
Benjamin Petersone41251e2008-04-25 01:59:09 +000096 .. method:: from_float(flt)
Guido van Rossum7736b5b2008-01-15 21:44:53 +000097
Mark Dickinsonce279a72008-06-27 17:01:17 +000098 This class method constructs a :class:`Fraction` representing the exact
Benjamin Petersone41251e2008-04-25 01:59:09 +000099 value of *flt*, which must be a :class:`float`. Beware that
Mark Dickinsonce279a72008-06-27 17:01:17 +0000100 ``Fraction.from_float(0.3)`` is not the same value as ``Fraction(3, 10)``
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000101
Mark Dickinson98127c32010-04-03 11:18:52 +0000102 .. note:: From Python 3.2 onwards, you can also construct a
103 :class:`Fraction` instance directly from a :class:`float`.
104
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000105
Benjamin Petersone41251e2008-04-25 01:59:09 +0000106 .. method:: from_decimal(dec)
Christian Heimes587c2bf2008-01-19 16:21:02 +0000107
Mark Dickinsonce279a72008-06-27 17:01:17 +0000108 This class method constructs a :class:`Fraction` representing the exact
Mark Dickinson268bf4a2008-06-24 15:32:27 +0000109 value of *dec*, which must be a :class:`decimal.Decimal` instance.
Christian Heimes587c2bf2008-01-19 16:21:02 +0000110
Mark Dickinson98127c32010-04-03 11:18:52 +0000111 .. note:: From Python 3.2 onwards, you can also construct a
112 :class:`Fraction` instance directly from a :class:`decimal.Decimal`
113 instance.
114
Christian Heimes587c2bf2008-01-19 16:21:02 +0000115
Benjamin Petersone41251e2008-04-25 01:59:09 +0000116 .. method:: limit_denominator(max_denominator=1000000)
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000117
Benjamin Petersone41251e2008-04-25 01:59:09 +0000118 Finds and returns the closest :class:`Fraction` to ``self`` that has
119 denominator at most max_denominator. This method is useful for finding
120 rational approximations to a given floating-point number:
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000121
Benjamin Petersone41251e2008-04-25 01:59:09 +0000122 >>> from fractions import Fraction
123 >>> Fraction('3.1415926535897932').limit_denominator(1000)
Mark Dickinson79edbd52008-06-24 14:26:24 +0000124 Fraction(355, 113)
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000125
Benjamin Petersone41251e2008-04-25 01:59:09 +0000126 or for recovering a rational number that's represented as a float:
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000127
Benjamin Petersone41251e2008-04-25 01:59:09 +0000128 >>> from math import pi, cos
Mark Dickinson98127c32010-04-03 11:18:52 +0000129 >>> Fraction(cos(pi/3))
Mark Dickinson79edbd52008-06-24 14:26:24 +0000130 Fraction(4503599627370497, 9007199254740992)
Mark Dickinson98127c32010-04-03 11:18:52 +0000131 >>> Fraction(cos(pi/3)).limit_denominator()
Mark Dickinson79edbd52008-06-24 14:26:24 +0000132 Fraction(1, 2)
Mark Dickinson98127c32010-04-03 11:18:52 +0000133 >>> Fraction(1.1).limit_denominator()
134 Fraction(11, 10)
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000135
136
Benjamin Petersone41251e2008-04-25 01:59:09 +0000137 .. method:: __floor__()
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000138
Mark Dickinson268bf4a2008-06-24 15:32:27 +0000139 Returns the greatest :class:`int` ``<= self``. This method can
140 also be accessed through the :func:`math.floor` function:
141
142 >>> from math import floor
143 >>> floor(Fraction(355, 113))
144 3
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000145
146
Benjamin Petersone41251e2008-04-25 01:59:09 +0000147 .. method:: __ceil__()
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000148
Mark Dickinson268bf4a2008-06-24 15:32:27 +0000149 Returns the least :class:`int` ``>= self``. This method can
150 also be accessed through the :func:`math.ceil` function.
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000151
152
Benjamin Petersone41251e2008-04-25 01:59:09 +0000153 .. method:: __round__()
154 __round__(ndigits)
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000155
Mark Dickinson268bf4a2008-06-24 15:32:27 +0000156 The first version returns the nearest :class:`int` to ``self``,
157 rounding half to even. The second version rounds ``self`` to the
158 nearest multiple of ``Fraction(1, 10**ndigits)`` (logically, if
159 ``ndigits`` is negative), again rounding half toward even. This
160 method can also be accessed through the :func:`round` function.
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000161
162
Mark Dickinsonce279a72008-06-27 17:01:17 +0000163.. function:: gcd(a, b)
164
Georg Brandl36ab1ef2009-01-03 21:17:04 +0000165 Return the greatest common divisor of the integers *a* and *b*. If either
166 *a* or *b* is nonzero, then the absolute value of ``gcd(a, b)`` is the
167 largest integer that divides both *a* and *b*. ``gcd(a,b)`` has the same
168 sign as *b* if *b* is nonzero; otherwise it takes the sign of *a*. ``gcd(0,
169 0)`` returns ``0``.
Mark Dickinsonce279a72008-06-27 17:01:17 +0000170
171
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000172.. seealso::
173
174 Module :mod:`numbers`
175 The abstract base classes making up the numeric tower.