31
31
:class:`numbers.Rational` and is immutable and hashable.
34
.. method:: Fraction.from_float(flt)
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,
42
.. method:: Fraction.from_decimal(dec)
44
This classmethod constructs a :class:`Fraction` representing the
45
exact value of *dec*, which must be a
46
:class:`decimal.Decimal`.
49
.. method:: Fraction.limit_denominator(max_denominator=1000000)
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:
55
>>> from fractions import Fraction
56
>>> Fraction('3.1415926535897932').limit_denominator(1000)
59
or for recovering a rational number that's represented as a float:
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()
68
.. method:: Fraction.__floor__()
70
Returns the greatest :class:`int` ``<= self``. Will be accessible
71
through :func:`math.floor` in Py3k.
74
.. method:: Fraction.__ceil__()
76
Returns the least :class:`int` ``>= self``. Will be accessible
77
through :func:`math.ceil` in Py3k.
80
.. method:: Fraction.__round__()
81
Fraction.__round__(ndigits)
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)
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)``
41
.. method:: from_decimal(dec)
43
This classmethod constructs a :class:`Fraction` representing the exact
44
value of *dec*, which must be a :class:`decimal.Decimal`.
47
.. method:: limit_denominator(max_denominator=1000000)
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:
53
>>> from fractions import Fraction
54
>>> Fraction('3.1415926535897932').limit_denominator(1000)
57
or for recovering a rational number that's represented as a float:
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()
66
.. method:: __floor__()
68
Returns the greatest :class:`int` ``<= self``. Will be accessible through
69
:func:`math.floor` in Py3k.
72
.. method:: __ceil__()
74
Returns the least :class:`int` ``>= self``. Will be accessible through
75
:func:`math.ceil` in Py3k.
78
.. method:: __round__()
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`