Can I cast a scalar float64 to float32 in Numba?

import numba

@numba.njit
def foo(x):
    return np.float32(x)

x = np.float64(0.0)
type(foo(x))  # float

The .astype attribute is not available, and the jitted function does nothing either. The only thing I managed is to convert between integers and floats, but not to specify the precision

The function does indeed return a float32. It’s being automatically converted to a Python float.

If you print the function nopython_signatures you’ll see the return type is float32

1 Like

I see. Is there a fundamental reason why numba cannot / is not returning numpy scalars?

Unfortunately the distinction between NumPy and Python scalars in Numba is a bit fuzzy, and this is one instance of that general treatment.