blob: af6ed76386e118292f39642e95b929f2a372f687 [file] [log] [blame]
Jeffrey Yasskind7b00332008-01-15 07:46:24 +00001
Mark Dickinsond058cd22008-02-10 21:29:51 +00002:mod:`fractions` --- Rational numbers
Jeffrey Yasskind7b00332008-01-15 07:46:24 +00003====================================
4
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
Mark Dickinsond058cd22008-02-10 21:29:51 +000034.. method:: Fraction.from_float(flt)
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000035
Mark Dickinsond058cd22008-02-10 21:29:51 +000036 This classmethod constructs a :class:`Fraction` representing the
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000037 exact value of *flt*, which must be a :class:`float`. Beware that
Mark Dickinsond058cd22008-02-10 21:29:51 +000038 ``Fraction.from_float(0.3)`` is not the same value as ``Fraction(3,
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000039 10)``
40
41
Mark Dickinsond058cd22008-02-10 21:29:51 +000042.. method:: Fraction.from_decimal(dec)
Jeffrey Yasskin45169fb2008-01-19 09:56:06 +000043
Mark Dickinsond058cd22008-02-10 21:29:51 +000044 This classmethod constructs a :class:`Fraction` representing the
Jeffrey Yasskin45169fb2008-01-19 09:56:06 +000045 exact value of *dec*, which must be a
46 :class:`decimal.Decimal`.
47
48
Mark Dickinsond058cd22008-02-10 21:29:51 +000049.. method:: Fraction.__floor__()
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000050
51 Returns the greatest :class:`int` ``<= self``. Will be accessible
52 through :func:`math.floor` in Py3k.
53
54
Mark Dickinsond058cd22008-02-10 21:29:51 +000055.. method:: Fraction.__ceil__()
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000056
57 Returns the least :class:`int` ``>= self``. Will be accessible
58 through :func:`math.ceil` in Py3k.
59
60
Mark Dickinsond058cd22008-02-10 21:29:51 +000061.. method:: Fraction.__round__()
62 Fraction.__round__(ndigits)
Jeffrey Yasskind7b00332008-01-15 07:46:24 +000063
64 The first version returns the nearest :class:`int` to ``self``,
65 rounding half to even. The second version rounds ``self`` to the
Mark Dickinsond058cd22008-02-10 21:29:51 +000066 nearest multiple of ``Fraction(1, 10**ndigits)`` (logically, if
Jeffrey Yasskind7b00332008-01-15 07:46:24 +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