I created.pyd file:
#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"));
}
I call it from python:
import py_dll
num = 500
a = py_dll.add(num)
print ('python is typing: ', a)
It prints:
Why digit became int? I speak about 22. I expect it to be float 22.8722
This function
int add(int num)
takes an int
as parameter and returns an int
. The "problem" is not with pybind. Try this to see:
int main() {
auto x = add(42);
std::cout << "return value: " << x << '\n';
std::cout << typeid(x).name() << std::endl;
}
If the function should return a float
you have to declare that it returns a float
:
float add(int num)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.