[Numpy-discussion] Casting and promotion rules (e.g. int + uint64 => float)
Fri Mar 8 10:23:33 CST 2013
I have noticed that numpy introduces some unexpected type casts, that are
in some cases problematic.
A very weird cast is
int + uint64 -> float
for instance, consider the following snippet:
import numpy as np
this cast is quite different from what other programming languages (e.g., C)
would do in this case, so it already comes unexpected.
Furthermore, an int64 (or an uint64) is too large to fit into a float,
hence this automatic conversion also results in data loss! For instance
-> 18446744073709551615 # CORRECT!
# Actually 1.84467440737095516160e+19 - LOSS OF DATA
Weird, isn't it?
Another issue is that variables unexpectedly change type with accumulation
now a is float
I believe that some casting/promotion rules should be revised, since they
now lead to difficult to catch, intermittent errors. In case this cannot
be done immediately, I suggest at least documenting these promotions,
providing examples on how to code many conventional tasks. E.g.,
incrementing an integer of unknown size
I have also reported this in https://github.com/numpy/numpy/issues/3118
More information about the NumPy-Discussion