Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

326
Views
strange implicit conversion of data type in numpy

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

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!