blob: 299e154d8c7c81438be583be9aee05672bbec5da [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
Christian Heimes3feef612008-02-11 06:19:17 +000033.. method:: Fraction.from_float(flt)
Guido van Rossum7736b5b2008-01-15 21:44:53 +000034
Christian Heimes3feef612008-02-11 06:19:17 +000035 This classmethod constructs a :class:`Fraction` representing the
Guido van Rossum7736b5b2008-01-15 21:44:53 +000036 exact value of *flt*, which must be a :class:`float`. Beware that
Christian Heimes3feef612008-02-11 06:19:17 +000037 ``Fraction.from_float(0.3)`` is not the same value as ``Rational(3,
Guido van Rossum7736b5b2008-01-15 21:44:53 +000038 10)``
39
40
Christian Heimes3feef612008-02-11 06:19:17 +000041.. method:: Fraction.from_decimal(dec)
Christian Heimes587c2bf2008-01-19 16:21:02 +000042
Christian Heimes3feef612008-02-11 06:19:17 +000043 This classmethod constructs a :class:`Fraction` representing the
Christian Heimes587c2bf2008-01-19 16:21:02 +000044 exact value of *dec*, which must be a
45 :class:`decimal.Decimal`.
46
47
Christian Heimes68f5fbe2008-02-14 08:27:37 +000048.. method:: Fraction.limit_denominator(max_denominator=1000000)
49
50 Finds and returns the closest :class:`Fraction` to ``self`` that
51 has denominator at most max_denominator. This method is useful for
Christian Heimesfe337bf2008-03-23 21:54:12 +000052 finding rational approximations to a given floating-point number:
Christian Heimes68f5fbe2008-02-14 08:27:37 +000053
Christian Heimesfe337bf2008-03-23 21:54:12 +000054 >>> from fractions import Fraction
Christian Heimes68f5fbe2008-02-14 08:27:37 +000055 >>> Fraction('3.1415926535897932').limit_denominator(1000)
Christian Heimesfe337bf2008-03-23 21:54:12 +000056 Fraction(355L, 113L)
Christian Heimes68f5fbe2008-02-14 08:27:37 +000057
Christian Heimesfe337bf2008-03-23 21:54:12 +000058 or for recovering a rational number that's represented as a float:
Christian Heimes68f5fbe2008-02-14 08:27:37 +000059
60 >>> from math import pi, cos
61 >>> Fraction.from_float(cos(pi/3))
62 Fraction(4503599627370497L, 9007199254740992L)
63 >>> Fraction.from_float(cos(pi/3)).limit_denominator()
Christian Heimesfe337bf2008-03-23 21:54:12 +000064 Fraction(1L, 2L)
Christian Heimes68f5fbe2008-02-14 08:27:37 +000065
66
Christian Heimes3feef612008-02-11 06:19:17 +000067.. method:: Fraction.__floor__()
Guido van Rossum7736b5b2008-01-15 21:44:53 +000068
69 Returns the greatest :class:`int` ``<= self``. Will be accessible
70 through :func:`math.floor` in Py3k.
71
72
Christian Heimes3feef612008-02-11 06:19:17 +000073.. method:: Fraction.__ceil__()
Guido van Rossum7736b5b2008-01-15 21:44:53 +000074
75 Returns the least :class:`int` ``>= self``. Will be accessible
76 through :func:`math.ceil` in Py3k.
77
78
Christian Heimes3feef612008-02-11 06:19:17 +000079.. method:: Fraction.__round__()
80 Fraction.__round__(ndigits)
Guido van Rossum7736b5b2008-01-15 21:44:53 +000081
82 The first version returns the nearest :class:`int` to ``self``,
83 rounding half to even. The second version rounds ``self`` to the
Christian Heimes3feef612008-02-11 06:19:17 +000084 nearest multiple of ``Fraction(1, 10**ndigits)`` (logically, if
Guido van Rossum7736b5b2008-01-15 21:44:53 +000085 ``ndigits`` is negative), again rounding half toward even. Will be
86 accessible through :func:`round` in Py3k.
87
88
89.. seealso::
90
91 Module :mod:`numbers`
92 The abstract base classes making up the numeric tower.