Skip to content

ellipe fails with pi as argument #604

Description

@mo271

When using ellipe with pi, there is an UnboundLocalError, while it works fine when using +pi or 1*pi.

Here's an ipython session showing the problem:

Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.20.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import mpmath

In [2]: mpmath.__version__
Out[2]: '1.2.1'

In [3]: mpmath.ellipe(mpmath.pi, 1)
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-3-cbbd0507302e> in <module>
----> 1 mpmath.ellipe(mpmath.pi, 1)

/usr/lib/python3/dist-packages/mpmath/ctx_mp_python.py in f_wrapped(ctx, *args, **kwargs)
   1019                 try:
   1020                     ctx.prec += 10
-> 1021                     retval = f(ctx, *args, **kwargs)
   1022                 finally:
   1023                     ctx.prec = prec

/usr/lib/python3/dist-packages/mpmath/functions/elliptic.py in ellipe(ctx, *args)
   1274         RD = ctx.elliprd(x, y, 1)
   1275         return s*RF, -m*s**3*RD/3
-> 1276     return ctx.sum_accurately(terms) + P
   1277 
   1278 @defun_wrapped

/usr/lib/python3/dist-packages/mpmath/ctx_base.py in sum_accurately(ctx, terms, check_step)
    369                             break
    370                     k += 1
--> 371                 cancellation = max_mag - sum_mag
    372                 if cancellation != cancellation:
    373                     break

UnboundLocalError: local variable 'sum_mag' referenced before assignment

In [4]: mpmath.ellipe(+mpmath.pi, 1)
Out[4]: mpf('1.9999999999999998')

In [5]: mpmath.ellipe(1*mpmath.pi, 1)
Out[5]: mpf('1.9999999999999998')

In [5]: mpmath.ellipe(mpmath.e, 1)
Out[5]: mpf('1.5892187094970913')

Surprisingly it works with other mpmath.ctx_mp_python.constants. I have not investigated what is going in detail.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions