Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么pybind11将double转换为int?_Python_C++_Pybind11 - Fatal编程技术网

Python 为什么pybind11将double转换为int?

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; }

我创建了.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;
    }        
    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)