This is a conscious design decision by the numpy
authors. When deciding on the resulting type, only the types of the operands are considered, not their actual values. And for the operation you perform, there is a risk of having a result outside the valid range, e.g. if you subtract a very large uint64
number, the result would not fit in an int64
. The safe selection is thus to convert to float64
, which certainly will fit the result (possibly with reduced precision, though).
Compare with an example of x = np.int32(2) - np.uint32(1)
. This can always be safely represented as an int64
, therefore that type is chosen. The same would be true for x = np.int64(2) - np.uint32(1)
. This will also yield an int64
.
The alternative would be to follow e.g. the c rules, which would cast everything to uint64
. But that could, of course, lead to very strange results with over/underflows.
If you want to know ahead of time what type you will end up with, look into np.result_type()
, np.can_cast()
, and np.promote_types()
. Reading about this in the docs might also help you understand the issue a bit better.