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

Skip to content

fix conversion of large integers #426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 10, 2023
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
2 changes: 2 additions & 0 deletions symengine/lib/symengine.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ cdef extern from 'symengine/mp_class.h' namespace "SymEngine":
integer_class(const string &s) except +
mpz_t get_mpz_t(integer_class &a)
const mpz_t get_mpz_t(const integer_class &a)
string mp_get_hex_str(const integer_class &a)
void mp_set_str(integer_class &a, const string &s)
cdef cppclass rational_class:
rational_class()
rational_class(mpq_t)
Expand Down
18 changes: 10 additions & 8 deletions symengine/lib/symengine_wrapper.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1685,10 +1685,10 @@ class Rational(Number):
return c2py(<rcp_const_basic>symengine.Rational.from_two_ints(p_, q_));
except OverflowError:
# Too big, need to use mpz
tmp = str(p).encode("utf-8")
p__ = symengine.integer_class(tmp)
tmp = str(q).encode("utf-8")
q__ = symengine.integer_class(tmp)
tmp = hex(p).encode("utf-8")
symengine.mp_set_str(p__, tmp)
tmp = hex(q).encode("utf-8")
symengine.mp_set_str(q__, tmp)
return c2py(<rcp_const_basic>symengine.Integer(p__).divint(symengine.Integer(q__)))

@property
Expand Down Expand Up @@ -1756,8 +1756,8 @@ class Integer(Rational):
except OverflowError:
# Too big, need to use mpz
int_ok = False
tmp = str(i).encode("utf-8")
i__ = symengine.integer_class(tmp)
tmp = hex(i).encode("utf-8")
symengine.mp_set_str(i__, tmp)
# Note: all other exceptions are left intact
if int_ok:
return c2py(<rcp_const_basic>symengine.integer(i_))
Expand Down Expand Up @@ -1817,7 +1817,7 @@ class Integer(Rational):

def _sympy_(Basic self):
import sympy
return sympy.Integer(deref(self.thisptr).__str__().decode("utf-8"))
return sympy.Integer(int(self))

def _sage_(Basic self):
try:
Expand All @@ -1828,7 +1828,9 @@ class Integer(Rational):
return sage.Integer(str(self))

def __int__(Basic self):
return int(str(self))
cdef string s = symengine.mp_get_hex_str(
deref(symengine.rcp_static_cast_Integer(self.thisptr)).as_integer_class())
return int(s.decode("utf-8"), base=16)

@property
def p(self):
Expand Down
9 changes: 9 additions & 0 deletions symengine/tests/test_sympy_conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,3 +806,12 @@ def test_conv_doubles():
assert sympify(a._sympy_()) == a
assert float(a) == f
assert float(a._sympy_()) == f

def test_conv_large_integers():
a = Integer(10)**10000
# check that convert to python int does not throw
b = int(a)
# check that convert to sympy int does not throw
if have_sympy:
c = a._sympy_()
d = sympify(c)
2 changes: 1 addition & 1 deletion symengine_version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.9.0
5a9d4fa8c769b2dc8d7904ec16d7ea76328c4565