blob: 79600269c95f084105d8860d91e678eff689897c [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
9
Mark Dickinsonce279a72008-06-27 17:01:17 +000010The :mod:`fractions` module provides support for rational number arithmetic.
Guido van Rossum7736b5b2008-01-15 21:44:53 +000011
12
Mark Dickinsonce279a72008-06-27 17:01:17 +000013A Fraction instance can be constructed from a pair of integers, from
14another rational number, or from a string.
15
Christian Heimes3feef612008-02-11 06:19:17 +000016.. class:: Fraction(numerator=0, denominator=1)
17 Fraction(other_fraction)
18 Fraction(string)
Guido van Rossum7736b5b2008-01-15 21:44:53 +000019
20 The first version requires that *numerator* and *denominator* are
Mark Dickinson4d0fe042009-07-30 10:03:32 +000021 instances of :class:`numbers.Rational` and returns a new
Mark Dickinsonce279a72008-06-27 17:01:17 +000022 :class:`Fraction` instance with value ``numerator/denominator``. If
23 *denominator* is :const:`0`, it raises a
24 :exc:`ZeroDivisionError`. The second version requires that
25 *other_fraction* is an instance of :class:`numbers.Rational` and
26 returns an :class:`Fraction` instance with the same value. The
Mark Dickinsoncf63f2f2009-04-22 17:50:21 +000027 last version of the constructor expects a string instance. The
28 usual form for this string is::
Guido van Rossum7736b5b2008-01-15 21:44:53 +000029
Mark Dickinsonce279a72008-06-27 17:01:17 +000030 [sign] numerator ['/' denominator]
31
32 where the optional ``sign`` may be either '+' or '-' and
33 ``numerator`` and ``denominator`` (if present) are strings of
Mark Dickinsoncf63f2f2009-04-22 17:50:21 +000034 decimal digits. In addition, any string that represents a finite
35 value and is accepted by the :class:`float` constructor is also
36 accepted by the :class:`Fraction` constructor. In either form the
37 input string may also have leading and/or trailing whitespace.
38 Here are some examples::
Mark Dickinsonce279a72008-06-27 17:01:17 +000039
40 >>> from fractions import Fraction
41 >>> Fraction(16, -10)
42 Fraction(-8, 5)
43 >>> Fraction(123)
44 Fraction(123, 1)
45 >>> Fraction()
46 Fraction(0, 1)
47 >>> Fraction('3/7')
48 Fraction(3, 7)
49 [40794 refs]
50 >>> Fraction(' -3/7 ')
51 Fraction(-3, 7)
52 >>> Fraction('1.414213 \t\n')
53 Fraction(1414213, 1000000)
54 >>> Fraction('-.125')
55 Fraction(-1, 8)
Mark Dickinsoncf63f2f2009-04-22 17:50:21 +000056 >>> Fraction('7e-6')
57 Fraction(7, 1000000)
Mark Dickinsonce279a72008-06-27 17:01:17 +000058
59
60 The :class:`Fraction` class inherits from the abstract base class
61 :class:`numbers.Rational`, and implements all of the methods and
62 operations from that class. :class:`Fraction` instances are hashable,
63 and should be treated as immutable. In addition,
64 :class:`Fraction` has the following methods:
Guido van Rossum7736b5b2008-01-15 21:44:53 +000065
66
Benjamin Petersone41251e2008-04-25 01:59:09 +000067 .. method:: from_float(flt)
Guido van Rossum7736b5b2008-01-15 21:44:53 +000068
Mark Dickinsonce279a72008-06-27 17:01:17 +000069 This class method constructs a :class:`Fraction` representing the exact
Benjamin Petersone41251e2008-04-25 01:59:09 +000070 value of *flt*, which must be a :class:`float`. Beware that
Mark Dickinsonce279a72008-06-27 17:01:17 +000071 ``Fraction.from_float(0.3)`` is not the same value as ``Fraction(3, 10)``
Guido van Rossum7736b5b2008-01-15 21:44:53 +000072
73
Benjamin Petersone41251e2008-04-25 01:59:09 +000074 .. method:: from_decimal(dec)
Christian Heimes587c2bf2008-01-19 16:21:02 +000075
Mark Dickinsonce279a72008-06-27 17:01:17 +000076 This class method constructs a :class:`Fraction` representing the exact
Mark Dickinson268bf4a2008-06-24 15:32:27 +000077 value of *dec*, which must be a :class:`decimal.Decimal` instance.
Christian Heimes587c2bf2008-01-19 16:21:02 +000078
79
Benjamin Petersone41251e2008-04-25 01:59:09 +000080 .. method:: limit_denominator(max_denominator=1000000)
Christian Heimes68f5fbe2008-02-14 08:27:37 +000081
Benjamin Petersone41251e2008-04-25 01:59:09 +000082 Finds and returns the closest :class:`Fraction` to ``self`` that has
83 denominator at most max_denominator. This method is useful for finding
84 rational approximations to a given floating-point number:
Christian Heimes68f5fbe2008-02-14 08:27:37 +000085
Benjamin Petersone41251e2008-04-25 01:59:09 +000086 >>> from fractions import Fraction
87 >>> Fraction('3.1415926535897932').limit_denominator(1000)
Mark Dickinson79edbd52008-06-24 14:26:24 +000088 Fraction(355, 113)
Christian Heimes68f5fbe2008-02-14 08:27:37 +000089
Benjamin Petersone41251e2008-04-25 01:59:09 +000090 or for recovering a rational number that's represented as a float:
Christian Heimes68f5fbe2008-02-14 08:27:37 +000091
Benjamin Petersone41251e2008-04-25 01:59:09 +000092 >>> from math import pi, cos
93 >>> Fraction.from_float(cos(pi/3))
Mark Dickinson79edbd52008-06-24 14:26:24 +000094 Fraction(4503599627370497, 9007199254740992)
Benjamin Petersone41251e2008-04-25 01:59:09 +000095 >>> Fraction.from_float(cos(pi/3)).limit_denominator()
Mark Dickinson79edbd52008-06-24 14:26:24 +000096 Fraction(1, 2)
Christian Heimes68f5fbe2008-02-14 08:27:37 +000097
98
Benjamin Petersone41251e2008-04-25 01:59:09 +000099 .. method:: __floor__()
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000100
Mark Dickinson268bf4a2008-06-24 15:32:27 +0000101 Returns the greatest :class:`int` ``<= self``. This method can
102 also be accessed through the :func:`math.floor` function:
103
104 >>> from math import floor
105 >>> floor(Fraction(355, 113))
106 3
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000107
108
Benjamin Petersone41251e2008-04-25 01:59:09 +0000109 .. method:: __ceil__()
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000110
Mark Dickinson268bf4a2008-06-24 15:32:27 +0000111 Returns the least :class:`int` ``>= self``. This method can
112 also be accessed through the :func:`math.ceil` function.
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000113
114
Benjamin Petersone41251e2008-04-25 01:59:09 +0000115 .. method:: __round__()
116 __round__(ndigits)
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000117
Mark Dickinson268bf4a2008-06-24 15:32:27 +0000118 The first version returns the nearest :class:`int` to ``self``,
119 rounding half to even. The second version rounds ``self`` to the
120 nearest multiple of ``Fraction(1, 10**ndigits)`` (logically, if
121 ``ndigits`` is negative), again rounding half toward even. This
122 method can also be accessed through the :func:`round` function.
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000123
124
Mark Dickinsonce279a72008-06-27 17:01:17 +0000125.. function:: gcd(a, b)
126
Georg Brandl36ab1ef2009-01-03 21:17:04 +0000127 Return the greatest common divisor of the integers *a* and *b*. If either
128 *a* or *b* is nonzero, then the absolute value of ``gcd(a, b)`` is the
129 largest integer that divides both *a* and *b*. ``gcd(a,b)`` has the same
130 sign as *b* if *b* is nonzero; otherwise it takes the sign of *a*. ``gcd(0,
131 0)`` returns ``0``.
Mark Dickinsonce279a72008-06-27 17:01:17 +0000132
133
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000134.. seealso::
135
136 Module :mod:`numbers`
137 The abstract base classes making up the numeric tower.