blob: e0e4bdd5bc65ec7832a66b11888f11208c0a9f7b [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
Christian Heimes3feef612008-02-11 06:19:17 +000011The :mod:`fractions` module defines an immutable, infinite-precision
Guido van Rossum7736b5b2008-01-15 21:44:53 +000012Rational number class.
13
14
Christian Heimes3feef612008-02-11 06:19:17 +000015.. class:: Fraction(numerator=0, denominator=1)
16 Fraction(other_fraction)
17 Fraction(string)
Guido van Rossum7736b5b2008-01-15 21:44:53 +000018
19 The first version requires that *numerator* and *denominator* are
20 instances of :class:`numbers.Integral` and returns a new
Christian Heimes3feef612008-02-11 06:19:17 +000021 ``Fraction`` representing ``numerator/denominator``. If
Guido van Rossum7736b5b2008-01-15 21:44:53 +000022 *denominator* is :const:`0`, raises a :exc:`ZeroDivisionError`. The
Christian Heimes3feef612008-02-11 06:19:17 +000023 second version requires that *other_fraction* is an instance of
24 :class:`numbers.Fraction` and returns an instance of
Christian Heimes587c2bf2008-01-19 16:21:02 +000025 :class:`Rational` with the same value. The third version expects a
26 string of the form ``[-+]?[0-9]+(/[0-9]+)?``, optionally surrounded
27 by spaces.
Guido van Rossum7736b5b2008-01-15 21:44:53 +000028
29 Implements all of the methods and operations from
Christian Heimes587c2bf2008-01-19 16:21:02 +000030 :class:`numbers.Rational` and is immutable and hashable.
Guido van Rossum7736b5b2008-01-15 21:44:53 +000031
32
Benjamin Petersone41251e2008-04-25 01:59:09 +000033 .. method:: from_float(flt)
Guido van Rossum7736b5b2008-01-15 21:44:53 +000034
Benjamin Petersone41251e2008-04-25 01:59:09 +000035 This classmethod constructs a :class:`Fraction` representing the exact
36 value of *flt*, which must be a :class:`float`. Beware that
37 ``Fraction.from_float(0.3)`` is not the same value as ``Rational(3, 10)``
Guido van Rossum7736b5b2008-01-15 21:44:53 +000038
39
Benjamin Petersone41251e2008-04-25 01:59:09 +000040 .. method:: from_decimal(dec)
Christian Heimes587c2bf2008-01-19 16:21:02 +000041
Benjamin Petersone41251e2008-04-25 01:59:09 +000042 This classmethod constructs a :class:`Fraction` representing the exact
43 value of *dec*, which must be a :class:`decimal.Decimal`.
Christian Heimes587c2bf2008-01-19 16:21:02 +000044
45
Benjamin Petersone41251e2008-04-25 01:59:09 +000046 .. method:: limit_denominator(max_denominator=1000000)
Christian Heimes68f5fbe2008-02-14 08:27:37 +000047
Benjamin Petersone41251e2008-04-25 01:59:09 +000048 Finds and returns the closest :class:`Fraction` to ``self`` that has
49 denominator at most max_denominator. This method is useful for finding
50 rational approximations to a given floating-point number:
Christian Heimes68f5fbe2008-02-14 08:27:37 +000051
Benjamin Petersone41251e2008-04-25 01:59:09 +000052 >>> from fractions import Fraction
53 >>> Fraction('3.1415926535897932').limit_denominator(1000)
Mark Dickinson79edbd52008-06-24 14:26:24 +000054 Fraction(355, 113)
Christian Heimes68f5fbe2008-02-14 08:27:37 +000055
Benjamin Petersone41251e2008-04-25 01:59:09 +000056 or for recovering a rational number that's represented as a float:
Christian Heimes68f5fbe2008-02-14 08:27:37 +000057
Benjamin Petersone41251e2008-04-25 01:59:09 +000058 >>> from math import pi, cos
59 >>> Fraction.from_float(cos(pi/3))
Mark Dickinson79edbd52008-06-24 14:26:24 +000060 Fraction(4503599627370497, 9007199254740992)
Benjamin Petersone41251e2008-04-25 01:59:09 +000061 >>> Fraction.from_float(cos(pi/3)).limit_denominator()
Mark Dickinson79edbd52008-06-24 14:26:24 +000062 Fraction(1, 2)
Christian Heimes68f5fbe2008-02-14 08:27:37 +000063
64
Benjamin Petersone41251e2008-04-25 01:59:09 +000065 .. method:: __floor__()
Guido van Rossum7736b5b2008-01-15 21:44:53 +000066
Benjamin Petersone41251e2008-04-25 01:59:09 +000067 Returns the greatest :class:`int` ``<= self``. Will be accessible through
68 :func:`math.floor` in Py3k.
Guido van Rossum7736b5b2008-01-15 21:44:53 +000069
70
Benjamin Petersone41251e2008-04-25 01:59:09 +000071 .. method:: __ceil__()
Guido van Rossum7736b5b2008-01-15 21:44:53 +000072
Benjamin Petersone41251e2008-04-25 01:59:09 +000073 Returns the least :class:`int` ``>= self``. Will be accessible through
74 :func:`math.ceil` in Py3k.
Guido van Rossum7736b5b2008-01-15 21:44:53 +000075
76
Benjamin Petersone41251e2008-04-25 01:59:09 +000077 .. method:: __round__()
78 __round__(ndigits)
Guido van Rossum7736b5b2008-01-15 21:44:53 +000079
Benjamin Petersone41251e2008-04-25 01:59:09 +000080 The first version returns the nearest :class:`int` to ``self``, rounding
81 half to even. The second version rounds ``self`` to the nearest multiple
82 of ``Fraction(1, 10**ndigits)`` (logically, if ``ndigits`` is negative),
83 again rounding half toward even. Will be accessible through :func:`round`
84 in Py3k.
Guido van Rossum7736b5b2008-01-15 21:44:53 +000085
86
87.. seealso::
88
89 Module :mod:`numbers`
90 The abstract base classes making up the numeric tower.