-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Inconsistent behavior in casting from float64 to uint32 depending on processors #16073
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
In general, I believe that Numpy defaults to C casting - though I couldn't immediately find a definitive document for this. Differences in C casting between platforms explains some differences. #include <stdio.h>
#include <stdint.h>
int main(int argc, const char *argv[])
{
double dinputs[] = {1, 2, 3, 4, -1, -2, -3, -4};
double dval;
int i;
for (i=0; i < 8; i++) {
dval = dinputs[i];
printf("Double input %0.2f; UInt32 output %u\n", dval, (uint32_t) dval);
}
return 0;
} On a 32-bit Raspbian:
On macOS:
I haven't got a 64-bit ARM to play with, and I can't explain the difference between |
Thank you for the response. Your code prints the same result as 32-bit Raspbian on AArch64. I found that, to convert double to uint32, arm32 uses After googling, I found that the C specification says in §6.4.1.4:
So both the conversion results are C11-specifically correct since the behavior when converting less-than-or-equal-to- Though the second issue is still weird to me, it can be said that the both issues are resolved in terms of the C specification, so closing now. Thanks again for the response. |
§6.3.1.4 sigh. |
Casting from float64 to uint32 behaves inconsistently depending on processors and bit sizes. The code I executed is:
On Raspbian buster armhf (32-bit), Python 3.7.3, Numpy 1.18.1:
On Debian sid arm64, Python 3.8.2, Numpy 1.18.3:
On macOS (64-bit), Python 3.8.2, Numpy 1.18.2:
The issues are:
np.float64(...).astype(np.uint32)
andnp.array(..., dtype=np.float64).astype(np.uint32)
should be the same.Are these expected? Thanks.
The text was updated successfully, but these errors were encountered: