涉及双精度数和整数的错误 C# 代码
for (iy = 0; iy < h; iy++)
{
double angy = (camera.fov_y / h) * iy;
for (ix = 0; ix < w; ix++)
{
double angx = (camera.fov_x / w) * ix;
//output[ix,iy].r = (int)Math.Round(255 * (angy / camera.fov_y);
//output[ix,iy].b = (int)Math.Round(255 * (angy / camera.fov_y);
double tr = (angx / camera.fov_x) * 255D;
double tb = (angy / camera.fov_y) * 255D;
Console.Write("({0},{1})",Math.Round(tr),Math.Round(tb));
output.SetPixel(ix, iy, Color.FromArgb(Convert.ToInt32(tr), 0, Convert.ToInt32(tb)) );
Console.Write(".");
}
Console.WriteLine();
}
任何人都可以看到该代码有任何直接问题吗? 变量 tr
和 tb
的计算结果始终为 0。
如果需要,我很乐意提供更多信息。
for (iy = 0; iy < h; iy++)
{
double angy = (camera.fov_y / h) * iy;
for (ix = 0; ix < w; ix++)
{
double angx = (camera.fov_x / w) * ix;
//output[ix,iy].r = (int)Math.Round(255 * (angy / camera.fov_y);
//output[ix,iy].b = (int)Math.Round(255 * (angy / camera.fov_y);
double tr = (angx / camera.fov_x) * 255D;
double tb = (angy / camera.fov_y) * 255D;
Console.Write("({0},{1})",Math.Round(tr),Math.Round(tb));
output.SetPixel(ix, iy, Color.FromArgb(Convert.ToInt32(tr), 0, Convert.ToInt32(tb)) );
Console.Write(".");
}
Console.WriteLine();
}
Can anyone see any immediate problems with that code?
The variables tr
and tb
always evaluate to 0.
I'm happy to provide more information if it is needed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还没有给出其他变量的类型 - 特别是,
camera.fov_x
和camera.fov_y
的类型是什么? 如果它们都是整数,则初始化angx
和angy
的行将使用整数算术进行计算。这可以通过转换操作数之一来解决:
尽管
fovy
和fovx
变量已经是双精度数,但这不是问题。您能否给出一个完整的示例,我们可以自己编译和测试?
编辑:Koistya Navin 的编辑太过分了。 只需要表达式的一个操作数为双精度数,即可使用双精度算术计算整个表达式。 (不过,它必须是正确的表达式 - 如果您执行
(a/b) * c
并将c
转换为 double,则乘法将使用 double 算术完成,但 a /b 可能仍以整数形式完成。)以下是适当更改的列表,以确保在所有应该使用的地方都使用双精度算术:
You haven't given the types for the other variables - in particular, what are the types of
camera.fov_x
andcamera.fov_y
? If they're both integers, then the lines initializingangx
andangy
will be evaluated using integer arithmetic.This can be fixed by casting one of the operands:
The
fovy
andfovx
variables are already doubles though, this isn't the problem.Could you give a complete example of this, which we can compile and test ourselves?
EDIT: Koistya Navin's edit is overkill. You only need one operand of an expression to be a double for the whole thing to be computed using double arithmetic. (It needs to be the right expression though - if you do
(a/b) * c
and castc
a double, the multiplication will be done with double arithmetic but a/b might still be done as integers.)Here's the listing changed appropriately to make sure double arithmetic is used everywhere it should be:
不要忘记将整数转换为双精度数。 例如:
快速参考:
Don't forget to cast your integers to doubles. For example:
Quick Reference:
它知道这不是您原来问题的一部分,但使用 SetPixel(..) 效率不是很高,如果您计划在光线跟踪引擎中使用它,可能会出现问题。
您可能想使用 LockBits() 方法,请参阅此 答案和这个了解更多详细信息。 另一种方法是使用“不安全”C# 代码访问数据,该代码允许您使用指向数据的指针。 有关详细信息,请参阅此问题,我通过使用“获得了~x2的加速”不安全”的代码。
It know it's not part of your original question, but using SetPixel(..) is not very efficient and could be a problem if you are planning to use it in a ray-tracing engine.
You might want to use the LockBits() method, see this answer and this one for more details. They other way to go is to access the data using "unsafe" C# code, which allows you to uses pointers to the data. See this question for more info, I got a ~x2 speed-up by using "unsafe" code.