blob: 68a8ef62957e4ed6cecb0661053acbc313c9ddfd [file] [log] [blame]
Guido van Rossum7736b5b2008-01-15 21:44:53 +00001
Christian Heimes3feef612008-02-11 06:19:17 +00002:mod:`fractions` --- Rational numbers
Christian Heimes0bd4e112008-02-12 22:59:25 +00003=====================================
Guido van Rossum7736b5b2008-01-15 21:44:53 +00004
Christian Heimes3feef612008-02-11 06:19:17 +00005.. module:: fractions
Guido van Rossum7736b5b2008-01-15 21:44:53 +00006 :synopsis: Rational numbers.
7.. moduleauthor:: Jeffrey Yasskin <jyasskin at gmail.com>
8.. sectionauthor:: Jeffrey Yasskin <jyasskin at gmail.com>
Guido van Rossum7736b5b2008-01-15 21:44:53 +00009
10
Mark Dickinsonce279a72008-06-27 17:01:17 +000011The :mod:`fractions` module provides support for rational number arithmetic.
Guido van Rossum7736b5b2008-01-15 21:44:53 +000012
13
Mark Dickinsonce279a72008-06-27 17:01:17 +000014A Fraction instance can be constructed from a pair of integers, from
15another rational number, or from a string.
16
Christian Heimes3feef612008-02-11 06:19:17 +000017.. class:: Fraction(numerator=0, denominator=1)
18 Fraction(other_fraction)
19 Fraction(string)
Guido van Rossum7736b5b2008-01-15 21:44:53 +000020
21 The first version requires that *numerator* and *denominator* are
22 instances of :class:`numbers.Integral` and returns a new
Mark Dickinsonce279a72008-06-27 17:01:17 +000023 :class:`Fraction` instance with value ``numerator/denominator``. If
24 *denominator* is :const:`0`, it raises a
25 :exc:`ZeroDivisionError`. The second version requires that
26 *other_fraction* is an instance of :class:`numbers.Rational` and
27 returns an :class:`Fraction` instance with the same value. The
Georg Brandlb1590082008-12-04 18:18:16 +000028 last version of the constructor expects a string
Mark Dickinsonce279a72008-06-27 17:01:17 +000029 instance in one of two possible forms. The first form is::
Guido van Rossum7736b5b2008-01-15 21:44:53 +000030
Mark Dickinsonce279a72008-06-27 17:01:17 +000031 [sign] numerator ['/' denominator]
32
33 where the optional ``sign`` may be either '+' or '-' and
34 ``numerator`` and ``denominator`` (if present) are strings of
35 decimal digits. The second permitted form is that of a number
36 containing a decimal point::
37
38 [sign] integer '.' [fraction] | [sign] '.' fraction
39
40 where ``integer`` and ``fraction`` are strings of digits. In
41 either form the input string may also have leading and/or trailing
42 whitespace. Here are some examples::
43
44 >>> from fractions import Fraction
45 >>> Fraction(16, -10)
46 Fraction(-8, 5)
47 >>> Fraction(123)
48 Fraction(123, 1)
49 >>> Fraction()
50 Fraction(0, 1)
51 >>> Fraction('3/7')
52 Fraction(3, 7)
53 [40794 refs]
54 >>> Fraction(' -3/7 ')
55 Fraction(-3, 7)
56 >>> Fraction('1.414213 \t\n')
57 Fraction(1414213, 1000000)
58 >>> Fraction('-.125')
59 Fraction(-1, 8)
60
61
62 The :class:`Fraction` class inherits from the abstract base class
63 :class:`numbers.Rational`, and implements all of the methods and
64 operations from that class. :class:`Fraction` instances are hashable,
65 and should be treated as immutable. In addition,
66 :class:`Fraction` has the following methods:
Guido van Rossum7736b5b2008-01-15 21:44:53 +000067
68
Benjamin Petersone41251e2008-04-25 01:59:09 +000069 .. method:: from_float(flt)
Guido van Rossum7736b5b2008-01-15 21:44:53 +000070
Mark Dickinsonce279a72008-06-27 17:01:17 +000071 This class method constructs a :class:`Fraction` representing the exact
Benjamin Petersone41251e2008-04-25 01:59:09 +000072 value of *flt*, which must be a :class:`float`. Beware that
Mark Dickinsonce279a72008-06-27 17:01:17 +000073 ``Fraction.from_float(0.3)`` is not the same value as ``Fraction(3, 10)``
Guido van Rossum7736b5b2008-01-15 21:44:53 +000074
75
Benjamin Petersone41251e2008-04-25 01:59:09 +000076 .. method:: from_decimal(dec)
Christian Heimes587c2bf2008-01-19 16:21:02 +000077
Mark Dickinsonce279a72008-06-27 17:01:17 +000078 This class method constructs a :class:`Fraction` representing the exact
Mark Dickinson268bf4a2008-06-24 15:32:27 +000079 value of *dec*, which must be a :class:`decimal.Decimal` instance.
Christian Heimes587c2bf2008-01-19 16:21:02 +000080
81
Benjamin Petersone41251e2008-04-25 01:59:09 +000082 .. method:: limit_denominator(max_denominator=1000000)
Christian Heimes68f5fbe2008-02-14 08:27:37 +000083
Benjamin Petersone41251e2008-04-25 01:59:09 +000084 Finds and returns the closest :class:`Fraction` to ``self`` that has
85 denominator at most max_denominator. This method is useful for finding
86 rational approximations to a given floating-point number:
Christian Heimes68f5fbe2008-02-14 08:27:37 +000087
Benjamin Petersone41251e2008-04-25 01:59:09 +000088 >>> from fractions import Fraction
89 >>> Fraction('3.1415926535897932').limit_denominator(1000)
Mark Dickinson79edbd52008-06-24 14:26:24 +000090 Fraction(355, 113)
Christian Heimes68f5fbe2008-02-14 08:27:37 +000091
Benjamin Petersone41251e2008-04-25 01:59:09 +000092 or for recovering a rational number that's represented as a float:
Christian Heimes68f5fbe2008-02-14 08:27:37 +000093
Benjamin Petersone41251e2008-04-25 01:59:09 +000094 >>> from math import pi, cos
95 >>> Fraction.from_float(cos(pi/3))
Mark Dickinson79edbd52008-06-24 14:26:24 +000096 Fraction(4503599627370497, 9007199254740992)
Benjamin Petersone41251e2008-04-25 01:59:09 +000097 >>> Fraction.from_float(cos(pi/3)).limit_denominator()
Mark Dickinson79edbd52008-06-24 14:26:24 +000098 Fraction(1, 2)
Christian Heimes68f5fbe2008-02-14 08:27:37 +000099
100
Benjamin Petersone41251e2008-04-25 01:59:09 +0000101 .. method:: __floor__()
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000102
Mark Dickinson268bf4a2008-06-24 15:32:27 +0000103 Returns the greatest :class:`int` ``<= self``. This method can
104 also be accessed through the :func:`math.floor` function:
105
106 >>> from math import floor
107 >>> floor(Fraction(355, 113))
108 3
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000109
110
Benjamin Petersone41251e2008-04-25 01:59:09 +0000111 .. method:: __ceil__()
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000112
Mark Dickinson268bf4a2008-06-24 15:32:27 +0000113 Returns the least :class:`int` ``>= self``. This method can
114 also be accessed through the :func:`math.ceil` function.
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000115
116
Benjamin Petersone41251e2008-04-25 01:59:09 +0000117 .. method:: __round__()
118 __round__(ndigits)
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000119
Mark Dickinson268bf4a2008-06-24 15:32:27 +0000120 The first version returns the nearest :class:`int` to ``self``,
121 rounding half to even. The second version rounds ``self`` to the
122 nearest multiple of ``Fraction(1, 10**ndigits)`` (logically, if
123 ``ndigits`` is negative), again rounding half toward even. This
124 method can also be accessed through the :func:`round` function.
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000125
126
Mark Dickinsonce279a72008-06-27 17:01:17 +0000127.. function:: gcd(a, b)
128
Georg Brandl36ab1ef2009-01-03 21:17:04 +0000129 Return the greatest common divisor of the integers *a* and *b*. If either
130 *a* or *b* is nonzero, then the absolute value of ``gcd(a, b)`` is the
131 largest integer that divides both *a* and *b*. ``gcd(a,b)`` has the same
132 sign as *b* if *b* is nonzero; otherwise it takes the sign of *a*. ``gcd(0,
133 0)`` returns ``0``.
Mark Dickinsonce279a72008-06-27 17:01:17 +0000134
135
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000136.. seealso::
137
138 Module :mod:`numbers`
139 The abstract base classes making up the numeric tower.