Numba: np.round_() fails at nopython

Created on 14 Dec 2017  路  3Comments  路  Source: numba/numba

Hello,

I tried to define a function rounding a vector:

@jit(float64[:](float64[:]),nopython=True)
def rnd(x):
    return np.round_(x)

Based on your website I assumed that this would work, as np.round_ is supported, but it throws the usual error:
TypingError: Failed at nopython (nopython frontend) Can I please ask where the problem might be?

Numba version: 0.36.1
Numpy version: 1.13.1

Thanks in advance for help.

bug

Most helpful comment

Thanks for the report. From a quick look at the implementation, I think this is because there is at present no function definition for a signature without an out arg specified. Provision of the 3 arg signature does work:

from numba import jit, njit
from numba.types import float64, int64
import numpy as np

@jit(float64[:](float64[:], int64, float64[:]),nopython=True)
def rnd1(x, decimals, out):
    return np.round_(x, decimals, out)

x=np.arange(10.) + 0.2
print(x)
print(np.round_(x))
y=np.empty_like(x)
print(rnd1(x, 0, y))

gives:

$ python issue2648.py 
[ 0.2  1.2  2.2  3.2  4.2  5.2  6.2  7.2  8.2  9.2]
[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]
[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]

All 3 comments

I can confirm I am also seeing this problem.

Thanks for the report. From a quick look at the implementation, I think this is because there is at present no function definition for a signature without an out arg specified. Provision of the 3 arg signature does work:

from numba import jit, njit
from numba.types import float64, int64
import numpy as np

@jit(float64[:](float64[:], int64, float64[:]),nopython=True)
def rnd1(x, decimals, out):
    return np.round_(x, decimals, out)

x=np.arange(10.) + 0.2
print(x)
print(np.round_(x))
y=np.empty_like(x)
print(rnd1(x, 0, y))

gives:

$ python issue2648.py 
[ 0.2  1.2  2.2  3.2  4.2  5.2  6.2  7.2  8.2  9.2]
[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]
[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]

Works fine, thanks for help.

Was this page helpful?
0 / 5 - 0 ratings