[英]How to pass a default numpy array argument to a function in pybind11?
[英]How to pass float to a pybind11 function expecting an int
我从 pybind11 文档中复制了示例:
#include <pybind11/pybind11.h>
namespace py = pybind11;
int add(int i, int j) {
return i + j;
}
PYBIND11_MODULE(example, m) {
m.def("add", &add, "A function which adds two numbers");
}
像这样构建它:
g++ -fPIC -shared -I/usr/include/python3.7 -o example.so example.cpp
然后:
$ python3
Python 3.7.3 (default, Jul 25 2020, 13:03:44)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from example import *
>>> add(1, 2)
3
>>> add(1.0, 2.0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: add(): incompatible function arguments. The following argument types are supported:
1. (arg0: int, arg1: int) -> int
Invoked with: 1.0, 2.0
>>>
我意识到我可以重写add
以采用double
arguments ,然后在add
的主体内截断它们。 但是有没有办法告诉 pybind11 可以将浮点数传递给 function (即使它需要int
参数)并让它自动执行转换?
根据文档,相反(从 integer 类型转换为double
)是隐式自动完成的。
默默地和隐式地截断为int
是一件可怕的事情, pybind11
不会为你做这件事。 如果您必须允许,您可以为每个参数类型创建一个重载的 function,并让double
变体代表您进行截断(请记住,这可能不仅会丢失小数点后的精度; double
可以表示比任何 integer 类型,所以这些数字会发生坏事)。
一个粗略的例子是:
#include <pybind11/pybind11.h>
namespace py = pybind11;
int add_int(int i, int j) {
return i + j;
}
int add_double_as_int(double i, double j) {
return add_int((int)i, (int)j);
}
PYBIND11_MODULE(example, m) {
m.def("add", &add_int, "A function which adds two integers");
m.def("add", &add_double_as_int, "A function which adds two floats after truncating to int");
}
在 C++ 级别,函数具有不同的名称(您可以通过类似于使用 class 方法的示例进行创造性转换来避免这种情况,但它几乎不值得麻烦),但在 ZA7F5F35426B9274111FC923 级别,它们出现,作为具有文档中描述的多个原型的单个 API。
C++ 是强类型的。 只有当它可以安全地完成时,它才会对基本类型进行自动类型转换。 由于 integer 不能代表双精度罐 C++ 无法自动执行转换的每个值。 因此,您需要自己明确地进行转换。 我不确定这是如何在 python 中完成的。 在 C++ 中,最好使用:
#include \<cmath\>
double dbl = 3.5;
int newval = std::round(dbl);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.