Python 为什么pybind11将double转换为int?
我创建了.pyd文件:Python 为什么pybind11将double转换为int?,python,c++,pybind11,Python,C++,Pybind11,我创建了.pyd文件: #include <pybind11/pybind11.h> #include <iostream> #include <typeinfo> namespace py = pybind11; int add(int num) { float a = 1.0; for (int i = 0; i <= num; i = i + 1) { a = (a + i)/a; }
#include <pybind11/pybind11.h>
#include <iostream>
#include <typeinfo>
namespace py = pybind11;
int add(int num) {
float a = 1.0;
for (int i = 0; i <= num; i = i + 1) {
a = (a + i)/a;
}
std::cout << "dll is typing: " << a << '\n';
std::cout << typeid(a).name() << std::endl;
return a;
}
PYBIND11_MODULE(py_dll, m) {
m.doc() = "pybind11 py_dll plugin"; // optional module docstring
m.def("add", &add, "Add function", py::arg("num"));
}
它打印:
为什么数字变成int?我说的是22岁左右。我希望这个函数是float 22.8722
int add(int num)
将int
作为参数并返回int
。“问题”不在于pybind。请尝试以下内容:
int main() {
auto x = add(42);
std::cout << "return value: " << x << '\n';
std::cout << typeid(x).name() << std::endl;
}
暂时忘记pybind,那么
add
返回什么?它不是浮点数,是吗?@idclev463035818,嗯。。。。我希望它能恢复浮动。因为变量a是浮动的。
int main() {
auto x = add(42);
std::cout << "return value: " << x << '\n';
std::cout << typeid(x).name() << std::endl;
}
float add(int num)