Issue #2531: Make float-to-decimal comparisons return correct results.

Float to decimal comparison operations now return a result based on
the numeric values of the operands.  Decimal.__hash__ has also been
fixed so that Decimal and float values that compare equal have equal
hash value.
This commit is contained in:
Mark Dickinson 2010-04-02 08:53:22 +00:00
parent 6eba779235
commit 99d8096c17
4 changed files with 74 additions and 13 deletions

View file

@ -364,6 +364,24 @@ Decimal objects
compared, sorted, and coerced to another type (such as :class:`float` or
:class:`long`).
Decimal objects cannot generally be combined with floats in
arithmetic operations: an attempt to add a :class:`Decimal` to a
:class:`float`, for example, will raise a :exc:`TypeError`.
There's one exception to this rule: it's possible to use Python's
comparison operators to compare a :class:`float` instance ``x``
with a :class:`Decimal` instance ``y``. Without this exception,
comparisons between :class:`Decimal` and :class:`float` instances
would follow the general rules for comparing objects of different
types described in the :ref:`expressions` section of the reference
manual, leading to confusing results.
.. versionchanged:: 2.7
A comparison between a :class:`float` instance ``x`` and a
:class:`Decimal` instance ``y`` now returns a result based on
the values of ``x`` and ``y``. In earlier versions ``x < y``
returned the same (arbitrary) result for any :class:`Decimal`
instance ``x`` and any :class:`float` instance ``y``.
In addition to the standard numeric properties, decimal floating point
objects also have a number of specialized methods: