Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Doc/library/fractions.rst

    r2 r391  
    1 
    21:mod:`fractions` --- Rational numbers
    32=====================================
     
    98.. versionadded:: 2.6
    109
     10**Source code:** :source:`Lib/fractions.py`
     11
     12--------------
    1113
    1214The :mod:`fractions` module provides support for rational number arithmetic.
     
    1820.. class:: Fraction(numerator=0, denominator=1)
    1921           Fraction(other_fraction)
     22           Fraction(float)
     23           Fraction(decimal)
    2024           Fraction(string)
    2125
    22    The first version requires that *numerator* and *denominator* are
    23    instances of :class:`numbers.Integral` and returns a new
    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::
     26   The first version requires that *numerator* and *denominator* are instances
     27   of :class:`numbers.Rational` and returns a new :class:`Fraction` instance
     28   with value ``numerator/denominator``. If *denominator* is :const:`0`, it
     29   raises a :exc:`ZeroDivisionError`. The second version requires that
     30   *other_fraction* is an instance of :class:`numbers.Rational` and returns a
     31   :class:`Fraction` instance with the same value.  The next two versions accept
     32   either a :class:`float` or a :class:`decimal.Decimal` instance, and return a
     33   :class:`Fraction` instance with exactly the same value.  Note that due to the
     34   usual issues with binary floating-point (see :ref:`tut-fp-issues`), the
     35   argument to ``Fraction(1.1)`` is not exactly equal to 11/10, and so
     36   ``Fraction(1.1)`` does *not* return ``Fraction(11, 10)`` as one might expect.
     37   (But see the documentation for the :meth:`limit_denominator` method below.)
     38   The last version of the constructor expects a string or unicode instance.
     39   The usual form for this instance is::
    3140
    3241      [sign] numerator ['/' denominator]
     
    3443   where the optional ``sign`` may be either '+' or '-' and
    3544   ``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::
     45   decimal digits.  In addition, any string that represents a finite
     46   value and is accepted by the :class:`float` constructor is also
     47   accepted by the :class:`Fraction` constructor.  In either form the
     48   input string may also have leading and/or trailing whitespace.
     49   Here are some examples::
    4450
    4551      >>> from fractions import Fraction
     
    5258      >>> Fraction('3/7')
    5359      Fraction(3, 7)
    54       [40794 refs]
    5560      >>> Fraction(' -3/7 ')
    5661      Fraction(-3, 7)
     
    5964      >>> Fraction('-.125')
    6065      Fraction(-1, 8)
     66      >>> Fraction('7e-6')
     67      Fraction(7, 1000000)
     68      >>> Fraction(2.25)
     69      Fraction(9, 4)
     70      >>> Fraction(1.1)
     71      Fraction(2476979795053773, 2251799813685248)
     72      >>> from decimal import Decimal
     73      >>> Fraction(Decimal('1.1'))
     74      Fraction(11, 10)
    6175
    6276
     
    6781   :class:`Fraction` has the following methods:
    6882
     83   .. versionchanged:: 2.7
     84      The :class:`Fraction` constructor now accepts :class:`float` and
     85      :class:`decimal.Decimal` instances.
     86
    6987
    7088   .. method:: from_float(flt)
     
    7492      ``Fraction.from_float(0.3)`` is not the same value as ``Fraction(3, 10)``
    7593
     94      .. note:: From Python 2.7 onwards, you can also construct a
     95         :class:`Fraction` instance directly from a :class:`float`.
     96
    7697
    7798   .. method:: from_decimal(dec)
     
    79100      This class method constructs a :class:`Fraction` representing the exact
    80101      value of *dec*, which must be a :class:`decimal.Decimal`.
     102
     103      .. note:: From Python 2.7 onwards, you can also construct a
     104         :class:`Fraction` instance directly from a :class:`decimal.Decimal`
     105         instance.
    81106
    82107
     
    94119
    95120         >>> from math import pi, cos
    96          >>> Fraction.from_float(cos(pi/3))
     121         >>> Fraction(cos(pi/3))
    97122         Fraction(4503599627370497, 9007199254740992)
    98          >>> Fraction.from_float(cos(pi/3)).limit_denominator()
     123         >>> Fraction(cos(pi/3)).limit_denominator()
    99124         Fraction(1, 2)
     125         >>> Fraction(1.1).limit_denominator()
     126         Fraction(11, 10)
    100127
    101128
Note: See TracChangeset for help on using the changeset viewer.