Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`rational` --- Rational numbers |
| 3 | ==================================== |
| 4 | |
| 5 | .. module:: rational |
| 6 | :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 | |
| 12 | The :mod:`rational` module defines an immutable, infinite-precision |
| 13 | Rational number class. |
| 14 | |
| 15 | |
| 16 | .. class:: Rational(numerator=0, denominator=1) |
| 17 | Rational(other_rational) |
| 18 | |
| 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. |
| 26 | |
| 27 | Implements all of the methods and operations from |
| 28 | :class:`numbers.Rational` and is hashable. |
| 29 | |
| 30 | |
| 31 | .. method:: Rational.from_float(flt) |
| 32 | |
| 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, |
| 36 | 10)`` |
| 37 | |
| 38 | |
| 39 | .. method:: Rational.__floor__() |
| 40 | |
| 41 | Returns the greatest :class:`int` ``<= self``. Will be accessible |
| 42 | through :func:`math.floor` in Py3k. |
| 43 | |
| 44 | |
| 45 | .. method:: Rational.__ceil__() |
| 46 | |
| 47 | Returns the least :class:`int` ``>= self``. Will be accessible |
| 48 | through :func:`math.ceil` in Py3k. |
| 49 | |
| 50 | |
| 51 | .. method:: Rational.__round__() |
| 52 | Rational.__round__(ndigits) |
| 53 | |
| 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. |
| 59 | |
| 60 | |
| 61 | .. seealso:: |
| 62 | |
| 63 | Module :mod:`numbers` |
| 64 | The abstract base classes making up the numeric tower. |
| 65 | |