首页 > 解决方案 > Cython 拒绝融合 dtype 中的 float32

问题描述

我正在尝试编写一个 Cython 函数,该函数能够使用包含 float32 的融合 dtype。但是,除非明确指定,否则它会拒绝输入。为什么 ?

例如:

cimport numpy as cnp

ctypedef fused floatdtype:
    cnp.npy_float32

@cy.boundscheck(False)
@cy.wraparound(False)
cpdef void test(floatdtype smth):
    print(smth)

然后在 Python 中,下面会引发“TypeError:未找到匹配的签名”错误:

import numpy as np

smth= np.float32(19700101.0)
test(smth)

然而,这工作得很好:

cimport numpy as cnp

@cy.boundscheck(False)
@cy.wraparound(False)
cpdef void test( cnp.npy_float32 smth):
    print(smth)

标签: pythoncnumpycython

解决方案


推荐阅读