~pythonregexp2.7/python/issue2636-20

« back to all changes in this revision

Viewing changes to Doc/library/fractions.rst

  • Committer: benjamin.peterson
  • Date: 2008-04-25 01:29:10 UTC
  • Revision ID: svn-v3-trunk1:6015fed2-1504-0410-9fe1-9d1591cc4771:python%2Ftrunk:62490
reformat some documentation of classes so methods and attributes are under the class directive

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
   :class:`numbers.Rational` and is immutable and hashable.
32
32
 
33
33
 
34
 
.. method:: Fraction.from_float(flt)
35
 
 
36
 
   This classmethod constructs a :class:`Fraction` representing the
37
 
   exact 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,
39
 
   10)``
40
 
 
41
 
 
42
 
.. method:: Fraction.from_decimal(dec)
43
 
 
44
 
   This classmethod constructs a :class:`Fraction` representing the
45
 
   exact value of *dec*, which must be a
46
 
   :class:`decimal.Decimal`.
47
 
 
48
 
 
49
 
.. method:: Fraction.limit_denominator(max_denominator=1000000)
50
 
 
51
 
   Finds and returns the closest :class:`Fraction` to ``self`` that
52
 
   has denominator at most max_denominator.  This method is useful for
53
 
   finding rational approximations to a given floating-point number:
54
 
 
55
 
      >>> from fractions import Fraction
56
 
      >>> Fraction('3.1415926535897932').limit_denominator(1000)
57
 
      Fraction(355L, 113L)
58
 
 
59
 
   or for recovering a rational number that's represented as a float:
60
 
 
61
 
      >>> from math import pi, cos
62
 
      >>> Fraction.from_float(cos(pi/3))
63
 
      Fraction(4503599627370497L, 9007199254740992L)
64
 
      >>> Fraction.from_float(cos(pi/3)).limit_denominator()
65
 
      Fraction(1L, 2L)
66
 
 
67
 
 
68
 
.. method:: Fraction.__floor__()
69
 
 
70
 
   Returns the greatest :class:`int` ``<= self``. Will be accessible
71
 
   through :func:`math.floor` in Py3k.
72
 
 
73
 
 
74
 
.. method:: Fraction.__ceil__()
75
 
 
76
 
   Returns the least :class:`int` ``>= self``. Will be accessible
77
 
   through :func:`math.ceil` in Py3k.
78
 
 
79
 
 
80
 
.. method:: Fraction.__round__()
81
 
            Fraction.__round__(ndigits)
82
 
 
83
 
   The first version returns the nearest :class:`int` to ``self``,
84
 
   rounding half to even. The second version rounds ``self`` to the
85
 
   nearest multiple of ``Fraction(1, 10**ndigits)`` (logically, if
86
 
   ``ndigits`` is negative), again rounding half toward even. Will be
87
 
   accessible through :func:`round` in Py3k.
 
34
   .. method:: from_float(flt)
 
35
 
 
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)``
 
39
 
 
40
 
 
41
   .. method:: from_decimal(dec)
 
42
 
 
43
      This classmethod constructs a :class:`Fraction` representing the exact
 
44
      value of *dec*, which must be a :class:`decimal.Decimal`.
 
45
 
 
46
 
 
47
   .. method:: limit_denominator(max_denominator=1000000)
 
48
 
 
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:
 
52
 
 
53
         >>> from fractions import Fraction
 
54
         >>> Fraction('3.1415926535897932').limit_denominator(1000)
 
55
         Fraction(355L, 113L)
 
56
 
 
57
      or for recovering a rational number that's represented as a float:
 
58
 
 
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)
 
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.
88
86
 
89
87
 
90
88
.. seealso::