-1

Currently the following error occurs. I believe the problem is due to this code been written for python 2.X and not working in my current version of python 3.8.3

self.zz=np.zeros(self.nhalf,'f') TypeError: 'float' object cannot be interpreted as an integer

References to self.zz and self.nhalf in the program are shown below

self.nhalf=0
self.nhalf=self.num_fft/2

self.zz=[]
self.zz=self.zz[n1:n2]

I wonder is there simple adjustment required to remove the error?

6
  • 1
    From numpy docs: numpy.zeros(shape, dtype=float, order='C') shape : int or tuple of ints -- / returns a float... Commented Dec 3, 2020 at 11:50
  • 2
    Since Python 3.5(I think) the / operator does regular division, meaning the result could be a float. If you want floor division, you can use the // operator. Commented Dec 3, 2020 at 11:51
  • @jaaq correction: meaning the result is always a float Commented Dec 3, 2020 at 11:52
  • @Tomerikoo oh you're right! I never bothered to try type(4/2). Thanks, learned something new :) Commented Dec 3, 2020 at 11:55
  • 1
    @jaaq No need. The result of 4/2 is 2.0 which gives a hint of its type ^_^ Commented Dec 3, 2020 at 11:56

1 Answer 1

1

I am pretty sure that the problem is that Python2 did integer division by default while Python3.5(ish) started using regular division.

I think your error means that you are trying to create a numpy array with e.g. 17.5 zeros in it, which obviously doesn't work.

Try replacing the / operator with the // floor division operator:

self.nhalf=self.num_fft//2
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.