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 | df90ee6 | 2008-06-27 16:49:27 +0000 | [diff] [blame] | 12 | The :mod:`fractions` module provides support for rational number arithmetic. |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 13 | |
| 14 | |
Mark Dickinson | df90ee6 | 2008-06-27 16:49:27 +0000 | [diff] [blame] | 15 | A Fraction instance can be constructed from a pair of integers, from |
| 16 | another rational number, or from a string. |
| 17 | |
Mark Dickinson | d058cd2 | 2008-02-10 21:29:51 +0000 | [diff] [blame] | 18 | .. class:: Fraction(numerator=0, denominator=1) |
| 19 | Fraction(other_fraction) |
| 20 | Fraction(string) |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 21 | |
| 22 | The first version requires that *numerator* and *denominator* are |
Mark Dickinson | 9ad0b36 | 2009-07-30 10:00:10 +0000 | [diff] [blame] | 23 | instances of :class:`numbers.Rational` and returns a new |
Mark Dickinson | df90ee6 | 2008-06-27 16:49:27 +0000 | [diff] [blame] | 24 | :class:`Fraction` instance with value ``numerator/denominator``. If |
| 25 | *denominator* is :const:`0`, it raises a |
| 26 | :exc:`ZeroDivisionError`. The second version requires that |
| 27 | *other_fraction* is an instance of :class:`numbers.Rational` and |
| 28 | returns an :class:`Fraction` instance with the same value. The |
| 29 | last version of the constructor expects a string or unicode |
Mark Dickinson | 8100bd8 | 2009-04-22 18:15:25 +0000 | [diff] [blame] | 30 | instance. The usual form for this instance is:: |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 31 | |
Mark Dickinson | df90ee6 | 2008-06-27 16:49:27 +0000 | [diff] [blame] | 32 | [sign] numerator ['/' denominator] |
| 33 | |
| 34 | where the optional ``sign`` may be either '+' or '-' and |
| 35 | ``numerator`` and ``denominator`` (if present) are strings of |
Mark Dickinson | 8100bd8 | 2009-04-22 18:15:25 +0000 | [diff] [blame] | 36 | decimal digits. In addition, any string that represents a finite |
| 37 | value and is accepted by the :class:`float` constructor is also |
| 38 | accepted by the :class:`Fraction` constructor. In either form the |
| 39 | input string may also have leading and/or trailing whitespace. |
| 40 | Here are some examples:: |
Mark Dickinson | df90ee6 | 2008-06-27 16:49:27 +0000 | [diff] [blame] | 41 | |
| 42 | >>> from fractions import Fraction |
| 43 | >>> Fraction(16, -10) |
| 44 | Fraction(-8, 5) |
| 45 | >>> Fraction(123) |
| 46 | Fraction(123, 1) |
| 47 | >>> Fraction() |
| 48 | Fraction(0, 1) |
| 49 | >>> Fraction('3/7') |
| 50 | Fraction(3, 7) |
| 51 | [40794 refs] |
| 52 | >>> Fraction(' -3/7 ') |
| 53 | Fraction(-3, 7) |
| 54 | >>> Fraction('1.414213 \t\n') |
| 55 | Fraction(1414213, 1000000) |
| 56 | >>> Fraction('-.125') |
| 57 | Fraction(-1, 8) |
Mark Dickinson | 8100bd8 | 2009-04-22 18:15:25 +0000 | [diff] [blame] | 58 | >>> Fraction('7e-6') |
| 59 | Fraction(7, 1000000) |
Mark Dickinson | df90ee6 | 2008-06-27 16:49:27 +0000 | [diff] [blame] | 60 | |
| 61 | |
| 62 | The :class:`Fraction` class inherits from the abstract base class |
| 63 | :class:`numbers.Rational`, and implements all of the methods and |
| 64 | operations from that class. :class:`Fraction` instances are hashable, |
| 65 | and should be treated as immutable. In addition, |
| 66 | :class:`Fraction` has the following methods: |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 67 | |
| 68 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 69 | .. method:: from_float(flt) |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 70 | |
Mark Dickinson | df90ee6 | 2008-06-27 16:49:27 +0000 | [diff] [blame] | 71 | This class method constructs a :class:`Fraction` representing the exact |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 72 | value of *flt*, which must be a :class:`float`. Beware that |
| 73 | ``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] | 74 | |
| 75 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 76 | .. method:: from_decimal(dec) |
Jeffrey Yasskin | 45169fb | 2008-01-19 09:56:06 +0000 | [diff] [blame] | 77 | |
Mark Dickinson | df90ee6 | 2008-06-27 16:49:27 +0000 | [diff] [blame] | 78 | This class method constructs a :class:`Fraction` representing the exact |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 79 | value of *dec*, which must be a :class:`decimal.Decimal`. |
Jeffrey Yasskin | 45169fb | 2008-01-19 09:56:06 +0000 | [diff] [blame] | 80 | |
| 81 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 82 | .. method:: limit_denominator(max_denominator=1000000) |
Mark Dickinson | e1b8247 | 2008-02-12 21:31:59 +0000 | [diff] [blame] | 83 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 84 | Finds and returns the closest :class:`Fraction` to ``self`` that has |
| 85 | denominator at most max_denominator. This method is useful for finding |
| 86 | rational approximations to a given floating-point number: |
Mark Dickinson | e1b8247 | 2008-02-12 21:31:59 +0000 | [diff] [blame] | 87 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 88 | >>> from fractions import Fraction |
| 89 | >>> Fraction('3.1415926535897932').limit_denominator(1000) |
Mark Dickinson | df90ee6 | 2008-06-27 16:49:27 +0000 | [diff] [blame] | 90 | Fraction(355, 113) |
Mark Dickinson | e1b8247 | 2008-02-12 21:31:59 +0000 | [diff] [blame] | 91 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 92 | or for recovering a rational number that's represented as a float: |
Mark Dickinson | e1b8247 | 2008-02-12 21:31:59 +0000 | [diff] [blame] | 93 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 94 | >>> from math import pi, cos |
| 95 | >>> Fraction.from_float(cos(pi/3)) |
Mark Dickinson | df90ee6 | 2008-06-27 16:49:27 +0000 | [diff] [blame] | 96 | Fraction(4503599627370497, 9007199254740992) |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 97 | >>> Fraction.from_float(cos(pi/3)).limit_denominator() |
Mark Dickinson | df90ee6 | 2008-06-27 16:49:27 +0000 | [diff] [blame] | 98 | Fraction(1, 2) |
Mark Dickinson | e1b8247 | 2008-02-12 21:31:59 +0000 | [diff] [blame] | 99 | |
| 100 | |
Mark Dickinson | df90ee6 | 2008-06-27 16:49:27 +0000 | [diff] [blame] | 101 | .. function:: gcd(a, b) |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 102 | |
Georg Brandl | e92818f | 2009-01-03 20:47:01 +0000 | [diff] [blame] | 103 | Return the greatest common divisor of the integers *a* and *b*. If either |
| 104 | *a* or *b* is nonzero, then the absolute value of ``gcd(a, b)`` is the |
| 105 | largest integer that divides both *a* and *b*. ``gcd(a,b)`` has the same |
| 106 | sign as *b* if *b* is nonzero; otherwise it takes the sign of *a*. ``gcd(0, |
| 107 | 0)`` returns ``0``. |
Jeffrey Yasskin | d7b0033 | 2008-01-15 07:46:24 +0000 | [diff] [blame] | 108 | |
| 109 | |
| 110 | .. seealso:: |
| 111 | |
| 112 | Module :mod:`numbers` |
| 113 | The abstract base classes making up the numeric tower. |