0

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?

Drise
  • 4,310
  • 5
  • 41
  • 66
Post169
  • 668
  • 8
  • 26
  • I think you need to put `from __future__ import division` before any other import in your file to get floating point divisions when using `a / b` in Python 2. You don't need to do this in Python 3 because there all divisions are floating point ones. – Carlos Cordoba Mar 13 '18 at 23:02
  • @CarlosCordoba Thanks, I just tried that but nothing changes. I still only get the right answer when the `dictWords` function is called from within the same .py file. – Post169 Mar 14 '18 at 01:37
  • Then you need to evaluate `from __future__ import division` in the console too. – Carlos Cordoba Mar 14 '18 at 12:38
  • @CarlosCordoba Thank you. I tried importing to the separate script and that did nothing, though I didn't import it to the console. – Post169 Mar 16 '18 at 18:03

1 Answers1

1

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.

Post169
  • 668
  • 8
  • 26