Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 1 | |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 2 | :mod:`fractions` --- Rational numbers |
Christian Heimes | 0bd4e11 | 2008-02-12 22:59:25 +0000 | [diff] [blame] | 3 | ===================================== |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 4 | |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 5 | .. module:: fractions |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 6 | :synopsis: Rational numbers. |
| 7 | .. moduleauthor:: Jeffrey Yasskin <jyasskin at gmail.com> |
| 8 | .. sectionauthor:: Jeffrey Yasskin <jyasskin at gmail.com> |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 9 | |
| 10 | |
Mark Dickinson | ce279a7 | 2008-06-27 17:01:17 +0000 | [diff] [blame] | 11 | The :mod:`fractions` module provides support for rational number arithmetic. |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 12 | |
| 13 | |
Mark Dickinson | ce279a7 | 2008-06-27 17:01:17 +0000 | [diff] [blame] | 14 | A Fraction instance can be constructed from a pair of integers, from |
| 15 | another rational number, or from a string. |
| 16 | |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 17 | .. class:: Fraction(numerator=0, denominator=1) |
| 18 | Fraction(other_fraction) |
| 19 | Fraction(string) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 20 | |
| 21 | The first version requires that *numerator* and *denominator* are |
| 22 | instances of :class:`numbers.Integral` and returns a new |
Mark Dickinson | ce279a7 | 2008-06-27 17:01:17 +0000 | [diff] [blame] | 23 | :class:`Fraction` instance with value ``numerator/denominator``. If |
| 24 | *denominator* is :const:`0`, it raises a |
| 25 | :exc:`ZeroDivisionError`. The second version requires that |
| 26 | *other_fraction* is an instance of :class:`numbers.Rational` and |
| 27 | returns an :class:`Fraction` instance with the same value. The |
Georg Brandl | b159008 | 2008-12-04 18:18:16 +0000 | [diff] [blame^] | 28 | last version of the constructor expects a string |
Mark Dickinson | ce279a7 | 2008-06-27 17:01:17 +0000 | [diff] [blame] | 29 | instance in one of two possible forms. The first form is:: |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 30 | |
Mark Dickinson | ce279a7 | 2008-06-27 17:01:17 +0000 | [diff] [blame] | 31 | [sign] numerator ['/' denominator] |
| 32 | |
| 33 | where the optional ``sign`` may be either '+' or '-' and |
| 34 | ``numerator`` and ``denominator`` (if present) are strings of |
| 35 | decimal digits. The second permitted form is that of a number |
| 36 | containing a decimal point:: |
| 37 | |
| 38 | [sign] integer '.' [fraction] | [sign] '.' fraction |
| 39 | |
| 40 | where ``integer`` and ``fraction`` are strings of digits. In |
| 41 | either form the input string may also have leading and/or trailing |
| 42 | whitespace. Here are some examples:: |
| 43 | |
| 44 | >>> from fractions import Fraction |
| 45 | >>> Fraction(16, -10) |
| 46 | Fraction(-8, 5) |
| 47 | >>> Fraction(123) |
| 48 | Fraction(123, 1) |
| 49 | >>> Fraction() |
| 50 | Fraction(0, 1) |
| 51 | >>> Fraction('3/7') |
| 52 | Fraction(3, 7) |
| 53 | [40794 refs] |
| 54 | >>> Fraction(' -3/7 ') |
| 55 | Fraction(-3, 7) |
| 56 | >>> Fraction('1.414213 \t\n') |
| 57 | Fraction(1414213, 1000000) |
| 58 | >>> Fraction('-.125') |
| 59 | Fraction(-1, 8) |
| 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: |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 67 | |
| 68 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 69 | .. method:: from_float(flt) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 70 | |
Mark Dickinson | ce279a7 | 2008-06-27 17:01:17 +0000 | [diff] [blame] | 71 | This class method constructs a :class:`Fraction` representing the exact |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 72 | value of *flt*, which must be a :class:`float`. Beware that |
Mark Dickinson | ce279a7 | 2008-06-27 17:01:17 +0000 | [diff] [blame] | 73 | ``Fraction.from_float(0.3)`` is not the same value as ``Fraction(3, 10)`` |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 74 | |
| 75 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 76 | .. method:: from_decimal(dec) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 77 | |
Mark Dickinson | ce279a7 | 2008-06-27 17:01:17 +0000 | [diff] [blame] | 78 | This class method constructs a :class:`Fraction` representing the exact |
Mark Dickinson | 268bf4a | 2008-06-24 15:32:27 +0000 | [diff] [blame] | 79 | value of *dec*, which must be a :class:`decimal.Decimal` instance. |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 80 | |
| 81 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 82 | .. method:: limit_denominator(max_denominator=1000000) |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 83 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +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: |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 87 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 88 | >>> from fractions import Fraction |
| 89 | >>> Fraction('3.1415926535897932').limit_denominator(1000) |
Mark Dickinson | 79edbd5 | 2008-06-24 14:26:24 +0000 | [diff] [blame] | 90 | Fraction(355, 113) |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 91 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 92 | or for recovering a rational number that's represented as a float: |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 93 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 94 | >>> from math import pi, cos |
| 95 | >>> Fraction.from_float(cos(pi/3)) |
Mark Dickinson | 79edbd5 | 2008-06-24 14:26:24 +0000 | [diff] [blame] | 96 | Fraction(4503599627370497, 9007199254740992) |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 97 | >>> Fraction.from_float(cos(pi/3)).limit_denominator() |
Mark Dickinson | 79edbd5 | 2008-06-24 14:26:24 +0000 | [diff] [blame] | 98 | Fraction(1, 2) |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 99 | |
| 100 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 101 | .. method:: __floor__() |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 102 | |
Mark Dickinson | 268bf4a | 2008-06-24 15:32:27 +0000 | [diff] [blame] | 103 | Returns the greatest :class:`int` ``<= self``. This method can |
| 104 | also be accessed through the :func:`math.floor` function: |
| 105 | |
| 106 | >>> from math import floor |
| 107 | >>> floor(Fraction(355, 113)) |
| 108 | 3 |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 109 | |
| 110 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 111 | .. method:: __ceil__() |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 112 | |
Mark Dickinson | 268bf4a | 2008-06-24 15:32:27 +0000 | [diff] [blame] | 113 | Returns the least :class:`int` ``>= self``. This method can |
| 114 | also be accessed through the :func:`math.ceil` function. |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 115 | |
| 116 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 117 | .. method:: __round__() |
| 118 | __round__(ndigits) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 119 | |
Mark Dickinson | 268bf4a | 2008-06-24 15:32:27 +0000 | [diff] [blame] | 120 | The first version returns the nearest :class:`int` to ``self``, |
| 121 | rounding half to even. The second version rounds ``self`` to the |
| 122 | nearest multiple of ``Fraction(1, 10**ndigits)`` (logically, if |
| 123 | ``ndigits`` is negative), again rounding half toward even. This |
| 124 | method can also be accessed through the :func:`round` function. |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 125 | |
| 126 | |
Mark Dickinson | ce279a7 | 2008-06-27 17:01:17 +0000 | [diff] [blame] | 127 | .. function:: gcd(a, b) |
| 128 | |
| 129 | Return the greatest common divisor of the integers `a` and `b`. If |
| 130 | either `a` or `b` is nonzero, then the absolute value of `gcd(a, |
| 131 | b)` is the largest integer that divides both `a` and `b`. `gcd(a,b)` |
| 132 | has the same sign as `b` if `b` is nonzero; otherwise it takes the sign |
| 133 | of `a`. `gcd(0, 0)` returns `0`. |
| 134 | |
| 135 | |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 136 | .. seealso:: |
| 137 | |
| 138 | Module :mod:`numbers` |
| 139 | The abstract base classes making up the numeric tower. |