~pythonregexp2.7/python/issue2636-01+09-01-01

« back to all changes in this revision

Viewing changes to Doc/library/fractions.rst

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-09-22 00:02:12 UTC
  • mfrom: (39022.1.34 Regexp-2.7)
  • Revision ID: darklord@timehorse.com-20080922000212-7r0q4f4ugiq57jph
Merged in changes from the Atomic Grouping / Possessive Qualifiers branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
.. versionadded:: 2.6
10
10
 
11
11
 
12
 
The :mod:`fractions` module defines an immutable, infinite-precision
13
 
Fraction number class.
14
 
 
 
12
The :mod:`fractions` module provides support for rational number arithmetic.
 
13
 
 
14
 
 
15
A Fraction instance can be constructed from a pair of integers, from
 
16
another rational number, or from a string.
15
17
 
16
18
.. class:: Fraction(numerator=0, denominator=1)
17
19
           Fraction(other_fraction)
19
21
 
20
22
   The first version requires that *numerator* and *denominator* are
21
23
   instances of :class:`numbers.Integral` and returns a new
22
 
   ``Fraction`` representing ``numerator/denominator``. If
23
 
   *denominator* is :const:`0`, raises a :exc:`ZeroDivisionError`. The
24
 
   second version requires that *other_fraction* is an instance of
25
 
   :class:`numbers.Rational` and returns an instance of
26
 
   :class:`Fraction` with the same value. The third version expects a
27
 
   string of the form ``[-+]?[0-9]+(/[0-9]+)?``, optionally surrounded
28
 
   by spaces.
29
 
 
30
 
   Implements all of the methods and operations from
31
 
   :class:`numbers.Rational` and is immutable and hashable.
 
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
 
30
   instance in one of two possible forms.  The first form is::
 
31
 
 
32
      [sign] numerator ['/' denominator]
 
33
 
 
34
   where the optional ``sign`` may be either '+' or '-' and
 
35
   ``numerator`` and ``denominator`` (if present) are strings of
 
36
   decimal digits.  The second permitted form is that of a number
 
37
   containing a decimal point::
 
38
 
 
39
      [sign] integer '.' [fraction] | [sign] '.' fraction
 
40
 
 
41
   where ``integer`` and ``fraction`` are strings of digits.  In
 
42
   either form the input string may also have leading and/or trailing
 
43
   whitespace.  Here are some examples::
 
44
 
 
45
      >>> from fractions import Fraction
 
46
      >>> Fraction(16, -10)
 
47
      Fraction(-8, 5)
 
48
      >>> Fraction(123)
 
49
      Fraction(123, 1)
 
50
      >>> Fraction()
 
51
      Fraction(0, 1)
 
52
      >>> Fraction('3/7')
 
53
      Fraction(3, 7)
 
54
      [40794 refs]
 
55
      >>> Fraction(' -3/7 ')
 
56
      Fraction(-3, 7)
 
57
      >>> Fraction('1.414213 \t\n')
 
58
      Fraction(1414213, 1000000)
 
59
      >>> Fraction('-.125')
 
60
      Fraction(-1, 8)
 
61
 
 
62
 
 
63
   The :class:`Fraction` class inherits from the abstract base class
 
64
   :class:`numbers.Rational`, and implements all of the methods and
 
65
   operations from that class.  :class:`Fraction` instances are hashable,
 
66
   and should be treated as immutable.  In addition,
 
67
   :class:`Fraction` has the following methods:
32
68
 
33
69
 
34
70
   .. method:: from_float(flt)
35
71
 
36
 
      This classmethod constructs a :class:`Fraction` representing the exact
 
72
      This class method constructs a :class:`Fraction` representing the exact
37
73
      value of *flt*, which must be a :class:`float`. Beware that
38
74
      ``Fraction.from_float(0.3)`` is not the same value as ``Fraction(3, 10)``
39
75
 
40
76
 
41
77
   .. method:: from_decimal(dec)
42
78
 
43
 
      This classmethod constructs a :class:`Fraction` representing the exact
 
79
      This class method constructs a :class:`Fraction` representing the exact
44
80
      value of *dec*, which must be a :class:`decimal.Decimal`.
45
81
 
46
82
 
52
88
 
53
89
         >>> from fractions import Fraction
54
90
         >>> Fraction('3.1415926535897932').limit_denominator(1000)
55
 
         Fraction(355L, 113L)
 
91
         Fraction(355, 113)
56
92
 
57
93
      or for recovering a rational number that's represented as a float:
58
94
 
59
95
         >>> from math import pi, cos
60
96
         >>> Fraction.from_float(cos(pi/3))
61
 
         Fraction(4503599627370497L, 9007199254740992L)
 
97
         Fraction(4503599627370497, 9007199254740992)
62
98
         >>> Fraction.from_float(cos(pi/3)).limit_denominator()
63
 
         Fraction(1L, 2L)
64
 
 
65
 
 
66
 
   .. method:: __floor__()
67
 
 
68
 
      Returns the greatest :class:`int` ``<= self``. Will be accessible through
69
 
      :func:`math.floor` in Py3k.
70
 
 
71
 
 
72
 
   .. method:: __ceil__()
73
 
 
74
 
      Returns the least :class:`int` ``>= self``. Will be accessible through
75
 
      :func:`math.ceil` in Py3k.
76
 
 
77
 
 
78
 
   .. method:: __round__()
79
 
               __round__(ndigits)
80
 
 
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.
 
99
         Fraction(1, 2)
 
100
 
 
101
 
 
102
.. function:: gcd(a, b)
 
103
 
 
104
   Return the greatest common divisor of the integers `a` and `b`.  If
 
105
   either `a` or `b` is nonzero, then the absolute value of `gcd(a,
 
106
   b)` is the largest integer that divides both `a` and `b`.  `gcd(a,b)`
 
107
   has the same sign as `b` if `b` is nonzero; otherwise it takes the sign
 
108
   of `a`.  `gcd(0, 0)` returns `0`.
86
109
 
87
110
 
88
111
.. seealso::