Issue
I'm developing some number-crunching code in the Spyder 3.2.4 IDE and as I check the results, I'm perplexed.
The function code is
def dictWords(dataLength):
maxLength = 1.
sumLengths = 2.
totalWords = 2.
while sumLengths < dataLength:
maxLength += 1
sumLengths = 2*(maxLength*2**maxLength - 2**maxLength + 1)
extraLength = sumLengths - dataLength
totalWords = 2**(maxLength+1) - 2
extraWords = np.ceil(extraLength/maxLength)
finalWords = totalWords - extraWords
return finalWords
When I call it from the IPython console by dl75a = BA.dictWords(75)
, I get an int
value of 25
for dl75a. But that is not the value I expect. If I tell the console dl75b = BA.dictWords(75.)
, then I get a float
value of 24.25
- still not right. (I was expecting only integer values, with extraWords
being the only fraction and going through np.ceil
, and I read on another question that float
, which I expect maxLength = 1.
to generate, is automatically 64 bits.)
However, if I put some script dl75 = dictWords(75)
in the .py file that contains the function code and run it, then I get the float64 of 24.0 that I expected. It looks like the problem might have something to do with IPython, and could be sidestepped by calling the function from within a script and running it. But, if I try calling the function from a script in a separate file, then I get the int
value of 25
again, so clearly the problem is not with IPython, but with calling the function from outside the file containing it.
One thought that comes to mind is that Numpy is imported at the top of the module, as np
, and maybe that makes a difference. However, I tried adding import numpy as np
as the first line of the function definition and it made no difference - the calls from the other script and from IPython still produce the int
of 25
and the float
of 24.25
.
Where is the problem, causing the same function code to be interpreted differently depending on whether it is from within the .py file that contains the def
or not?
Solution
After nothing worked on the evening I posted this question, I turned my computer off, shutting Spyder down in the process. Starting it again today, I tried the advice from the comments again, and found it worked! I rebuked myself for not trying it again without the advice first, so I closed Spyder, started it up again, and found that there was no longer any trouble; everything was running as expected.
Conclusion: If different parts of an IDE run the same code in different manners, it might be a problem with the current session. Close the IDE and open it again, or restart the computer and open the IDE again. If the same problem happens again, see if there is an update you can install, that might have the bug fixed.
Answered By - Post169
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.