Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 1 | |
Mark Dickinson | d058cd2 | 2008-02-10 21:29:51 +0000 | [diff] [blame] | 2 | :mod:`fractions` --- Rational numbers |
Raymond Hettinger | 2ddbd80 | 2008-02-11 23:34:56 +0000 | [diff] [blame^] | 3 | ===================================== |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 4 | |
Mark Dickinson | d058cd2 | 2008-02-10 21:29:51 +0000 | [diff] [blame] | 5 | .. module:: fractions |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 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 | |
Mark Dickinson | d058cd2 | 2008-02-10 21:29:51 +0000 | [diff] [blame] | 12 | The :mod:`fractions` module defines an immutable, infinite-precision |
| 13 | Fraction number class. |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 14 | |
| 15 | |
Mark Dickinson | d058cd2 | 2008-02-10 21:29:51 +0000 | [diff] [blame] | 16 | .. class:: Fraction(numerator=0, denominator=1) |
| 17 | Fraction(other_fraction) |
| 18 | Fraction(string) |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 19 | |
| 20 | The first version requires that *numerator* and *denominator* are |
| 21 | instances of :class:`numbers.Integral` and returns a new |
Mark Dickinson | d058cd2 | 2008-02-10 21:29:51 +0000 | [diff] [blame] | 22 | ``Fraction`` representing ``numerator/denominator``. If |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 23 | *denominator* is :const:`0`, raises a :exc:`ZeroDivisionError`. The |
Mark Dickinson | d058cd2 | 2008-02-10 21:29:51 +0000 | [diff] [blame] | 24 | second version requires that *other_fraction* is an instance of |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 25 | :class:`numbers.Rational` and returns an instance of |
Mark Dickinson | d058cd2 | 2008-02-10 21:29:51 +0000 | [diff] [blame] | 26 | :class:`Fraction` with the same value. The third version expects a |
Jeffrey Yasskin | 45169fb | 2008-01-19 09:56:06 +0000 | [diff] [blame] | 27 | string of the form ``[-+]?[0-9]+(/[0-9]+)?``, optionally surrounded |
| 28 | by spaces. |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 29 | |
| 30 | Implements all of the methods and operations from |
Jeffrey Yasskin | 45169fb | 2008-01-19 09:56:06 +0000 | [diff] [blame] | 31 | :class:`numbers.Rational` and is immutable and hashable. |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 32 | |
| 33 | |
Mark Dickinson | d058cd2 | 2008-02-10 21:29:51 +0000 | [diff] [blame] | 34 | .. method:: Fraction.from_float(flt) |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 35 | |
Mark Dickinson | d058cd2 | 2008-02-10 21:29:51 +0000 | [diff] [blame] | 36 | This classmethod constructs a :class:`Fraction` representing the |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 37 | exact value of *flt*, which must be a :class:`float`. Beware that |
Mark Dickinson | d058cd2 | 2008-02-10 21:29:51 +0000 | [diff] [blame] | 38 | ``Fraction.from_float(0.3)`` is not the same value as ``Fraction(3, |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 39 | 10)`` |
| 40 | |
| 41 | |
Mark Dickinson | d058cd2 | 2008-02-10 21:29:51 +0000 | [diff] [blame] | 42 | .. method:: Fraction.from_decimal(dec) |
Jeffrey Yasskin | 45169fb | 2008-01-19 09:56:06 +0000 | [diff] [blame] | 43 | |
Mark Dickinson | d058cd2 | 2008-02-10 21:29:51 +0000 | [diff] [blame] | 44 | This classmethod constructs a :class:`Fraction` representing the |
Jeffrey Yasskin | 45169fb | 2008-01-19 09:56:06 +0000 | [diff] [blame] | 45 | exact value of *dec*, which must be a |
| 46 | :class:`decimal.Decimal`. |
| 47 | |
| 48 | |
Mark Dickinson | d058cd2 | 2008-02-10 21:29:51 +0000 | [diff] [blame] | 49 | .. method:: Fraction.__floor__() |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 50 | |
| 51 | Returns the greatest :class:`int` ``<= self``. Will be accessible |
| 52 | through :func:`math.floor` in Py3k. |
| 53 | |
| 54 | |
Mark Dickinson | d058cd2 | 2008-02-10 21:29:51 +0000 | [diff] [blame] | 55 | .. method:: Fraction.__ceil__() |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 56 | |
| 57 | Returns the least :class:`int` ``>= self``. Will be accessible |
| 58 | through :func:`math.ceil` in Py3k. |
| 59 | |
| 60 | |
Mark Dickinson | d058cd2 | 2008-02-10 21:29:51 +0000 | [diff] [blame] | 61 | .. method:: Fraction.__round__() |
| 62 | Fraction.__round__(ndigits) |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 63 | |
| 64 | The first version returns the nearest :class:`int` to ``self``, |
| 65 | rounding half to even. The second version rounds ``self`` to the |
Mark Dickinson | d058cd2 | 2008-02-10 21:29:51 +0000 | [diff] [blame] | 66 | nearest multiple of ``Fraction(1, 10**ndigits)`` (logically, if |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 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 | |