Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5daab45

Browse files
committed
Merged revisions 79804 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r79804 | mark.dickinson | 2010-04-05 19:07:51 +0100 (Mon, 05 Apr 2010) | 5 lines Use a more robust infinity check in _Py_HashDouble. This fixes a test_decimal failure on FreeBSD 8.0. (modf apparently doesn't follow C99 Annex F on FreeBSD.) ........
1 parent 19192dd commit 5daab45

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

Lib/test/test_float.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,15 @@ def notest_float_inf(self):
914914
self.assertFalse(NAN.is_inf())
915915
self.assertFalse((0.).is_inf())
916916

917+
def test_hash_inf(self):
918+
# the actual values here should be regarded as an
919+
# implementation detail, but they need to be
920+
# identical to those used in the Decimal module.
921+
self.assertEqual(hash(float('inf')), 314159)
922+
self.assertEqual(hash(float('-inf')), -271828)
923+
self.assertEqual(hash(float('nan')), 0)
924+
925+
917926
fromHex = float.fromhex
918927
toHex = float.hex
919928
class HexFloatTestCase(unittest.TestCase):

Objects/object.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -656,15 +656,15 @@ _Py_HashDouble(double v)
656656
* of mapping keys will turn out weird.
657657
*/
658658

659+
if (Py_IS_INFINITY(v))
660+
/* can't convert to long int -- arbitrary */
661+
v = v < 0 ? -271828.0 : 314159.0;
659662
fractpart = modf(v, &intpart);
660663
if (fractpart == 0.0) {
661664
/* This must return the same hash as an equal int or long. */
662665
if (intpart > LONG_MAX/2 || -intpart > LONG_MAX/2) {
663666
/* Convert to long and use its hash. */
664667
PyObject *plong; /* converted to Python long */
665-
if (Py_IS_INFINITY(intpart))
666-
/* can't convert to long int -- arbitrary */
667-
v = v < 0 ? -271828.0 : 314159.0;
668668
plong = PyLong_FromDouble(v);
669669
if (plong == NULL)
670670
return -1;

0 commit comments

Comments
 (0)