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

Robert Kern robert.kern at gmail.com
Thu Jan 6 12:11:20 EST 2011


On Thu, Jan 6, 2011 at 04:14,  <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])

This is behavior inherited from C and matches Python's behavior.
int(aa[0]) == 0. Similarly, inside the C code, (int)(1.0 - 2.22e-16)
== 0.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco



More information about the NumPy-Discussion mailing list