Opened 3 years ago

Closed 16 months ago

#29009 closed defect (fixed)

Error using mpmath (python 3 version)

Reported by: Vatuzov Alexander Owned by:
Priority: major Milestone: sage-9.4
Component: python3 Keywords: mpmath, py3, python3
Cc: Dima Pasechnik Merged in:
Authors: Sebastian Oehms Reviewers: Dima Pasechnik
Report Upstream: Reported upstream. No feedback yet. Work issues:
Branch: e7d6c32 (Commits, GitHub, GitLab) Commit: e7d6c32d5a7d76ef7e337978a9db971d7fd1e89a
Dependencies: Stopgaps:

Status badges

Description

I have a problem with sage on python3, example sage8.9 and 9.0

simple code:

import mpmath
mpmath.mp.prec = 1000
mpmath.findroot(lambda x: x^2 - 3, 2)

______________________

OverflowError: Python int too large to convert to C long

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "sage/libs/mpmath/ext_main.pyx", line 1691, in sage.libs.mpmath.ext_main.mpf_base.__repr__ (build/cythonized/sage/libs/mpmath/ext_main.c:21256)
    return "mpf('%s')" % to_str(self._mpf_, n)
  File "/home/nouret/SageMath/local/lib/python3.7/site-packages/mpmath/libmp/libmpf.py", line 1242, in to_str
    sign, digits, exponent = to_digits_exp(s, dps+3)
  File "/home/nouret/SageMath/local/lib/python3.7/site-packages/mpmath/libmp/libmpf.py", line 1200, in to_digits_exp
    digits = numeral(sd, base=10, size=dps)
  File "/home/nouret/SageMath/local/lib/python3.7/site-packages/mpmath/libmp/libintmath.py", line 158, in numeral_python
    A, B = divmod(n, base**half)
SystemError: <built-in function divmod> returned a result with an error set

Problem is on File ".../SageMath/local/lib/python3.7/site-packages/mpmath/libmp/libintmath.py", line 161, in numeral_python

and using

A, B = n // (base**half), n % (base**half)

instead of

A, B = divmod(n, base**half)

solves this problem

Change History (15)

comment:1 Changed 3 years ago by Eric Gourgoulhon

Keywords: py3 python3 added
Summary: error using mpmathError using mpmath (python 3 version)

comment:2 Changed 3 years ago by Eric Gourgoulhon

As pointed out in this sage-support thread, everything is fine with the Python 2 version of Sage 9.1.beta0.

Last edited 3 years ago by Eric Gourgoulhon (previous) (diff)

comment:3 Changed 3 years ago by Paul Masson

Report Upstream: N/AReported upstream. No feedback yet.

comment:4 Changed 2 years ago by Matthias Köppe

Milestone: sage-9.1sage-9.2

Moving tickets to milestone sage-9.2 based on a review of last modification date, branch status, and severity.

comment:5 Changed 2 years ago by Matthias Köppe

Cc: Dima Pasechnik added

comment:6 Changed 2 years ago by Matthias Köppe

Milestone: sage-9.2sage-9.3

comment:7 Changed 18 months ago by Sebastian Oehms

Branch: u/soehms/mpmath_divmod_29009

comment:8 Changed 18 months ago by Sebastian Oehms

Authors: Sebastian Oehms
Commit: ea877d9ee384bfa2794af3c20a070e7e42bb3f7f
Status: newneeds_review

The bug reported here is located in the Sage integer class, more precisely in the method quo_rem which is invoked from the code in numeral_python. Note that it disappears if you deactivate the mpmath sage backend, that means to call os.environ['MPMATH_NOSAGE'] ='Y' before you import mpmath.

The bug can be reproduced independently of mpmath in sage by the following example:

sage: divmod(1, sys.maxsize+1r)
---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
OverflowError: Python int too large to convert to C long

The above exception was the direct cause of the following exception:

SystemError                               Traceback (most recent call last)
<ipython-input-1-292a3012ef4a> in <module>
----> 1 divmod(Integer(1), sys.maxsize+1)

SystemError: <built-in function divmod> returned a result with an error set

The error occurs if the first argument is a sage integer and the second a python int of size larger than sys.maxsize in the statement d = PyInt_AS_LONG(other). Therefore, I'm suggesting the following fix:

  • src/sage/rings/integer.pyx

    diff --git a/src/sage/rings/integer.pyx b/src/sage/rings/integer.pyx
    index a2f913df7f..a268f29355 100644
    a b cdef class Integer(sage.structure.element.EuclideanDomainElement): 
    34683468            sage: 5.quo_rem(2/3)
    34693469            (15/2, 0)
    34703470
     3471        Check that :trac:`29009` is fixed:
     3472
     3473            sage: divmod(1, sys.maxsize+1r)
     3474            (0, 1)
     3475            sage: import mpmath
     3476            sage: mpmath.mp.prec = 1000
     3477            sage: root = mpmath.findroot(lambda x: x^2 - 3, 2)
     3478            sage: len(str(root))
     3479            301
    34713480        """
    34723481        cdef Integer q = PY_NEW(Integer)
    34733482        cdef Integer r = PY_NEW(Integer)
    34743483        cdef long d, res
    34753484
    3476         if type(other) is int:
     3485        if is_small_python_int(other):
    34773486            d = PyInt_AS_LONG(other)
    34783487            if d > 0:
    34793488                mpz_fdiv_qr_ui(q.value, r.value, self.value, d)

I suspect that there are some other places of code using PyInt_AS_LONG where a similar fix will be necessary to have them work with python3 for large python int. Shall I open a ticket for that?


New commits:

ea877d929009: initial version
Last edited 18 months ago by Sebastian Oehms (previous) (diff)

comment:9 Changed 18 months ago by Dima Pasechnik

What is the purpose of divmod() call in the doctest?

comment:10 in reply to:  9 Changed 18 months ago by Sebastian Oehms

Replying to dimpase:

What is the purpose of divmod() call in the doctest?

I added this for ease of reading as it gets much closer to the problem. If you want to save tests, I can remove it (or better the other one?).

comment:11 Changed 18 months ago by Dima Pasechnik

please add a comment describing divmod() meaning (imagine someone who didn't read details of this ticket seeing it - it looks like a typo).

something like # sanity check - see #29009 for details will do, if you cannot think of a better one.

comment:12 Changed 18 months ago by git

Commit: ea877d9ee384bfa2794af3c20a070e7e42bb3f7fe7d6c32d5a7d76ef7e337978a9db971d7fd1e89a

Branch pushed to git repo; I updated commit sha1. New commits:

e7d6c3229009: add comment to doctest

comment:13 Changed 18 months ago by Dima Pasechnik

Milestone: sage-9.3sage-9.4
Reviewers: Dima Pasechnik
Status: needs_reviewpositive_review

comment:14 Changed 18 months ago by Sebastian Oehms

Thanks! There is another similar one: #31676

comment:15 Changed 16 months ago by Volker Braun

Branch: u/soehms/mpmath_divmod_29009e7d6c32d5a7d76ef7e337978a9db971d7fd1e89a
Resolution: fixed
Status: positive_reviewclosed
Note: See TracTickets for help on using tickets.