7

I create a simple numpy of data type uint as below:

import numpy as np
a = np.array([1,2,3], dtype=np.uint)

When I compute

a[0] + 1

I expect a result of 2, but it gives

2.0

Why there is such an implicit conversion to float for np.uint? Note that it does not happen with int or np.int64

1

1 Answer 1

1

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.