另外,您在 C++ 中使用 32 位浮点数,这会产生与 python 的 64 位(?)数字不同的浮点错误。将类型从 float 更改为 double 至少会为 tot 和 ris< 创建相同的结果(除了较小的浮点错误) /代码>。感谢@Michael Szczesny 指出这个错误。
Python is an interpreted language while C++ gets compiled to machine code. This enables a C++ compiler to produce highly optimized code while the python interpreter "has to think more in order to execute the program".
You could compile the python code (as Samwise said), e.g., with Cython.
Also, you are using 32-bit floats in C++ which yield different floating point errors than the 64-bit (?) numbers of python. Changing the type from float to double at least created the same (apart from minor floating point errors) result for tot and ris. Thanks @Michael Szczesny for pointing towards this bug.
发布评论
评论(1)
Python 是一种解释性语言,而 C++ 则被编译为机器代码。这使得 C++ 编译器能够生成高度优化的代码,而 python 解释器“必须考虑更多才能执行程序”。
您可以编译 python 代码(如 Samwise 所说),例如,使用 Cython 。
另外,您在 C++ 中使用 32 位浮点数,这会产生与 python 的 64 位(?)数字不同的浮点错误。将类型从
float
更改为double
至少会为tot
和ris< 创建相同的结果(除了较小的浮点错误) /代码>。感谢@Michael Szczesny 指出这个错误。
Python is an interpreted language while C++ gets compiled to machine code. This enables a C++ compiler to produce highly optimized code while the python interpreter "has to think more in order to execute the program".
You could compile the python code (as Samwise said), e.g., with Cython.
Also, you are using 32-bit floats in C++ which yield different floating point errors than the 64-bit (?) numbers of python. Changing the type from
float
todouble
at least created the same (apart from minor floating point errors) result fortot
andris
. Thanks @Michael Szczesny for pointing towards this bug.