mirror of
https://github.com/python/cpython.git
synced 2025-09-17 22:20:23 +00:00
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:
parent
6eba779235
commit
99d8096c17
4 changed files with 74 additions and 13 deletions
|
@ -1208,6 +1208,23 @@ class DecimalUsabilityTest(unittest.TestCase):
|
|||
self.assertFalse(Decimal(1) < None)
|
||||
self.assertTrue(Decimal(1) > None)
|
||||
|
||||
def test_decimal_float_comparison(self):
|
||||
da = Decimal('0.25')
|
||||
db = Decimal('3.0')
|
||||
self.assert_(da < 3.0)
|
||||
self.assert_(da <= 3.0)
|
||||
self.assert_(db > 0.25)
|
||||
self.assert_(db >= 0.25)
|
||||
self.assert_(da != 1.5)
|
||||
self.assert_(da == 0.25)
|
||||
self.assert_(3.0 > da)
|
||||
self.assert_(3.0 >= da)
|
||||
self.assert_(0.25 < db)
|
||||
self.assert_(0.25 <= db)
|
||||
self.assert_(0.25 != db)
|
||||
self.assert_(3.0 == db)
|
||||
self.assert_(0.1 != Decimal('0.1'))
|
||||
|
||||
def test_copy_and_deepcopy_methods(self):
|
||||
d = Decimal('43.24')
|
||||
c = copy.copy(d)
|
||||
|
@ -1256,6 +1273,15 @@ class DecimalUsabilityTest(unittest.TestCase):
|
|||
self.assertTrue(hash(Decimal('Inf')))
|
||||
self.assertTrue(hash(Decimal('-Inf')))
|
||||
|
||||
# check that the hashes of a Decimal float match when they
|
||||
# represent exactly the same values
|
||||
test_strings = ['inf', '-Inf', '0.0', '-.0e1',
|
||||
'34.0', '2.5', '112390.625', '-0.515625']
|
||||
for s in test_strings:
|
||||
f = float(s)
|
||||
d = Decimal(s)
|
||||
self.assertEqual(hash(f), hash(d))
|
||||
|
||||
# check that the value of the hash doesn't depend on the
|
||||
# current context (issue #1757)
|
||||
c = getcontext()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue