diff options
author | Mark Dickinson <dickinsm@gmail.com> | 2008-02-10 21:29:51 (GMT) |
---|---|---|
committer | Mark Dickinson <dickinsm@gmail.com> | 2008-02-10 21:29:51 (GMT) |
commit | d058cd2cc8e2a3f61609b92a8fc821ea8ec524ca (patch) | |
tree | 07e5d6aa70f60c886ca138de24fdca84686a0b54 /Doc | |
parent | da614dcc4f56bfb136c53b04d60889870d969926 (diff) | |
download | cpython-d058cd2cc8e2a3f61609b92a8fc821ea8ec524ca.zip cpython-d058cd2cc8e2a3f61609b92a8fc821ea8ec524ca.tar.gz cpython-d058cd2cc8e2a3f61609b92a8fc821ea8ec524ca.tar.bz2 |
Rename rational.Rational to fractions.Fraction, to avoid name clash
with numbers.Rational. See issue #1682 for related discussion.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/fractions.rst (renamed from Doc/library/rational.rst) | 40 | ||||
-rw-r--r-- | Doc/library/numbers.rst | 10 | ||||
-rw-r--r-- | Doc/whatsnew/2.6.rst | 22 |
3 files changed, 36 insertions, 36 deletions
diff --git a/Doc/library/rational.rst b/Doc/library/fractions.rst index 8ed702f..af6ed76 100644 --- a/Doc/library/rational.rst +++ b/Doc/library/fractions.rst @@ -1,29 +1,29 @@ -:mod:`rational` --- Rational numbers +:mod:`fractions` --- Rational numbers ==================================== -.. module:: rational +.. module:: fractions :synopsis: Rational numbers. .. moduleauthor:: Jeffrey Yasskin <jyasskin at gmail.com> .. sectionauthor:: Jeffrey Yasskin <jyasskin at gmail.com> .. versionadded:: 2.6 -The :mod:`rational` module defines an immutable, infinite-precision -Rational number class. +The :mod:`fractions` module defines an immutable, infinite-precision +Fraction number class. -.. class:: Rational(numerator=0, denominator=1) - Rational(other_rational) - Rational(string) +.. class:: Fraction(numerator=0, denominator=1) + Fraction(other_fraction) + Fraction(string) The first version requires that *numerator* and *denominator* are instances of :class:`numbers.Integral` and returns a new - ``Rational`` representing ``numerator/denominator``. If + ``Fraction`` representing ``numerator/denominator``. If *denominator* is :const:`0`, raises a :exc:`ZeroDivisionError`. The - second version requires that *other_rational* is an instance of + second version requires that *other_fraction* is an instance of :class:`numbers.Rational` and returns an instance of - :class:`Rational` with the same value. The third version expects a + :class:`Fraction` with the same value. The third version expects a string of the form ``[-+]?[0-9]+(/[0-9]+)?``, optionally surrounded by spaces. @@ -31,39 +31,39 @@ Rational number class. :class:`numbers.Rational` and is immutable and hashable. -.. method:: Rational.from_float(flt) +.. method:: Fraction.from_float(flt) - This classmethod constructs a :class:`Rational` representing the + This classmethod constructs a :class:`Fraction` representing the exact value of *flt*, which must be a :class:`float`. Beware that - ``Rational.from_float(0.3)`` is not the same value as ``Rational(3, + ``Fraction.from_float(0.3)`` is not the same value as ``Fraction(3, 10)`` -.. method:: Rational.from_decimal(dec) +.. method:: Fraction.from_decimal(dec) - This classmethod constructs a :class:`Rational` representing the + This classmethod constructs a :class:`Fraction` representing the exact value of *dec*, which must be a :class:`decimal.Decimal`. -.. method:: Rational.__floor__() +.. method:: Fraction.__floor__() Returns the greatest :class:`int` ``<= self``. Will be accessible through :func:`math.floor` in Py3k. -.. method:: Rational.__ceil__() +.. method:: Fraction.__ceil__() Returns the least :class:`int` ``>= self``. Will be accessible through :func:`math.ceil` in Py3k. -.. method:: Rational.__round__() - Rational.__round__(ndigits) +.. method:: Fraction.__round__() + Fraction.__round__(ndigits) The first version returns the nearest :class:`int` to ``self``, rounding half to even. The second version rounds ``self`` to the - nearest multiple of ``Rational(1, 10**ndigits)`` (logically, if + nearest multiple of ``Fraction(1, 10**ndigits)`` (logically, if ``ndigits`` is negative), again rounding half toward even. Will be accessible through :func:`round` in Py3k. diff --git a/Doc/library/numbers.rst b/Doc/library/numbers.rst index 6ee8f27..7a5f105 100644 --- a/Doc/library/numbers.rst +++ b/Doc/library/numbers.rst @@ -106,7 +106,7 @@ Notes for type implementors Implementors should be careful to make equal numbers equal and hash them to the same values. This may be subtle if there are two different -extensions of the real numbers. For example, :class:`rational.Rational` +extensions of the real numbers. For example, :class:`fractions.Fraction` implements :func:`hash` as follows:: def __hash__(self): @@ -201,11 +201,11 @@ in :class:`complex`, and both :meth:`__radd__` s land there, so ``a+b Because most of the operations on any given type will be very similar, it can be useful to define a helper function which generates the forward and reverse instances of any given operator. For example, -:class:`rational.Rational` uses:: +:class:`fractions.Fraction` uses:: def _operator_fallbacks(monomorphic_operator, fallback_operator): def forward(a, b): - if isinstance(b, (int, long, Rational)): + if isinstance(b, (int, long, Fraction)): return monomorphic_operator(a, b) elif isinstance(b, float): return fallback_operator(float(a), b) @@ -217,7 +217,7 @@ forward and reverse instances of any given operator. For example, forward.__doc__ = monomorphic_operator.__doc__ def reverse(b, a): - if isinstance(a, RationalAbc): + if isinstance(a, Rational): # Includes ints. return monomorphic_operator(a, b) elif isinstance(a, numbers.Real): @@ -233,7 +233,7 @@ forward and reverse instances of any given operator. For example, def _add(a, b): """a + b""" - return Rational(a.numerator * b.denominator + + return Fraction(a.numerator * b.denominator + b.numerator * a.denominator, a.denominator * b.denominator) diff --git a/Doc/whatsnew/2.6.rst b/Doc/whatsnew/2.6.rst index cbc8b8f..83cca99 100644 --- a/Doc/whatsnew/2.6.rst +++ b/Doc/whatsnew/2.6.rst @@ -578,8 +578,8 @@ and comparisons. :class:`Rational` numbers derive from :class:`Real`, have :attr:`numerator` and :attr:`denominator` properties, and can be -converted to floats. Python 2.6 adds a simple rational-number class -in the :mod:`rational` module. +converted to floats. Python 2.6 adds a simple rational-number class, +:class:`Fraction`, in the :mod:`fractions` module. :class:`Integral` numbers derive from :class:`Rational`, and can be shifted left and right with ``<<`` and ``>>``, @@ -598,29 +598,29 @@ one, :func:`trunc`, that's been backported to Python 2.6. -The Rational Module +The Fraction Module -------------------------------------------------- To fill out the hierarchy of numeric types, a rational-number class -has been added as the :mod:`rational` module. Rational numbers are +has been added as the :mod:`fractions` module. Rational numbers are represented as a fraction; rational numbers can exactly represent numbers such as two-thirds that floating-point numbers can only approximate. -The :class:`Rational` constructor takes two :class:`Integral` values +The :class:`Fraction` constructor takes two :class:`Integral` values that will be the numerator and denominator of the resulting fraction. :: - >>> from rational import Rational - >>> a = Rational(2, 3) - >>> b = Rational(2, 5) + >>> from fractions import Fraction + >>> a = Fraction(2, 3) + >>> b = Fraction(2, 5) >>> float(a), float(b) (0.66666666666666663, 0.40000000000000002) >>> a+b - rational.Rational(16,15) + Fraction(16,15) >>> a/b - rational.Rational(5,3) + Fraction(5,3) -The :mod:`rational` module is based upon an implementation by Sjoerd +The :mod:`fractions` module is based upon an implementation by Sjoerd Mullender that was in Python's :file:`Demo/classes/` directory for a long time. This implementation was significantly updated by Jeffrey Yaskin. |