blob: 36df11cc431236203d3d5e73a44ff627073579fe [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)
20 Fraction(string)
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000021
22 The first version requires that *numerator* and *denominator* are
23 instances of :class:`numbers.Integral` and returns a new
Mark Dickinsondf90ee62008-06-27 16:49:27 +000024 :class:`Fraction` instance with value ``numerator/denominator``. If
25 *denominator* is :const:`0`, it raises a
26 :exc:`ZeroDivisionError`. The second version requires that
27 *other_fraction* is an instance of :class:`numbers.Rational` and
28 returns an :class:`Fraction` instance with the same value. The
29 last version of the constructor expects a string or unicode
30 instance in one of two possible forms. The first form is::
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000031
Mark Dickinsondf90ee62008-06-27 16:49:27 +000032 [sign] numerator ['/' denominator]
33
34 where the optional ``sign`` may be either '+' or '-' and
35 ``numerator`` and ``denominator`` (if present) are strings of
36 decimal digits. The second permitted form is that of a number
37 containing a decimal point::
38
39 [sign] integer '.' [fraction] | [sign] '.' fraction
40
41 where ``integer`` and ``fraction`` are strings of digits. In
42 either form the input string may also have leading and/or trailing
43 whitespace. Here are some examples::
44
45 >>> from fractions import Fraction
46 >>> Fraction(16, -10)
47 Fraction(-8, 5)
48 >>> Fraction(123)
49 Fraction(123, 1)
50 >>> Fraction()
51 Fraction(0, 1)
52 >>> Fraction('3/7')
53 Fraction(3, 7)
54 [40794 refs]
55 >>> Fraction(' -3/7 ')
56 Fraction(-3, 7)
57 >>> Fraction('1.414213 \t\n')
58 Fraction(1414213, 1000000)
59 >>> Fraction('-.125')
60 Fraction(-1, 8)
61
62
63 The :class:`Fraction` class inherits from the abstract base class
64 :class:`numbers.Rational`, and implements all of the methods and
65 operations from that class. :class:`Fraction` instances are hashable,
66 and should be treated as immutable. In addition,
67 :class:`Fraction` has the following methods:
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000068
69
Benjamin Petersonc7b05922008-04-25 01:29:10 +000070 .. method:: from_float(flt)
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000071
Mark Dickinsondf90ee62008-06-27 16:49:27 +000072 This class method constructs a :class:`Fraction` representing the exact
Benjamin Petersonc7b05922008-04-25 01:29:10 +000073 value of *flt*, which must be a :class:`float`. Beware that
74 ``Fraction.from_float(0.3)`` is not the same value as ``Fraction(3, 10)``
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000075
76
Benjamin Petersonc7b05922008-04-25 01:29:10 +000077 .. method:: from_decimal(dec)
Jeffrey Yasskin45169fb2008-01-19 09:56:06 +000078
Mark Dickinsondf90ee62008-06-27 16:49:27 +000079 This class method constructs a :class:`Fraction` representing the exact
Benjamin Petersonc7b05922008-04-25 01:29:10 +000080 value of *dec*, which must be a :class:`decimal.Decimal`.
Jeffrey Yasskin45169fb2008-01-19 09:56:06 +000081
82
Benjamin Petersonc7b05922008-04-25 01:29:10 +000083 .. method:: limit_denominator(max_denominator=1000000)
Mark Dickinsone1b82472008-02-12 21:31:59 +000084
Benjamin Petersonc7b05922008-04-25 01:29:10 +000085 Finds and returns the closest :class:`Fraction` to ``self`` that has
86 denominator at most max_denominator. This method is useful for finding
87 rational approximations to a given floating-point number:
Mark Dickinsone1b82472008-02-12 21:31:59 +000088
Benjamin Petersonc7b05922008-04-25 01:29:10 +000089 >>> from fractions import Fraction
90 >>> Fraction('3.1415926535897932').limit_denominator(1000)
Mark Dickinsondf90ee62008-06-27 16:49:27 +000091 Fraction(355, 113)
Mark Dickinsone1b82472008-02-12 21:31:59 +000092
Benjamin Petersonc7b05922008-04-25 01:29:10 +000093 or for recovering a rational number that's represented as a float:
Mark Dickinsone1b82472008-02-12 21:31:59 +000094
Benjamin Petersonc7b05922008-04-25 01:29:10 +000095 >>> from math import pi, cos
96 >>> Fraction.from_float(cos(pi/3))
Mark Dickinsondf90ee62008-06-27 16:49:27 +000097 Fraction(4503599627370497, 9007199254740992)
Benjamin Petersonc7b05922008-04-25 01:29:10 +000098 >>> Fraction.from_float(cos(pi/3)).limit_denominator()
Mark Dickinsondf90ee62008-06-27 16:49:27 +000099 Fraction(1, 2)
Mark Dickinsone1b82472008-02-12 21:31:59 +0000100
101
Mark Dickinsondf90ee62008-06-27 16:49:27 +0000102.. function:: gcd(a, b)
Jeffrey Yasskind7b00332008-01-15 07:46:24 +0000103
Georg Brandle92818f2009-01-03 20:47:01 +0000104 Return the greatest common divisor of the integers *a* and *b*. If either
105 *a* or *b* is nonzero, then the absolute value of ``gcd(a, b)`` is the
106 largest integer that divides both *a* and *b*. ``gcd(a,b)`` has the same
107 sign as *b* if *b* is nonzero; otherwise it takes the sign of *a*. ``gcd(0,
108 0)`` returns ``0``.
Jeffrey Yasskind7b00332008-01-15 07:46:24 +0000109
110
111.. seealso::
112
113 Module :mod:`numbers`
114 The abstract base classes making up the numeric tower.