blob: c135f9197a3fd3dd97836bec559965960e16e9de [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
Mark Dickinsoncf63f2f2009-04-22 17:50:21 +000028 last version of the constructor expects a string instance. The
29 usual form for this string 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
Mark Dickinsoncf63f2f2009-04-22 17:50:21 +000035 decimal digits. In addition, any string that represents a finite
36 value and is accepted by the :class:`float` constructor is also
37 accepted by the :class:`Fraction` constructor. In either form the
38 input string may also have leading and/or trailing whitespace.
39 Here are some examples::
Mark Dickinsonce279a72008-06-27 17:01:17 +000040
41 >>> from fractions import Fraction
42 >>> Fraction(16, -10)
43 Fraction(-8, 5)
44 >>> Fraction(123)
45 Fraction(123, 1)
46 >>> Fraction()
47 Fraction(0, 1)
48 >>> Fraction('3/7')
49 Fraction(3, 7)
50 [40794 refs]
51 >>> Fraction(' -3/7 ')
52 Fraction(-3, 7)
53 >>> Fraction('1.414213 \t\n')
54 Fraction(1414213, 1000000)
55 >>> Fraction('-.125')
56 Fraction(-1, 8)
Mark Dickinsoncf63f2f2009-04-22 17:50:21 +000057 >>> Fraction('7e-6')
58 Fraction(7, 1000000)
Mark Dickinsonce279a72008-06-27 17:01:17 +000059
60
61 The :class:`Fraction` class inherits from the abstract base class
62 :class:`numbers.Rational`, and implements all of the methods and
63 operations from that class. :class:`Fraction` instances are hashable,
64 and should be treated as immutable. In addition,
65 :class:`Fraction` has the following methods:
Guido van Rossum7736b5b2008-01-15 21:44:53 +000066
67
Benjamin Petersone41251e2008-04-25 01:59:09 +000068 .. method:: from_float(flt)
Guido van Rossum7736b5b2008-01-15 21:44:53 +000069
Mark Dickinsonce279a72008-06-27 17:01:17 +000070 This class method constructs a :class:`Fraction` representing the exact
Benjamin Petersone41251e2008-04-25 01:59:09 +000071 value of *flt*, which must be a :class:`float`. Beware that
Mark Dickinsonce279a72008-06-27 17:01:17 +000072 ``Fraction.from_float(0.3)`` is not the same value as ``Fraction(3, 10)``
Guido van Rossum7736b5b2008-01-15 21:44:53 +000073
74
Benjamin Petersone41251e2008-04-25 01:59:09 +000075 .. method:: from_decimal(dec)
Christian Heimes587c2bf2008-01-19 16:21:02 +000076
Mark Dickinsonce279a72008-06-27 17:01:17 +000077 This class method constructs a :class:`Fraction` representing the exact
Mark Dickinson268bf4a2008-06-24 15:32:27 +000078 value of *dec*, which must be a :class:`decimal.Decimal` instance.
Christian Heimes587c2bf2008-01-19 16:21:02 +000079
80
Benjamin Petersone41251e2008-04-25 01:59:09 +000081 .. method:: limit_denominator(max_denominator=1000000)
Christian Heimes68f5fbe2008-02-14 08:27:37 +000082
Benjamin Petersone41251e2008-04-25 01:59:09 +000083 Finds and returns the closest :class:`Fraction` to ``self`` that has
84 denominator at most max_denominator. This method is useful for finding
85 rational approximations to a given floating-point number:
Christian Heimes68f5fbe2008-02-14 08:27:37 +000086
Benjamin Petersone41251e2008-04-25 01:59:09 +000087 >>> from fractions import Fraction
88 >>> Fraction('3.1415926535897932').limit_denominator(1000)
Mark Dickinson79edbd52008-06-24 14:26:24 +000089 Fraction(355, 113)
Christian Heimes68f5fbe2008-02-14 08:27:37 +000090
Benjamin Petersone41251e2008-04-25 01:59:09 +000091 or for recovering a rational number that's represented as a float:
Christian Heimes68f5fbe2008-02-14 08:27:37 +000092
Benjamin Petersone41251e2008-04-25 01:59:09 +000093 >>> from math import pi, cos
94 >>> Fraction.from_float(cos(pi/3))
Mark Dickinson79edbd52008-06-24 14:26:24 +000095 Fraction(4503599627370497, 9007199254740992)
Benjamin Petersone41251e2008-04-25 01:59:09 +000096 >>> Fraction.from_float(cos(pi/3)).limit_denominator()
Mark Dickinson79edbd52008-06-24 14:26:24 +000097 Fraction(1, 2)
Christian Heimes68f5fbe2008-02-14 08:27:37 +000098
99
Benjamin Petersone41251e2008-04-25 01:59:09 +0000100 .. method:: __floor__()
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000101
Mark Dickinson268bf4a2008-06-24 15:32:27 +0000102 Returns the greatest :class:`int` ``<= self``. This method can
103 also be accessed through the :func:`math.floor` function:
104
105 >>> from math import floor
106 >>> floor(Fraction(355, 113))
107 3
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000108
109
Benjamin Petersone41251e2008-04-25 01:59:09 +0000110 .. method:: __ceil__()
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000111
Mark Dickinson268bf4a2008-06-24 15:32:27 +0000112 Returns the least :class:`int` ``>= self``. This method can
113 also be accessed through the :func:`math.ceil` function.
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000114
115
Benjamin Petersone41251e2008-04-25 01:59:09 +0000116 .. method:: __round__()
117 __round__(ndigits)
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000118
Mark Dickinson268bf4a2008-06-24 15:32:27 +0000119 The first version returns the nearest :class:`int` to ``self``,
120 rounding half to even. The second version rounds ``self`` to the
121 nearest multiple of ``Fraction(1, 10**ndigits)`` (logically, if
122 ``ndigits`` is negative), again rounding half toward even. This
123 method can also be accessed through the :func:`round` function.
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000124
125
Mark Dickinsonce279a72008-06-27 17:01:17 +0000126.. function:: gcd(a, b)
127
Georg Brandl36ab1ef2009-01-03 21:17:04 +0000128 Return the greatest common divisor of the integers *a* and *b*. If either
129 *a* or *b* is nonzero, then the absolute value of ``gcd(a, b)`` is the
130 largest integer that divides both *a* and *b*. ``gcd(a,b)`` has the same
131 sign as *b* if *b* is nonzero; otherwise it takes the sign of *a*. ``gcd(0,
132 0)`` returns ``0``.
Mark Dickinsonce279a72008-06-27 17:01:17 +0000133
134
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000135.. seealso::
136
137 Module :mod:`numbers`
138 The abstract base classes making up the numeric tower.