浮点数舍入问题
我在 CUDA 中完成了论文,但我遇到了最后一个问题,这与舍入浮点数有关。
我有一个名为 bin 的整数变量,它是 x+y*X_dim 的编码。给定 bin,我想找到起源它的 x 和 y 坐标,这样我就可以进行对称性计算。这是我原来的程序:
float yaux,xaux;
yaux=(float)floorf((float)bin/((float)DETECTOR_X_DIM));
if(abs(yaux-floorf(yaux)) < 0.0001)
yaux=floorf(yaux);
else
yaux=ceilf(yaux);
xaux=(float)((float)(((float)bin/((float)DETECTOR_X_DIM))-(float)yaux)*((float)DETECTOR_X_DIM));
return (int)xaux;
if(abs(xaux-floorf(xaux)) < 0.0001)
xaux=floorf(xaux);
else
xaux=ceilf(xaux);
return (int)xaux;
xaux = (float)DETECTOR_X_DIM - xaux -(float)1;
return (int)xaux+(int)yaux*DETECTOR_X_DIM;
问题是它适用于检测器的某些箱,但不适用于其他箱(它返回 xaux 加 1)。有更好的方法吗?
先感谢您
I finished my thesis in CUDA but I am having a final problem, that has to do with rounding float numbers.
I have an integer variable named bin that is the codification of x+y*X_dim. Given the bin I want to find the x and y coordenates that originated it so I can do a symmetry calculation. This is my original program:
float yaux,xaux;
yaux=(float)floorf((float)bin/((float)DETECTOR_X_DIM));
if(abs(yaux-floorf(yaux)) < 0.0001)
yaux=floorf(yaux);
else
yaux=ceilf(yaux);
xaux=(float)((float)(((float)bin/((float)DETECTOR_X_DIM))-(float)yaux)*((float)DETECTOR_X_DIM));
return (int)xaux;
if(abs(xaux-floorf(xaux)) < 0.0001)
xaux=floorf(xaux);
else
xaux=ceilf(xaux);
return (int)xaux;
xaux = (float)DETECTOR_X_DIM - xaux -(float)1;
return (int)xaux+(int)yaux*DETECTOR_X_DIM;
The problem is that it works for some bins of the detector but it doesnt work for others(it returns xaux added by 1). Is there some better way to this?
Thank you in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您的意思是
bin = x+y*X_dim
其中 x,y 是您希望在给定浮点数bin
的情况下恢复的整数索引(不一定是整数)并且已知整数常量X_dim
。我将此基于您的其他问题(CUDA 内核向量的长度基于 threadIdx 和 CUDA 3D 矩阵索引),其中你的代码更具可读性。在这种情况下,您需要 Matlab 的 sub2idx 函数 的简化等效项:但是,我假设您想沿 x 轴向下舍入
bin
。双线性插值可能更合适,但对于这样一个不明确的问题来说,假设太多了。I assume you meant that
bin = x+y*X_dim
where x,y are integer indices you wish to recover given a floating point numberbin
(not necessary an integer) and a known integer constantX_dim
. I'm basing this on your other questions (CUDA kernel's vectors' length based on threadIdx and CUDA 3D matrix index) where your code was more readable. In that case you want a simplified equivalent of Matlab's sub2idx function:However, I've assumed you want to round down
bin
along the x-axis. Bilinear interpolation might be more appropriate, but that's far too many assumptions for such an unclear question.