[Numpy-discussion] floor with dtype
Renato Fabbri
renato.fabbri at gmail.com
Mon Sep 25 07:23:19 EDT 2017
I have an array of floats and
want their floor values as integers.
(e.g. to use them as indexes for a table lookup)
It seems reasonable to assume this is
a frequent use of floor.
Anyway, you gave me a better vay to do it:
>>> myints = n.floor(myarray).astype(n.int)
On Mon, Sep 25, 2017 at 7:23 AM, Thomas Jollans <tjol at tjol.eu> wrote:
> On 2017-09-25 10:59, Renato Fabbri wrote:
> > """
> > In [3]: n.floor(n.linspace(0,5,7), dtype=n.int <http://n.int>)
> > ------------------------------------------------------------
> ---------------
> > TypeError Traceback (most recent call
> last)
> > <ipython-input-3-1471f5d738e8> in <module>()
> > ----> 1 n.floor(n.linspace(0,5,7), dtype=n.int <http://n.int>)
> >
> > TypeError: No loop matching the specified signature and casting
> > was found for ufunc floor
> >
> > In [4]: n.__version__
> > Out[4]: '1.11.0'
> > """
> >
> > Is this the expected behavior?
>
> Yes. There is no floor function for integers.
>
> The dtype argument specified not only the return type, but the type the
> calculation is done in as well. floor() only exists, and only makes
> sense, for floats. (You can use floor(a, dtype='f4') and so on to insist
> on floats of a different width)
>
> If you have some floats, and you want to get their floor as integers,
> you'll have to cast. In that case, in actual fact, there is little
> reason to use floor at all:
>
> In [2]: np.arange(1.9, 11.)
> Out[2]: array([ 1.9, 2.9, 3.9, 4.9, 5.9, 6.9, 7.9, 8.9,
> 9.9, 10.9])
>
> In [3]: np.arange(1.9, 11.).astype('i8')
> Out[3]: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
>
>
> >
> > I am doing:
> >>>> myints = n.array(n.floor(myarray), dtype=n.int <http://n.int>)
> > to get the integers.
> >
> > tx.
> > R.
> >
> >
> > --
> > Renato Fabbri
> > GNU/Linux User #479299
> > labmacambira.sourceforge.net <http://labmacambira.sourceforge.net>
> >
> >
> > _______________________________________________
> > NumPy-Discussion mailing list
> > NumPy-Discussion at python.org
> > https://mail.python.org/mailman/listinfo/numpy-discussion
> >
>
>
> --
> Thomas Jollans
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
>
--
Renato Fabbri
GNU/Linux User #479299
labmacambira.sourceforge.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20170925/5b834405/attachment.html>
More information about the NumPy-Discussion
mailing list