Skip to content

Commit 146ba87

Browse files
committed
test extreme bigger int + mpmath quad
1 parent b26924a commit 146ba87

1 file changed

Lines changed: 29 additions & 8 deletions

File tree

quaddtype/tests/test_quaddtype.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5258,7 +5258,7 @@ def test_hash_matches_float(self, value):
52585258
float_val = float(value)
52595259
assert hash(quad_val) == hash(float_val)
52605260

5261-
@pytest.mark.parametrize("value", [0.1, 0.3, 0.7, 1.1, 2.3])
5261+
@pytest.mark.parametrize("value", [0.1, 0.3, 0.7, 1.1, 2.3, 1e300, 1e-300])
52625262
def test_hash_matches_float_from_float(self, value):
52635263
"""Test that QuadPrecision created from float has same hash as that float.
52645264
@@ -5332,15 +5332,36 @@ def test_hash_dict_cross_type_lookup(self):
53325332
assert d.get(1.0) == "one"
53335333

53345334
@pytest.mark.parametrize("value", [
5335-
"1e-100", "-1e-100",
5336-
"1e100", "-1e100",
5337-
"1e-300", "-1e-300",
5335+
# Powers of 2 outside double range but within quad range
5336+
# Double max exponent is ~1024, quad max is ~16384
5337+
2**1100, 2**2000, 2**5000, 2**10000,
5338+
-(2**1100), -(2**2000),
5339+
# Small powers of 2 (subnormal in double, normal in quad)
5340+
2**(-1100), 2**(-2000),
53385341
])
5339-
def test_hash_extreme_values(self, value):
5340-
"""Test hash works for extreme values without errors."""
5342+
def test_hash_extreme_integers_outside_double_range(self, value):
5343+
"""Test hash matches Python int for values outside double range.
5344+
5345+
We use powers of 2 which are exactly representable in quad precision.
5346+
Since these integers are exact, hash(QuadPrecision(x)) must equal hash(x).
5347+
"""
5348+
quad_val = QuadPrecision(value)
5349+
assert hash(quad_val) == hash(value)
5350+
5351+
@pytest.mark.parametrize("value", [
5352+
"1e500", "-1e500", "1e1000", "-1e1000", "1e-500", "-1e-500",
5353+
"1.23456789e500", "-9.87654321e-600",
5354+
])
5355+
def test_hash_matches_mpmath(self, value):
5356+
"""Test hash matches mpmath at quad precision (113 bits).
5357+
5358+
mpmath with 113-bit precision represents the same value as QuadPrecision,
5359+
so their hashes must match.
5360+
"""
5361+
mp.prec = 113
53415362
quad_val = QuadPrecision(value)
5342-
h = hash(quad_val)
5343-
assert isinstance(h, int)
5363+
mpf_val = mp.mpf(value)
5364+
assert hash(quad_val) == hash(mpf_val)
53445365

53455366
@pytest.mark.parametrize("backend", ["sleef", "longdouble"])
53465367
def test_hash_backends(self, backend):

0 commit comments

Comments
 (0)