2 :mod:`rational` --- Rational numbers
3 ====================================
6 :synopsis: Rational numbers.
7 .. moduleauthor:: Jeffrey Yasskin <jyasskin at gmail.com>
8 .. sectionauthor:: Jeffrey Yasskin <jyasskin at gmail.com>
12 The :mod:`rational` module defines an immutable, infinite-precision
13 Rational number class.
16 .. class:: Rational(numerator=0, denominator=1)
17 Rational(other_rational)
19 The first version requires that *numerator* and *denominator* are
20 instances of :class:`numbers.Integral` and returns a new
21 ``Rational`` representing ``numerator/denominator``. If
22 *denominator* is :const:`0`, raises a :exc:`ZeroDivisionError`. The
23 second version requires that *other_rational* is an instance of
24 :class:`numbers.Rational` and returns an instance of
25 :class:`Rational` with the same value.
27 Implements all of the methods and operations from
28 :class:`numbers.Rational` and is hashable.
31 .. method:: Rational.from_float(flt)
33 This classmethod constructs a :class:`Rational` representing the
34 exact value of *flt*, which must be a :class:`float`. Beware that
35 ``Rational.from_float(0.3)`` is not the same value as ``Rational(3,
39 .. method:: Rational.__floor__()
41 Returns the greatest :class:`int` ``<= self``. Will be accessible
42 through :func:`math.floor` in Py3k.
45 .. method:: Rational.__ceil__()
47 Returns the least :class:`int` ``>= self``. Will be accessible
48 through :func:`math.ceil` in Py3k.
51 .. method:: Rational.__round__()
52 Rational.__round__(ndigits)
54 The first version returns the nearest :class:`int` to ``self``,
55 rounding half to even. The second version rounds ``self`` to the
56 nearest multiple of ``Rational(1, 10**ndigits)`` (logically, if
57 ``ndigits`` is negative), again rounding half toward even. Will be
58 accessible through :func:`round` in Py3k.
64 The abstract base classes making up the numeric tower.