blob: 1ca9a9c46d4a5d3e2f3c2f1be5f2d3b87108a5a4 [file] [log] [blame]
Jeffrey Yasskind7b00332008-01-15 07:46:24 +00001
Mark Dickinsond058cd22008-02-10 21:29:51 +00002:mod:`fractions` --- Rational numbers
Raymond Hettinger2ddbd802008-02-11 23:34:56 +00003=====================================
Jeffrey Yasskind7b00332008-01-15 07:46:24 +00004
Mark Dickinsond058cd22008-02-10 21:29:51 +00005.. module:: fractions
Jeffrey Yasskind7b00332008-01-15 07:46:24 +00006 :synopsis: Rational numbers.
7.. moduleauthor:: Jeffrey Yasskin <jyasskin at gmail.com>
8.. sectionauthor:: Jeffrey Yasskin <jyasskin at gmail.com>
9.. versionadded:: 2.6
10
11
Mark Dickinsondf90ee62008-06-27 16:49:27 +000012The :mod:`fractions` module provides support for rational number arithmetic.
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000013
14
Mark Dickinsondf90ee62008-06-27 16:49:27 +000015A Fraction instance can be constructed from a pair of integers, from
16another rational number, or from a string.
17
Mark Dickinsond058cd22008-02-10 21:29:51 +000018.. class:: Fraction(numerator=0, denominator=1)
19 Fraction(other_fraction)
Mark Dickinson7c63eee2010-04-02 22:27:36 +000020 Fraction(float)
21 Fraction(decimal)
Mark Dickinsond058cd22008-02-10 21:29:51 +000022 Fraction(string)
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000023
Mark Dickinson7c63eee2010-04-02 22:27:36 +000024 The first version requires that *numerator* and *denominator* are instances
25 of :class:`numbers.Rational` and returns a new :class:`Fraction` instance
26 with value ``numerator/denominator``. If *denominator* is :const:`0`, it
27 raises a :exc:`ZeroDivisionError`. The second version requires that
28 *other_fraction* is an instance of :class:`numbers.Rational` and returns a
29 :class:`Fraction` instance with the same value. The next two versions accept
30 either a :class:`float` or a :class:`decimal.Decimal` instance, and return a
31 :class:`Fraction` instance with exactly the same value. Note that due to the
32 usual issues with binary floating-point (see :ref:`tut-fp-issues`), the
33 argument to ``Fraction(1.1)`` is not exactly equal to 11/10, and so
34 ``Fraction(1.1)`` does *not* return ``Fraction(11, 10)`` as one might expect.
35 (But see the documentation for the :meth:`limit_denominator` method below.)
36 The last version of the constructor expects a string or unicode instance.
37 The usual form for this instance is::
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000038
Mark Dickinsondf90ee62008-06-27 16:49:27 +000039 [sign] numerator ['/' denominator]
40
41 where the optional ``sign`` may be either '+' or '-' and
42 ``numerator`` and ``denominator`` (if present) are strings of
Mark Dickinson8100bd82009-04-22 18:15:25 +000043 decimal digits. In addition, any string that represents a finite
44 value and is accepted by the :class:`float` constructor is also
45 accepted by the :class:`Fraction` constructor. In either form the
46 input string may also have leading and/or trailing whitespace.
47 Here are some examples::
Mark Dickinsondf90ee62008-06-27 16:49:27 +000048
49 >>> from fractions import Fraction
50 >>> Fraction(16, -10)
51 Fraction(-8, 5)
52 >>> Fraction(123)
53 Fraction(123, 1)
54 >>> Fraction()
55 Fraction(0, 1)
56 >>> Fraction('3/7')
57 Fraction(3, 7)
58 [40794 refs]
59 >>> 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 Dickinson8100bd82009-04-22 18:15:25 +000065 >>> Fraction('7e-6')
66 Fraction(7, 1000000)
Mark Dickinson7c63eee2010-04-02 22:27:36 +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 Dickinsondf90ee62008-06-27 16:49:27 +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,
80 :class:`Fraction` has the following methods:
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000081
Mark Dickinson7c63eee2010-04-02 22:27:36 +000082 .. versionchanged:: 2.7
83 The :class:`Fraction` constructor now accepts :class:`float` and
84 :class:`decimal.Decimal` instances.
85
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000086
Benjamin Petersonc7b05922008-04-25 01:29:10 +000087 .. method:: from_float(flt)
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000088
Mark Dickinsondf90ee62008-06-27 16:49:27 +000089 This class method constructs a :class:`Fraction` representing the exact
Benjamin Petersonc7b05922008-04-25 01:29:10 +000090 value of *flt*, which must be a :class:`float`. Beware that
91 ``Fraction.from_float(0.3)`` is not the same value as ``Fraction(3, 10)``
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000092
Mark Dickinson7c63eee2010-04-02 22:27:36 +000093 .. note:: From Python 2.7 onwards, you can also construct a
94 :class:`Fraction` instance directly from a :class:`float`.
95
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000096
Benjamin Petersonc7b05922008-04-25 01:29:10 +000097 .. method:: from_decimal(dec)
Jeffrey Yasskin45169fb2008-01-19 09:56:06 +000098
Mark Dickinsondf90ee62008-06-27 16:49:27 +000099 This class method constructs a :class:`Fraction` representing the exact
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000100 value of *dec*, which must be a :class:`decimal.Decimal`.
Jeffrey Yasskin45169fb2008-01-19 09:56:06 +0000101
Mark Dickinson7c63eee2010-04-02 22:27:36 +0000102 .. note:: From Python 2.7 onwards, you can also construct a
103 :class:`Fraction` instance directly from a :class:`decimal.Decimal`
104 instance.
105
Jeffrey Yasskin45169fb2008-01-19 09:56:06 +0000106
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000107 .. method:: limit_denominator(max_denominator=1000000)
Mark Dickinsone1b82472008-02-12 21:31:59 +0000108
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000109 Finds and returns the closest :class:`Fraction` to ``self`` that has
110 denominator at most max_denominator. This method is useful for finding
111 rational approximations to a given floating-point number:
Mark Dickinsone1b82472008-02-12 21:31:59 +0000112
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000113 >>> from fractions import Fraction
114 >>> Fraction('3.1415926535897932').limit_denominator(1000)
Mark Dickinsondf90ee62008-06-27 16:49:27 +0000115 Fraction(355, 113)
Mark Dickinsone1b82472008-02-12 21:31:59 +0000116
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000117 or for recovering a rational number that's represented as a float:
Mark Dickinsone1b82472008-02-12 21:31:59 +0000118
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000119 >>> from math import pi, cos
Mark Dickinson7c63eee2010-04-02 22:27:36 +0000120 >>> Fraction(cos(pi/3))
Mark Dickinsondf90ee62008-06-27 16:49:27 +0000121 Fraction(4503599627370497, 9007199254740992)
Mark Dickinson7c63eee2010-04-02 22:27:36 +0000122 >>> Fraction(cos(pi/3)).limit_denominator()
Mark Dickinsondf90ee62008-06-27 16:49:27 +0000123 Fraction(1, 2)
Mark Dickinson7c63eee2010-04-02 22:27:36 +0000124 >>> Fraction(1.1).limit_denominator()
125 Fraction(11, 10)
Mark Dickinsone1b82472008-02-12 21:31:59 +0000126
127
Mark Dickinsondf90ee62008-06-27 16:49:27 +0000128.. function:: gcd(a, b)
Jeffrey Yasskind7b00332008-01-15 07:46:24 +0000129
Georg Brandle92818f2009-01-03 20:47:01 +0000130 Return the greatest common divisor of the integers *a* and *b*. If either
131 *a* or *b* is nonzero, then the absolute value of ``gcd(a, b)`` is the
132 largest integer that divides both *a* and *b*. ``gcd(a,b)`` has the same
133 sign as *b* if *b* is nonzero; otherwise it takes the sign of *a*. ``gcd(0,
134 0)`` returns ``0``.
Jeffrey Yasskind7b00332008-01-15 07:46:24 +0000135
136
137.. seealso::
138
139 Module :mod:`numbers`
140 The abstract base classes making up the numeric tower.