向量归一化时浮点精度异常低
我的代码中有一个简短的方法来标准化向量(实际上是 PCL 点),这会产生低精度的结果。代码:
void normalize(pcl::PointXYZ::PointXYZ * p){
float nf = 1/sqrt(p->x*p->x+p->y*p->y+p->z*p->z);
//nf is a normalization factor precalculated to eliminate two FP divisions.
p->x*=nf; p->y*=nf; p->z*=nf;
}
此函数传递坐标为 (-0.850650787, 1.37638187, -0.525731087)
的点。调试显示在计算第二行后 nf=0.587785244
。当我在 Mathematica 中进行相同的计算时,nf=0.617708029
。这个误差超过5%! p 的坐标永远不会大于 2 或小于 -2。这种不准确是这些操作的典型现象,还是有什么问题?
I have a short method in my code to normalize a vector (actually a PCL point) which produces results of low accuracy. The code:
void normalize(pcl::PointXYZ::PointXYZ * p){
float nf = 1/sqrt(p->x*p->x+p->y*p->y+p->z*p->z);
//nf is a normalization factor precalculated to eliminate two FP divisions.
p->x*=nf; p->y*=nf; p->z*=nf;
}
This function is passed the point with coordinates (-0.850650787, 1.37638187, -0.525731087)
. Debugging shows that nf=0.587785244
after evaluation of the second line. When I do the same calculation in Mathematica, nf=0.617708029
. This is an error of more than 5%! The coordinates of p are never greater than 2 or less than -2. Is this inaccuracy typical for these operations, or is there something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据我的计算,
0.587785244
是正确的结果(我使用 Perl 得到0.5877852727698576
)。我怀疑你在 Mathematica 中的计算不正确。According to my calculations,
0.587785244
is the correct result (I get0.5877852727698576
using Perl). I suspect you're doing the calculation incorrectly in Mathematica.你搞乱了 Mathematica 中的计算。 wolframalpha 给出相同的结果 C 做。
You messed up the calculation in Mathematica. wolframalpha gives the same result C does.