Skip to content

Commit c24a309

Browse files
authored
Merge pull request #426 from isuruf/conv_big_int
fix conversion of large integers
2 parents 7486735 + 75f0bf7 commit c24a309

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

symengine/lib/symengine.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ cdef extern from 'symengine/mp_class.h' namespace "SymEngine":
2424
integer_class(const string &s) except +
2525
mpz_t get_mpz_t(integer_class &a)
2626
const mpz_t get_mpz_t(const integer_class &a)
27+
string mp_get_hex_str(const integer_class &a)
28+
void mp_set_str(integer_class &a, const string &s)
2729
cdef cppclass rational_class:
2830
rational_class()
2931
rational_class(mpq_t)

symengine/lib/symengine_wrapper.pyx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,10 +1685,10 @@ class Rational(Number):
16851685
return c2py(<rcp_const_basic>symengine.Rational.from_two_ints(p_, q_));
16861686
except OverflowError:
16871687
# Too big, need to use mpz
1688-
tmp = str(p).encode("utf-8")
1689-
p__ = symengine.integer_class(tmp)
1690-
tmp = str(q).encode("utf-8")
1691-
q__ = symengine.integer_class(tmp)
1688+
tmp = hex(p).encode("utf-8")
1689+
symengine.mp_set_str(p__, tmp)
1690+
tmp = hex(q).encode("utf-8")
1691+
symengine.mp_set_str(q__, tmp)
16921692
return c2py(<rcp_const_basic>symengine.Integer(p__).divint(symengine.Integer(q__)))
16931693

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

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

18221822
def _sage_(Basic self):
18231823
try:
@@ -1828,7 +1828,9 @@ class Integer(Rational):
18281828
return sage.Integer(str(self))
18291829

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

18331835
@property
18341836
def p(self):

symengine/tests/test_sympy_conv.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,3 +806,12 @@ def test_conv_doubles():
806806
assert sympify(a._sympy_()) == a
807807
assert float(a) == f
808808
assert float(a._sympy_()) == f
809+
810+
def test_conv_large_integers():
811+
a = Integer(10)**10000
812+
# check that convert to python int does not throw
813+
b = int(a)
814+
# check that convert to sympy int does not throw
815+
if have_sympy:
816+
c = a._sympy_()
817+
d = sympify(c)

symengine_version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.9.0
1+
5a9d4fa8c769b2dc8d7904ec16d7ea76328c4565

0 commit comments

Comments
 (0)