python - Armadillo 中的矩阵( vector )在从文件加载后获得新的小数位

标签 python c++ armadillo

我想将源代码从 python 转移到 c++ (#include < armadillo >)。 我有一个 vector (矩阵)保存到“my_vec.txt”,尺寸为 1x200:

-0.082833
0.151422
-0.088526
...
...
0.115863
0.131043
0.041844

我想在 python 中计算两个 my_vec 的点积(这只是一个测试示例)。

result = my_vec.dot(my_vec)
print (str.format("{0:.10f}", result))

结果是 6.1402435303

当我尝试在 C++ 中使用 Armadillo 执行相同的操作时:

float result;
result = dot(my_vec, my_vec);
std::cout << std::setprecision(10) << result;

我得到 6.140244007

因此,在从文本文件加载值后,我在 Armadillo 中查看了我的浮点 vector my_vec。这是它的样子:

 -8.283299952745e-002
  1.514219939709e-001
 -8.852600306273e-002
 ...
 ...
 1.158630028367e-001
 1.310430020094e-001
 4.184399917722e-002

添加了这么多小数位(my_vec.txt 中不存在)。当然,这种差异会影响进一步的计算。我怎样才能防止这种情况发生?

最佳答案

看起来您在 C++ 代码中使用的精度较低。 C++ float 通常对应NumPy float32;如果你想要精度相当于 NumPy float64,那通常是 C++ double

关于python - Armadillo 中的矩阵( vector )在从文件加载后获得新的小数位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36073143/

相关文章:

C++ Armadillo : mat initialization from vector<vector<double>> destroys data

c++ - 使用python运行C++程序并测试

c++ - arma::rowvec 来自 std::vector<int>

python - 拆分配置文件中的文本

python - 比较 Sympy 因子结果为 false

python - bs4.FeatureNotFound : Couldn't find a tree builder with the features you requested: lxml. 需要安装解析器库吗?

实现三个相似整数范围运算符的 Pythonic 方式?

c++ - 将实例数组实现为不同类型的好方法是什么

c++ - boost:bind 和两个具有相同名称和参数计数的方法

c++ - 在 Armadillo C++ 中为矩阵类型变量获取键盘输入