Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 30 additions & 33 deletions src/flint/test/test_arb.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,37 +110,35 @@ def test_contains():
]:
assert (x in y) == expected

# TODO: Re-enable this if we ever add the ability to hash arbs.
# def test_hash():
# """`x` and `y` hash to the same value if they have the same midpoint and radius.

# Args:
# x: An arb.
# y: An arb.
# expected: Whether `x` and `y` should hash to the same value.
# """
# def arb_pi(prec):
# """Helper to calculate arb to a given precision."""
# with ctx.workprec(prec):
# return arb.pi()
# for x, y, expected in [
# (arb(10), arb(10), True),
# (arb(10), arb(11), False),
# (arb(10.0), arb(10), True),
# (
# arb(mid=10, rad=2),
# arb(mid=10, rad=2),
# True,
# ),
# (
# arb(mid=10, rad=2),
# arb(mid=10, rad=3),
# False,
# ),
# (arb_pi(100), arb_pi(100), True),
# (arb_pi(100), arb_pi(1000), False),
# ]:
# assert (hash(x) == hash(y)) == expected
def test_hash():
"""`x` and `y` hash to the same value if they have the same midpoint and radius.

Args:
x: An arb.
y: An arb.
expected: Whether `x` and `y` should hash to the same value.
"""
def arb_pi(prec):
"""Helper to calculate arb to a given precision."""
with ctx.workprec(prec):
return arb.pi()
for x, y, expected in [
(arb(10), arb(10), True),
(arb(10), arb(11), False),
(arb(10.0), arb(10), True),
]:
assert (hash(x) == hash(y)) == expected

for x in [
arb(mid=10, rad=2),
arb_pi(100),
]:
try:
hash(x)
except ValueError:
pass
else:
assert False, f"Expected {x} to raise an error if hashed, but succeeded."



Expand Down Expand Up @@ -338,8 +336,7 @@ def test_no_tests_missing():
test_lower,
test_upper,
test_contains,
# TODO: Re-enable this if we ever add the ability to hash arbs.
# test_hash,
test_hash,
test_arb_sub,
test_arb_add,
test_arb_mul,
Expand Down
6 changes: 6 additions & 0 deletions src/flint/types/arb.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,12 @@ cdef class arb(flint_scalar):
arb_clear(tval)
return res

def __hash__(self):
"""Hash."""
if self.is_exact():
return hash((self.mid().man_exp(), self.rad().man_exp()))
raise ValueError(f"Cannot hash non-exact arb: {self}. See pull/341 for details.")

def __contains__(self, other):
other = any_as_arb(other)
return arb_contains(self.val, (<arb>other).val)
Expand Down
Loading