blob: ceca9f5542f16ee349ad9dd05736d8d033e2fbea [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 Dickinsond058cd22008-02-10 21:29:51 +000012The :mod:`fractions` module defines an immutable, infinite-precision
13Fraction number class.
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000014
15
Mark Dickinsond058cd22008-02-10 21:29:51 +000016.. class:: Fraction(numerator=0, denominator=1)
17 Fraction(other_fraction)
18 Fraction(string)
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000019
20 The first version requires that *numerator* and *denominator* are
21 instances of :class:`numbers.Integral` and returns a new
Mark Dickinsond058cd22008-02-10 21:29:51 +000022 ``Fraction`` representing ``numerator/denominator``. If
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000023 *denominator* is :const:`0`, raises a :exc:`ZeroDivisionError`. The
Mark Dickinsond058cd22008-02-10 21:29:51 +000024 second version requires that *other_fraction* is an instance of
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000025 :class:`numbers.Rational` and returns an instance of
Mark Dickinsond058cd22008-02-10 21:29:51 +000026 :class:`Fraction` with the same value. The third version expects a
Jeffrey Yasskin45169fb2008-01-19 09:56:06 +000027 string of the form ``[-+]?[0-9]+(/[0-9]+)?``, optionally surrounded
28 by spaces.
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000029
30 Implements all of the methods and operations from
Jeffrey Yasskin45169fb2008-01-19 09:56:06 +000031 :class:`numbers.Rational` and is immutable and hashable.
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000032
33
Benjamin Petersonc7b05922008-04-25 01:29:10 +000034 .. method:: from_float(flt)
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000035
Benjamin Petersonc7b05922008-04-25 01:29:10 +000036 This classmethod constructs a :class:`Fraction` representing the exact
37 value of *flt*, which must be a :class:`float`. Beware that
38 ``Fraction.from_float(0.3)`` is not the same value as ``Fraction(3, 10)``
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000039
40
Benjamin Petersonc7b05922008-04-25 01:29:10 +000041 .. method:: from_decimal(dec)
Jeffrey Yasskin45169fb2008-01-19 09:56:06 +000042
Benjamin Petersonc7b05922008-04-25 01:29:10 +000043 This classmethod constructs a :class:`Fraction` representing the exact
44 value of *dec*, which must be a :class:`decimal.Decimal`.
Jeffrey Yasskin45169fb2008-01-19 09:56:06 +000045
46
Benjamin Petersonc7b05922008-04-25 01:29:10 +000047 .. method:: limit_denominator(max_denominator=1000000)
Mark Dickinsone1b82472008-02-12 21:31:59 +000048
Benjamin Petersonc7b05922008-04-25 01:29:10 +000049 Finds and returns the closest :class:`Fraction` to ``self`` that has
50 denominator at most max_denominator. This method is useful for finding
51 rational approximations to a given floating-point number:
Mark Dickinsone1b82472008-02-12 21:31:59 +000052
Benjamin Petersonc7b05922008-04-25 01:29:10 +000053 >>> from fractions import Fraction
54 >>> Fraction('3.1415926535897932').limit_denominator(1000)
55 Fraction(355L, 113L)
Mark Dickinsone1b82472008-02-12 21:31:59 +000056
Benjamin Petersonc7b05922008-04-25 01:29:10 +000057 or for recovering a rational number that's represented as a float:
Mark Dickinsone1b82472008-02-12 21:31:59 +000058
Benjamin Petersonc7b05922008-04-25 01:29:10 +000059 >>> from math import pi, cos
60 >>> Fraction.from_float(cos(pi/3))
61 Fraction(4503599627370497L, 9007199254740992L)
62 >>> Fraction.from_float(cos(pi/3)).limit_denominator()
63 Fraction(1L, 2L)
Mark Dickinsone1b82472008-02-12 21:31:59 +000064
65
Benjamin Petersonc7b05922008-04-25 01:29:10 +000066 .. method:: __floor__()
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000067
Mark Dickinsoncab7e562008-06-24 15:35:14 +000068 Returns the greatest :class:`int` ``<= self``.
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000069
70
Benjamin Petersonc7b05922008-04-25 01:29:10 +000071 .. method:: __ceil__()
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000072
Mark Dickinsoncab7e562008-06-24 15:35:14 +000073 Returns the least :class:`int` ``>= self``.
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000074
75
Benjamin Petersonc7b05922008-04-25 01:29:10 +000076 .. method:: __round__()
77 __round__(ndigits)
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000078
Benjamin Petersonc7b05922008-04-25 01:29:10 +000079 The first version returns the nearest :class:`int` to ``self``, rounding
80 half to even. The second version rounds ``self`` to the nearest multiple
81 of ``Fraction(1, 10**ndigits)`` (logically, if ``ndigits`` is negative),
Mark Dickinsoncab7e562008-06-24 15:35:14 +000082 again rounding half toward even.
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000083
84
85.. seealso::
86
87 Module :mod:`numbers`
88 The abstract base classes making up the numeric tower.