Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +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) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 18 | Rational(string) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 19 | |
| 20 | The first version requires that *numerator* and *denominator* are |
| 21 | instances of :class:`numbers.Integral` and returns a new |
| 22 | ``Rational`` representing ``numerator/denominator``. If |
| 23 | *denominator* is :const:`0`, raises a :exc:`ZeroDivisionError`. The |
| 24 | second version requires that *other_rational* is an instance of |
| 25 | :class:`numbers.Rational` and returns an instance of |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 26 | :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 Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 29 | |
| 30 | Implements all of the methods and operations from |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 31 | :class:`numbers.Rational` and is immutable and hashable. |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 32 | |
| 33 | |
| 34 | .. method:: Rational.from_float(flt) |
| 35 | |
| 36 | This classmethod constructs a :class:`Rational` representing the |
| 37 | exact value of *flt*, which must be a :class:`float`. Beware that |
| 38 | ``Rational.from_float(0.3)`` is not the same value as ``Rational(3, |
| 39 | 10)`` |
| 40 | |
| 41 | |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 42 | .. method:: Rational.from_decimal(dec) |
| 43 | |
| 44 | This classmethod constructs a :class:`Rational` representing the |
| 45 | exact value of *dec*, which must be a |
| 46 | :class:`decimal.Decimal`. |
| 47 | |
| 48 | |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 49 | .. method:: Rational.__floor__() |
| 50 | |
| 51 | Returns the greatest :class:`int` ``<= self``. Will be accessible |
| 52 | through :func:`math.floor` in Py3k. |
| 53 | |
| 54 | |
| 55 | .. method:: Rational.__ceil__() |
| 56 | |
| 57 | Returns the least :class:`int` ``>= self``. Will be accessible |
| 58 | through :func:`math.ceil` in Py3k. |
| 59 | |
| 60 | |
| 61 | .. method:: Rational.__round__() |
| 62 | Rational.__round__(ndigits) |
| 63 | |
| 64 | The first version returns the nearest :class:`int` to ``self``, |
| 65 | rounding half to even. The second version rounds ``self`` to the |
| 66 | nearest multiple of ``Rational(1, 10**ndigits)`` (logically, if |
| 67 | ``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 | |