Skip to content
Open
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
49 changes: 31 additions & 18 deletions src/flint/types/arb.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ cimport cython

ctx = thectx


def _arb_from_pickle(mid, rad):
if isinstance(mid, tuple):
mid = tuple(int(x, 16) for x in mid)
mid = tuple(int.from_bytes(x, 'big', signed=True) for x in mid)
if isinstance(rad, tuple):
rad = tuple(int(x, 16) for x in rad)
rad = tuple(int.from_bytes(x, 'big', signed=True) for x in rad)
return arb(mid, rad)


# cdef _str_trunc(s, trunc=0):
# if trunc > 0 and len(s) > 3 * trunc:
# left = right = trunc
Expand Down Expand Up @@ -415,22 +413,37 @@ cdef class arb(flint_scalar):
arf_set_mag(rad.val, arb_radref(self.val))
return (mid, rad)

def __reduce__(self):
# Hex avoids the decimal digit limit in text pickle protocols.
def __reduce_ex__(self, protocol: int):
mid, rad = self._to_arfs()
if mid.is_finite():
mid = tuple(hex(x) for x in mid.man_exp())
elif mid.is_nan():
mid = "nan"
elif mid.is_pos_inf():
mid = "inf"
else:
mid = "-inf"
if rad.is_finite():
rad = tuple(hex(x) for x in rad.man_exp())

if protocol < 2: # protocols 0 and 1 pickle ints inefficiently

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't overcomplicate things. It doesn't matter if protocols 0 and 1 are inefficient.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall i remove the comment?

if mid.is_finite():
mid = tuple(int(x).to_bytes((x.bit_length()+8)//8, 'big', signed=True) for x in mid.man_exp())
elif mid.is_nan():
mid = "nan"
elif mid.is_pos_inf():
mid = "inf"
else:
mid = "-inf"
if rad.is_finite():
rad = tuple(int(x).to_bytes((x.bit_length()+8)//8, 'big', signed=True) for x in rad.man_exp())
else:
rad = "inf"
return (_arb_from_pickle, (mid, rad))
else:
rad = "inf"
return (_arb_from_pickle, (mid, rad))
if mid.is_finite():
mid = tuple(map(int, mid.man_exp()))
elif mid.is_nan():
mid = "nan"
elif mid.is_pos_inf():
mid = "inf"
else:
mid = "-inf"
if rad.is_finite():
rad = tuple(map(int, rad.man_exp()))
else:
rad = "inf"
return (arb, (mid, rad))

def repr(self):
mid, rad = self._to_arfs()
Expand Down