2

The following lttle program fails using Python 3.8, but is OK under 2.7:

from mpmath import mpf, nsum

def z(n):
    x = [mpf(1) for k in range(1,n)]
    return 99

print (nsum(z, [2,2]))

The code looks odd, as it is pared down from a rather larger program. I can't pare it down any further. This can be confirmed easily using the interactive shell at https://www.python.org/shell/

The error report is:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.8/site-packages/mpmath/calculus/extrapolation.py", line 1698, in nsum
    return +g()
  File "/usr/lib/python3.8/site-packages/mpmath/calculus/extrapolation.py", line 1745, in <lambda>
    return False, lambda: f(*([0]*len(intervals)))
  File "/usr/lib/python3.8/site-packages/mpmath/calculus/extrapolation.py", line 1777, in g
    s += f(*args)
  File "<stdin>", line 2, in z
TypeError: 'mpf' object cannot be interpreted as an integer

Have I missed something obvious?

user2307360
  • 41
  • 1
  • 2

1 Answers1

0

nsum passes n as mpf to z, but range expects:

...int or any object that implements the __index__ special method.

So you could cast n explicitly to int, e.g.:

x = [mpf(1) for k in range(1, int(n))]
user7217806
  • 2,014
  • 2
  • 10
  • 12