I was able to track down this closely related github issue on the numpy repo. According to Robert Kern, a major contributor to numpy
and scipy
, the decision about the result type is made based on the input types. numpy
relies on underlying routines that have type-specific implementations where both arguments are of the same type, so it has to promote to some common type. In this case, the issue is that one type is unsigned and the other is signed:
...it's a confluence of several factors. The implementation of the
numpy.add()
ufunc that underlies these routines only has type-specific
implementations where the two arguments are of the same type. So the
ufunc system needs to cast the arguments to a common type. One of the
arguments is signed, so both arguments need to be casted to a signed
type. The smallest signed type that can represent values up through
the maximum range of uint64
is float64
(Note! Not all uint64
values
can be represented as float64
floats! Precision is lost! But it's
better than int64
where the whole upper half of the values are lost.).
Note, a similar thing occurs with unsigned and signed numpy types, np.uint
and np.int
:
>>> import numpy as np
>>> np.uint(0) + np.int64(1)
1.0