[Numpy-discussion] aa.astype(int) truncates and doesn't round

Keith Goodman kwgoodman at gmail.com
Thu Jan 6 10:40:57 EST 2011


On Thu, Jan 6, 2011 at 2:14 AM,  <josef.pktd at gmail.com> wrote:
> just something I bumped into and wasn't aware of
>
>>>> aa
> array([ 1.,  1.,  1.,  1.,  1.])
>>>> aa.astype(int)
> array([0, 1, 0, 0, 0])
>>>> aa - 1
> array([ -2.22044605e-16,   2.22044605e-16,  -2.22044605e-16,
>        -3.33066907e-16,  -3.33066907e-16])
>>>> np.round(aa).astype(int)
> array([1, 1, 1, 1, 1])

>> a = np.ones(100)
>> a.astype(int)
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1])

My default numpy int is 64 bits. Try 32 bits:

>> a = np.ones(100, np.int32)
>> a.astype(int)

array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1])



More information about the NumPy-Discussion mailing list