我使用 Eigen 计算旋转矩阵,并在 Linux 和 Windows 平台上运行相同的 C++ 代码,如下所示:
void testEigen()
{
Eigen::AngleAxisd xRot(0.0, Eigen::Vector3d::UnitX());
Eigen::AngleAxisd yRot(-M_PI/2, Eigen::Vector3d::UnitY());
Eigen::AngleAxisd zRot( M_PI/2, Eigen::Vector3d::UnitZ());
Eigen::Quaternion<double> q = xRot * yRot * zRot;
auto _rotationMatrix = q.matrix();
// for (size_t i = 0; i < 3; i++)
// {
// for (size_t j = 0; j < 3; j++)
// {
// if (std::abs(_rotationMatrix(i, j)) < 1e-13)
// {
// _rotationMatrix(i, j) = 0.0;
// }
// if (std::abs((std::abs(_rotationMatrix(i, j)) - 1.0)) < 1e-13)
// {
// _rotationMatrix(i, j) = _rotationMatrix(i, j) < 0 ? -1.0 : 1.0;
// }
// }
// }
TRACE(4, "angle axisd x: " << std::defaultfloat << xRot.angle() << " y: " << yRot.angle() << " z: " << zRot.angle());
TRACE(4, "m_axis axisd x: " << xRot.axis() << " y: " << yRot.axis() << " z: " << zRot.axis());
TRACE(4, "Rotate matrix debug init _rotationMatrix\n0: " << _rotationMatrix.row(0) << "\n1: " << _rotationMatrix.row(1) << "\n2: " << _rotationMatrix.row(2));
}
但是输出结果不太一致,如下图:
窗户:
角度轴 x:0 y:-1.5707963267948965579989817342721 z:1.5707963267948965579989817342721
m_axis 轴 x: 1, 0, 0 y: 0, 1, 0 z: 0, 0, 1
旋转矩阵调试初始化_rotationMatrix
0:-4.4408920985006261616945266723633e-16 0 -1.0000000000000004440892098500626
1:1.0000000000000004440892098500626 -4.4408920985006261616945266723633e-16 0
2: 0 -1.0000000000000004440892098500626 -4.4408920985006261616945266723633e-16
Linux:
角度轴 x:0 y:-1.5707963267948965579989817342721 z:1.5707963267948965579989817342721
m_axis 轴 x: 1, 0, 0 y: 0, 1, 0 z: 0, 0, 1
旋转矩阵调试初始化_rotationMatrix
0: 0 -2.2204460492503130808472633361816e-16 -1
1: 1 2.2204460492503130808472633361816e-16 0
2:2.220446049250313080847263336181e-16 -1 2.2204460492503130808472633361816e-16
在Windows平台上使用MSVC 2019 AMD 64。
使用Linux平台上使用的Gcc debian 12.2.0。
Eigen 库版本:Eigen 3.3.7
我想让两个平台的运行结果一致,谢谢!
不同的编译器产生不同的计算机命令,这就是导致不同结果的原因。顺便问一下,您是否使用同一台 com 计算机进行计算?此外,一些“标准”库函数确实有不同的实现。此外,众所周知,即使具有不同优化级别的同一编译器也会产生轻微(或可能不会)的计算结果。只是,由于不同的命令序列的舍入误差是不同的。因此,不同的结果并不一定是错误。