Opened 6 years ago

Last modified 5 weeks ago

#22834 new defect

infinite recursion in sage.rings.complex_interval.ComplexIntervalFieldElement.__invert__

Reported by: wonder Owned by:
Priority: major Milestone:
Component: basic arithmetic Keywords:
Cc: mmarco Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Status badges

Description (last modified by jdemeyer)

Division by zero in CIF leads to infinite recursion:

sage: ~CIF(RIF(-1,1))
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-4-7c8464c26127> in <module>()
----> 1 ~CIF(RIF(-Integer(1),Integer(1)))

/usr/local/src/sage-config/src/sage/rings/complex_interval.pyx in sage.rings.complex_interval.ComplexIntervalFieldElement.__invert__ (build/cythonized/sage/rings/complex_interval.c:13416)()
   1290             return -I*(-I*self).__invert__()
   1291         elif mpfr_sgn(a) <= 0 and mpfr_sgn(d) <= 0: # thirthd quadrant or between thirthd and fourth
-> 1292             return -(-self).__invert__()
   1293         elif mpfr_sgn(a) >=0: #fourth or between fourth and first
   1294             I = self.parent().gen(0)

... last 1 frames repeated, from the frame below ...

/usr/local/src/sage-config/src/sage/rings/complex_interval.pyx in sage.rings.complex_interval.ComplexIntervalFieldElement.__invert__ (build/cythonized/sage/rings/complex_interval.c:13416)()
   1290             return -I*(-I*self).__invert__()
   1291         elif mpfr_sgn(a) <= 0 and mpfr_sgn(d) <= 0: # thirthd quadrant or between thirthd and fourth
-> 1292             return -(-self).__invert__()
   1293         elif mpfr_sgn(a) >=0: #fourth or between fourth and first
   1294             I = self.parent().gen(0)

RuntimeError: maximum recursion depth exceeded while calling a Python object

The bug appears to trace to this code at sage/rings/complex_interval.pyx, line 1287 (SageMath version 7.3, Release Date: 2016-08-04):

        elif mpfr_sgn(a) <= 0 and mpfr_sgn(d) <= 0: # thirthd quadrant or between thirthd and fourth
            return -(-self).__invert__()

which looks like it can fail in that way if the signs are zero.

Minor additional comment: foo.__invert__() should be replaced by ~foo which is faster.

Change History (6)

comment:1 Changed 6 years ago by wonder

Calling N() before comparing to zero seems to be a usable workaround

comment:2 Changed 6 years ago by jdemeyer

Do you have a complete example?

comment:3 Changed 6 years ago by jdemeyer

Cc: mmarco added
Component: number fieldsbasic arithmetic
Description: modified (diff)
Milestone: sage-8.0

comment:4 Changed 6 years ago by jdemeyer

Description: modified (diff)

comment:5 Changed 3 years ago by chapoton

Works now

┌────────────────────────────────────────────────────────────────────┐
│ SageMath version 9.1.rc4, Release Date: 2020-05-09                 │
│ Using Python 3.7.3. Type "help()" for help.                        │
└────────────────────────────────────────────────────────────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Warning: this is a prerelease version, and it may be unstable.     ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
sage: ~CIF(RIF(-1,1))
[.. NaN ..] + [.. NaN ..]*I

comment:6 Changed 5 weeks ago by mkoeppe

Milestone: sage-8.0
Note: See TracTickets for help on using tickets.