2

In python 2.x, I can use the universal function numpy.divide to do floor division. However, the ufunc divide and true_divide both do true division in python 3.x.

In [24]: np.divide([1, 2, 3, 4], 2)
Out[24]: array([ 0.5,  1. ,  1.5,  2. ])

In [25]: np.true_divide([1, 2, 3, 4], 2)
Out[25]: array([ 0.5,  1. ,  1.5,  2. ])

So what is the universal function in numpy with python3 to do floor division?

Alex Riley
  • 169,130
  • 45
  • 262
  • 238
Eastsun
  • 18,526
  • 6
  • 57
  • 81
  • 1
    It's kind of silly to call the explicit functions, when you can just use `/` and `//` (for true and floor division respectively). `//` always works the same way in Python 2, and `/` can be made to work as true division in Py2 by including `from __future__ import division` at the top of your module. – ShadowRanger Dec 10 '15 at 15:17
  • 1
    @ShadowRanger Sometimes I prefer ufunc. Since the ufunc can accept array like objects i.e. list, tuple, iterator etc. as the example I given in the question, which operator like // can't do. – Eastsun Dec 10 '15 at 15:37

1 Answers1

3

This is the NumPy ufunc np.floor_divide:

>>> np.floor_divide([1, 2, 3, 4], 2)
array([0, 1, 1, 2])

Alternatively you can use the // operator:

>>> a = np.array([-2, -1, 0, 1, 2])
>>> a // 2
array([-1, -1,  0,  0,  1])
Alex Riley
  • 169,130
  • 45
  • 262
  • 238