I found the following bug in my code (attached in the image file). When multiplying the array by the variables without parentheses, it gives a weird result (first result). When placing the parentheses, the result (second result) is fine. It doesn't happen with other python versions. I have installed python 3.9. Carla.
Hi Carla, Le 2021-02-24 à 19:46, Carla Molina a écrit :
When multiplying the array by the variables without parentheses, it gives a weird result (first result). When placing the parentheses, the result (second result) is fine. It doesn't happen with other python versions. I have installed python 3.9.
You're hitting an integer overflow. Numpy works with platform dependent data types, in your case numpy used 32 bits signed integers to represent the values in your array. It's not pure Python land which uses magic to avoid integer overflows, it's fast computation land with hardware limitations. by doing 100 * 60_461_826 first, you're slighly overflowing the 31 bits used to represent the value, overwriting the sign bit and giving you the negative value you get. By doing the division first you're forcing numpy to use floats instead of integers, which have a wider range (and you're working on smaller values too, so you'll not hit the overflow anyway). By using: np.array([1, 50, 100, 150, 200, 250, 300], dtype=np.float64) or: np.array([1, 50, 100, 150, 200, 250, 300], dtype=np.int64) you're explicit about the memory requirement of your computations, and it goes OK. As this is the Python documentation mailing list, I have to tell you it's not the right place to post numpy issues, this mailing list is dedicated to https://docs.python.org, only. If you find an issue with numpy, please report it on their bug tracker: https://github.com/numpy/numpy/issues. Bests, -- [Julien Palard](https://mdk.fr)
participants (2)
-
Carla Molina
-
Julien Palard