[Numpy-discussion] how to round to int (I mean 5.2 to 5 not 5.0)

Robert Kern robert.kern at gmail.com
Tue Mar 6 23:53:54 EST 2007


Sebastian Haase wrote:
> Hi,
> why does
> numpy.round(a)
> return a float ?

Partly because that's what the underlying C standard library function does and
what the Python function round() does. The reason they do that is because the
range of integers supported by the double precision floating point type is
larger than the range of integers supported by C ints.

In [17]: numpy.round(1e16)
Out[17]: 1e+16

In [18]: numpy.round(1e16).astype(int)
Out[18]: -2147483648

> I need something that I can use as indices for another array. Do I
> have to (implicitly) create a temporary array  and use:
> N.round(a).astype(N.int)  ?

Yup.

-- 
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