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 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 34 | .. method:: from_float(flt) |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 35 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 36 | This classmethod constructs a :class:`Fraction` representing the exact |
| 37 | value of *flt*, which must be a :class:`float`. Beware that |
| 38 | ``Fraction.from_float(0.3)`` is not the same value as ``Fraction(3, 10)`` |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 39 | |
| 40 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 41 | .. method:: from_decimal(dec) |
Jeffrey Yasskin | 45169fb | 2008-01-19 09:56:06 +0000 | [diff] [blame] | 42 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 43 | This classmethod constructs a :class:`Fraction` representing the exact |
| 44 | value of *dec*, which must be a :class:`decimal.Decimal`. |
Jeffrey Yasskin | 45169fb | 2008-01-19 09:56:06 +0000 | [diff] [blame] | 45 | |
| 46 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 47 | .. method:: limit_denominator(max_denominator=1000000) |
Mark Dickinson | e1b8247 | 2008-02-12 21:31:59 +0000 | [diff] [blame] | 48 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 49 | Finds and returns the closest :class:`Fraction` to ``self`` that has |
| 50 | denominator at most max_denominator. This method is useful for finding |
| 51 | rational approximations to a given floating-point number: |
Mark Dickinson | e1b8247 | 2008-02-12 21:31:59 +0000 | [diff] [blame] | 52 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 53 | >>> from fractions import Fraction |
| 54 | >>> Fraction('3.1415926535897932').limit_denominator(1000) |
| 55 | Fraction(355L, 113L) |
Mark Dickinson | e1b8247 | 2008-02-12 21:31:59 +0000 | [diff] [blame] | 56 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 57 | or for recovering a rational number that's represented as a float: |
Mark Dickinson | e1b8247 | 2008-02-12 21:31:59 +0000 | [diff] [blame] | 58 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 59 | >>> from math import pi, cos |
| 60 | >>> Fraction.from_float(cos(pi/3)) |
| 61 | Fraction(4503599627370497L, 9007199254740992L) |
| 62 | >>> Fraction.from_float(cos(pi/3)).limit_denominator() |
| 63 | Fraction(1L, 2L) |
Mark Dickinson | e1b8247 | 2008-02-12 21:31:59 +0000 | [diff] [blame] | 64 | |
| 65 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 66 | .. method:: __floor__() |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 67 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 68 | Returns the greatest :class:`int` ``<= self``. Will be accessible through |
| 69 | :func:`math.floor` in Py3k. |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 70 | |
| 71 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 72 | .. method:: __ceil__() |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 73 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 74 | Returns the least :class:`int` ``>= self``. Will be accessible through |
| 75 | :func:`math.ceil` in Py3k. |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 76 | |
| 77 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 78 | .. method:: __round__() |
| 79 | __round__(ndigits) |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 80 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 81 | The first version returns the nearest :class:`int` to ``self``, rounding |
| 82 | half to even. The second version rounds ``self`` to the nearest multiple |
| 83 | of ``Fraction(1, 10**ndigits)`` (logically, if ``ndigits`` is negative), |
| 84 | again rounding half toward even. Will be accessible through :func:`round` |
| 85 | in Py3k. |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 86 | |
| 87 | |
| 88 | .. seealso:: |
| 89 | |
| 90 | Module :mod:`numbers` |
| 91 | The abstract base classes making up the numeric tower. |