blob: 1deea64cc6938c66fc13e40eda757a042847bada [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>
9.. versionadded:: 2.6
10
11
Christian Heimes3feef612008-02-11 06:19:17 +000012The :mod:`fractions` module defines an immutable, infinite-precision
Guido van Rossum7736b5b2008-01-15 21:44:53 +000013Rational number class.
14
15
Christian Heimes3feef612008-02-11 06:19:17 +000016.. class:: Fraction(numerator=0, denominator=1)
17 Fraction(other_fraction)
18 Fraction(string)
Guido van Rossum7736b5b2008-01-15 21:44:53 +000019
20 The first version requires that *numerator* and *denominator* are
21 instances of :class:`numbers.Integral` and returns a new
Christian Heimes3feef612008-02-11 06:19:17 +000022 ``Fraction`` representing ``numerator/denominator``. If
Guido van Rossum7736b5b2008-01-15 21:44:53 +000023 *denominator* is :const:`0`, raises a :exc:`ZeroDivisionError`. The
Christian Heimes3feef612008-02-11 06:19:17 +000024 second version requires that *other_fraction* is an instance of
25 :class:`numbers.Fraction` and returns an instance of
Christian Heimes587c2bf2008-01-19 16:21:02 +000026 :class:`Rational` with the same value. The third version expects a
27 string of the form ``[-+]?[0-9]+(/[0-9]+)?``, optionally surrounded
28 by spaces.
Guido van Rossum7736b5b2008-01-15 21:44:53 +000029
30 Implements all of the methods and operations from
Christian Heimes587c2bf2008-01-19 16:21:02 +000031 :class:`numbers.Rational` and is immutable and hashable.
Guido van Rossum7736b5b2008-01-15 21:44:53 +000032
33
Christian Heimes3feef612008-02-11 06:19:17 +000034.. method:: Fraction.from_float(flt)
Guido van Rossum7736b5b2008-01-15 21:44:53 +000035
Christian Heimes3feef612008-02-11 06:19:17 +000036 This classmethod constructs a :class:`Fraction` representing the
Guido van Rossum7736b5b2008-01-15 21:44:53 +000037 exact value of *flt*, which must be a :class:`float`. Beware that
Christian Heimes3feef612008-02-11 06:19:17 +000038 ``Fraction.from_float(0.3)`` is not the same value as ``Rational(3,
Guido van Rossum7736b5b2008-01-15 21:44:53 +000039 10)``
40
41
Christian Heimes3feef612008-02-11 06:19:17 +000042.. method:: Fraction.from_decimal(dec)
Christian Heimes587c2bf2008-01-19 16:21:02 +000043
Christian Heimes3feef612008-02-11 06:19:17 +000044 This classmethod constructs a :class:`Fraction` representing the
Christian Heimes587c2bf2008-01-19 16:21:02 +000045 exact value of *dec*, which must be a
46 :class:`decimal.Decimal`.
47
48
Christian Heimes3feef612008-02-11 06:19:17 +000049.. method:: Fraction.__floor__()
Guido van Rossum7736b5b2008-01-15 21:44:53 +000050
51 Returns the greatest :class:`int` ``<= self``. Will be accessible
52 through :func:`math.floor` in Py3k.
53
54
Christian Heimes3feef612008-02-11 06:19:17 +000055.. method:: Fraction.__ceil__()
Guido van Rossum7736b5b2008-01-15 21:44:53 +000056
57 Returns the least :class:`int` ``>= self``. Will be accessible
58 through :func:`math.ceil` in Py3k.
59
60
Christian Heimes3feef612008-02-11 06:19:17 +000061.. method:: Fraction.__round__()
62 Fraction.__round__(ndigits)
Guido van Rossum7736b5b2008-01-15 21:44:53 +000063
64 The first version returns the nearest :class:`int` to ``self``,
65 rounding half to even. The second version rounds ``self`` to the
Christian Heimes3feef612008-02-11 06:19:17 +000066 nearest multiple of ``Fraction(1, 10**ndigits)`` (logically, if
Guido van Rossum7736b5b2008-01-15 21:44:53 +000067 ``ndigits`` is negative), again rounding half toward even. Will be
68 accessible through :func:`round` in Py3k.
69
70
71.. seealso::
72
73 Module :mod:`numbers`
74 The abstract base classes making up the numeric tower.
75