-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
astype(int)
silently returns wrong answer under Windows
#17640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Your example is not the same as your error message - the test uses |
|
Sorry about that, I was typing the message from outside the VM where I got it. Very sloppy on my part. I updated the OP with a proper example.
Thanks for the info. |
Python 2 had two types of integer: int (C long) and long (unbounded precision). Python 3 only kept the second. |
Is there a workaround for this? |
Since the issue is caused by an overflow of |
Thanks, BvB93. Is there a way to map it globally? I have a codebase that works on linux with astype(int) all over it; now I have a Windows developer. Before I consider changing it everywhere it exists, I'm wondering if there's a solution that spares needing to take that route. |
I think you might be out of luck here. As far as I'm aware there is no way of manually setting the default integer type to something different from what is specified by the platform in question. |
There has been some discussion about changing the default type. I believe the use of c_long is inherited from early python. |
I had a PR once to make it That was mainly for discussion, but I am more and more seeing a NumPy 2.0 in any case, and this might be important enough to fold in if it happens. (Even if I would prefer to not do too many of such changes at once and rather make this type of "breaking a bit" releases every few years so that the number of such changes are small each time.) |
Is this done correctly in the NEP 47 array API? |
@asmeurer can you check this? From what I remember of the pass I did just today, it probably isn't. |
I think this issue is relevant data-apis/array-api#151 |
@asmeurer the point is that the standard probably wants a well defined "default integer"? But NumPy's default integer is currently not well defined:
If we add a new, clean namespace we should try to make good use of it and fix both of these. (unless we are sure we fix both of them in NumPy proper, which I would like to try but is more difficult.) |
I actually mean this change specifically https://github.com/data-apis/array-api/pull/167/files#diff-7e75cfe3133de16126433bc962f9fe14f216bd682e3870efa965bab40d08322fR9. Based on what is there, I guess we should make the "default" integer dtype int64 on 64-bit Windows. Note that in the array API itself the default integer dtype is only relevant in a couple of places. |
@asmeurer good that it is spelled out nicely! But your PR does not conform to this for |
You mean specifically on Windows? Or does asarray also use some value based casting in some cases? What is an abstract DType? If there is some trick that would avoid having to reimplement asarray, that would be ideal. |
This part is not windows specific.
A DType class as per NEP 42. That could customize the dtype discovery during array coercion as per the NEP. But, it we may have to fix casting to/for abstract DTypes first. (Shouldn't be super hard, it should have an additional "common DType" based path, I think – that is probably already mentioned in NEP 42.) |
In my codebase this is exactly what I have done. Explicit is better than implicit after all. No problems changing over, no problems after. Would recommend! |
Closing, we are trying to switch to 64bit on 64bi tplatforms for NumPy 2.0. See the dev release notes for example. Which will change the default on windows specifically. |
Note that for now, NumPy will still happily return uints and object for out of bound ints, this also doesn't affect 32bit platforms, just because it would be a bigger, more difficult to deal with change. |
Casting an array of float64 to int using
astype
with as argumentint
will yield the value -2147483648 under windows.Reproducing code example:
I expect this test to go through, but it fails with the following message:
Using dtypes
np.int64
works fine, alreadynp.int32
returns the wrong answer (although that's 0 and the above result occurs only with the builtinint
ornp.int
).NumPy/Python version information:
Numpy version: 1.19.1
System version: '3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]'
Bug is taking place under Windows 10. Cannot reproduce on my Arch system with numpy 1.19.2 and python 3.8.5 built using GCC.
The text was updated successfully, but these errors were encountered: