Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy sum of unsigned and signed number becomes floating point

Tags:

python

numpy

Why the answer is floating point? I thought it should be an integer.

>>> import numpy as np
>>> np.uint64(12)
12
>>> np.uint64(12) + int(4)
16.0
>>> 
like image 691
hamster on wheels Avatar asked Dec 05 '25 20:12

hamster on wheels


1 Answers

Based on a recent reddit post.

This is the way Numpy functions are designed to return consistent values for the whole range of input data.

Both 16-bit unsigned and signed integers would fit into int32, therefore

>>> np.uint16(4) + np.int16(5)
9

However for uint64 and int64 the smallest datatype that would fit all possible values is float64.

like image 165
Dmitri Chubarov Avatar answered Dec 08 '25 08:12

Dmitri Chubarov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!